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

 

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