[英]Conditional aggregate with Group By clause
我正在嘗試使用HiveQL,但我不知道如何在SQL中執行此操作。 表結構如下:
id1 id2 category 123 abc 1 123 def 1 123 def 2 456 abc 1 123 abc 1 123 abc 2 ...
我想寫一個輸出的查詢:
key count category1count category2count 123-abc 3 2 1 123-def 2 1 1 456-abc 1 1 0
到目前為止,我有這個:
SELECT concat( concat(id1,'-'), id2), count(*) ,
count( SELECT * WHERE buyingcategory = 1 ??? ) ,
count( SELECT * WHERE buyingcategory = 2 ??? )
FROM table
GROUP BY concat( concat(id1,'-'), id2)
嘗試這個
SELECT concat(id1,'-', id2) `key`, count(*) ,
sum( case when category = 1 then 1 else 0 end) category1count ,
sum( case when category = 2 then 1 else 0 end) category2count
FROM table1
GROUP BY concat(id1,'-', id2)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.