– 資料
stu_score
id userid subject score
1,001,語文,90
2,001,數學,92
3,001,英語,80
4,002,語文,88
5,002,數學,90
6,002,英語,75.5
7,003,語文,70
8,003,數學,85
9,003,英語,90
10,003,政治,82
預期結果:
userid 語文 數學 英語 政治 總成績
彙總 248.0 267.0 245.5 82.0 842.5
001 90.0 92.0 80.0 0.0 262.0
002 88.0 90.0 75.5 0.0 253.5
003 70.0 85.0 90.0 82.0 327.0
最初實現
select
userid,
sum(
case
when subject=
"語文"
then score else
0end)as
`語文`
,sum
(case
when subject=
"數學"
then score else
0end)as
`數學`
,sum
(case
when subject=
"英語"
then score else
0end)as
`英語`
,sum
(case
when subject=
"政治"
then score else
0end)as
`政治`
,sum
(score)
as`總成績`
from
stu_score
group
by userid
union
select
"彙總"
,sum
(case
when subject=
"語文"
then score else
0end)as
`語文`
,sum
(case
when subject=
"數學"
then score else
0end)as
`數學`
,sum
(case
when subject=
"英語"
then score else
0end)as
`英語`
,sum
(case
when subject=
"政治"
then score else
0end)as
`政治`
,max
(sum_score) sum_score
from
(select
stu_score.userid,
stu_score.subject,
stu_score.score,
sum(score)
over
() sum_score
from
stu_score
) t1
效能:開啟了4個job用時 176s 不是很理想
改進(使用cube)
select
nvl(userid,
"彙總"),
sum(
case
when subject=
'語文'
then score else
0end
)`語文`
,sum
(case
when subject=
'數學'
then score else
0end
)`數學`
,sum
(case
when subject=
'英語'
then score else
0end
)`英語`
,sum
(case
when subject=
'政治'
then score else
0end
)`政治`
,sum
(case
when subject is
null
then score else
0end
) total
from
(select
userid,
subject,
sum(score) score
from
stu_score
group
by userid,subject with cube
) tgroup
by userid;
效能只執行了乙個job,用時33s !
Hive之 資料操作
基本語法 select all distinct select expr,select expr,from tablename where where condition 1 hive命令列執行select from lyz 2 linux命令列執行hive e select from lyz hi...
第二章 HIve安裝之Hive其他命令操作
1 退出hive視窗 hive default exit hive default quit 在新版的hive中沒區別了,在以前的版本是有的 exit 先隱性提交資料,再退出 quit 不提交資料,退出 2 在hive cli命令視窗中如何檢視hdfs檔案系統 hive default dfs ls...
swift之字串的操作彙總
建立空字串 let str1 string gggggg let str2 string string 建立賦初值的字串 var str3 hello var str4 string 你好 判斷字串是否為空 if str.isempty 字串長度 if str.characters.count 0 ...