我正在尝试使用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)
答案 0 :(得分:2)
试试这个
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)