简体   繁体   中英

Hive query to group

I have the following table columns: ID (id), Type (string. could be either: open or close)

I would like to get an output count where for each unique ID, I count how many times the ID got the open type and how many times it got the close type.

So the output would look like this:

ID | openCount | closeCount
10 | 23        | 2

Any ideas?

select ID,count( * ) AS "openCount" where type = "open" group by ID union select ID,count( * ) AS "closeCount" where type = "close" group by ID;

that should do it. it's count(star) stack overflow doesn't seem to like that :)

SELECT id, SUM(type='open') openCount, SUM(type='closed') closedCount FROM table GROUP BY id;

发生的是,SUM()函数汇总了具有相同id值的所有行的所有type='open'type='closed'值,如果true为1 ,则false为0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM