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

78 篇文章 10 订阅

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

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值