1. 程式人生 > 其它 >學習筆記: sql

學習筆記: sql

行轉列 pivot函式

select VendorId ,
sum(case when  IncomeDay='MoN' then IncomeAmount else 0 end) MON,
sum(case when  IncomeDay='TUE' then IncomeAmount else 0 end) TUE,
sum(case when  IncomeDay='WED' then IncomeAmount else 0 end) WED,
sum(case when  IncomeDay='THU' then IncomeAmount else 0 end) THU,
sum(case when
IncomeDay='FRI' then IncomeAmount else 0 end) FRI, sum(case when IncomeDay='SAT' then IncomeAmount else 0 end) SAT, sum(case when IncomeDay='SUN' then IncomeAmount else 0 end) SUN from DailyIncome group by VendorId
select * from DailyIncome
pivot (max (IncomeAmount) for IncomeDay in ([MON],[TUE
],[WED],[THU],[FRI],[SAT],[SUN])) as MaxIncomePerDay where VendorId in ('SPIKE')

這就需要使用到行轉列,注意,這裡需要將括號去掉。


如將useridList->[123,345,234,2334]轉為
123
345
234
2334

select
userid2 from ( select substr(useridList,2,length(useridList)-2) as userid from a ) t1 lateral view explode(split(userid, ',')) t1 as
userid2

連結:https://zhuanlan.zhihu.com/p/214254848