sql:hive:mysql:group by與distinct 去重

mysql:

源數據(mysql) 

id    userid    subject    score
1	001	語文	90.0
2	001	數學	92.0
3	001	英語	80.0
4	002	語文	88.0
5	002	數學	90.0
6	002	英語	75.5
7	003	語文	70.0
8	003	數學	85.0
9	003	英語	90.0
10	003	政治	82.0
11	001	語文	91.0
12	001	語文	92.0

注意:mysql雖然後邊group by 了兩個字段,但是前邊select後可跟多個字段,並且默認取第一個出現的記錄

但是hive 後邊group by幾個字段,前邊只能查出幾個字段,是固定的。

group by:

 select userid,subject,score from tb_score
 group by userid,subject
001	數學	92.0
001	英語	80.0
001	語文	90.0
002	數學	90.0
002	英語	75.5
002	語文	88.0
003	政治	82.0
003	數學	85.0
003	英語	90.0
003	語文	70.0

distinct:

select distinct userid,subject from tb_score
001	語文
001	數學
001	英語
002	語文
002	數學
002	英語
003	語文
003	數學
003	英語
003	政治

與group by 不同的是, mysql中group by兩個字段,但是可以查三個字段,而distinct兩個字段,那隻能查詢這兩個字段

那給distinct加上括號呢?

select distinct(userid) u,subject from tb_score
u    subject
001	語文
001	數學
001	英語
002	語文
002	數學
002	英語
003	語文
003	數學
003	英語
003	政治
 select distinct(userid) u,subject,score from tb_score
u    subject    score
001	語文	90.0
001	數學	92.0
001	英語	80.0
002	語文	88.0
002	數學	90.0
002	英語	75.5
003	語文	70.0
003	數學	85.0
003	英語	90.0
003	政治	82.0
001	語文	91.0
001	語文	92.0
select distinct(userid,subject),score from tb_score 
select distinct(userid,subject,score) from tb_score
以上這樣寫報錯:
 [Error Code: 1241, SQL State: 21000]  Operand should contain 1 column(s)

匯聚測試:

 select distinct userid,subject ,sum(score)  from tb_score

結果:
001	語文	1025.5
 select  userid,subject ,sum(score)  from tb_score group by userid,subject

結果:
001	數學	92.0
001	英語	80.0
001	語文	273.0
002	數學	90.0
002	英語	75.5
002	語文	88.0
003	政治	82.0
003	數學	85.0
003	英語	90.0
003	語文	70.0

 

hive:

源數據(hive)

C1     C2      C3      C4
A	B	D	D
C	D	A	B
C	D	A	B
A	B	C	C
B	B	C	C
B	B	D	D

group by:

 select C1,C2 from tmp.distinctt group by C1,C2 
C1    C2
B	B
A	B
C	D

distinct:

 select distinct c1,c2,c3,c4 from tmp.distinctt
C1     C2      C3      C4
B	B	D	D
B	B	C	C
A	B	D	D
A	B	C	C
C	D	A	B
select distinct c1,c2 from tmp.distinctt
效果同:
select distinct(c1),c2 from tmp.distinctt
C1    C2
B	B
A	B
C	D

那distinct加上括號呢?

select distinct(c1,c2) from tmp.distinctt
select distinct(c1,c2),c3 from tmp.distinctt
以上兩種寫法都報錯:

SQL 錯誤 [40000] [42000]: Error while compiling statement: FAILED: ParseException line 1:19 missing ) at ',' near 'c2'
line 1:22 missing EOF at ')' near 'c2'
  Error while compiling statement: FAILED: ParseException line 1:19 missing ) at ',' near 'c2'
line 1:22 missing EOF at ')' near 'c2'
  Error while compiling statement: FAILED: ParseException line 1:19 missing ) at ',' near 'c2'
line 1:22 missing EOF at ')' near 'c2'
    org.apache.hadoop.hive.ql.parse.ParseException:line 1:19 missing ) at ',' near 'c2'
line 1:22 missing EOF at ')' near 'c2'
    org.apache.hadoop.hive.ql.parse.ParseException:line 1:19 missing ) at ',' near 'c2'
line 1:22 missing EOF at ')' near 'c2'

匯聚測試:

 select distinct c1,c2,sum(case(c4 as int)) from tmp.distinctt

報錯:

SQL 錯誤 [40000] [42000]: Error while compiling statement: FAILED: ParseException line 1:35 mismatched input 'as' expecting ) near 'c4' in expression specification
  Error while compiling statement: FAILED: ParseException line 1:35 mismatched input 'as' expecting ) near 'c4' in expression specification
  Error while compiling statement: FAILED: ParseException line 1:35 mismatched input 'as' expecting ) near 'c4' in expression specification
    org.apache.hadoop.hive.ql.parse.ParseException:line 1:35 mismatched input 'as' expecting ) near 'c4' in expression specification
    org.apache.hadoop.hive.ql.parse.ParseException:line 1:35 mismatched input 'as' expecting ) near 'c4' in expression specification

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章