Hive:无法获得不同类别的总和

时间:2013-03-25 11:46:29

标签: hadoop hive hiveql

来自Hive,我试图从简单的表格中获得结果,该表格如下所示

custername Prjtid Hours  Billable_Status

ABC         AB123  10     Billable

ABC         AB123  20     Non-Billable

ABC         AC123  10     Billable

ABC         AB123  30     Billable

PQR         PQ123  20     Billable

PQR         PQ123  30     Billable

PQR         PQ123  20     Non-Billable

现在我要显示像,

Custername, Prjtid, (Total number of billable), (total number of non-billable).

示例:

ABC, AB123, 40, 20

PQR, PQ123, 50, 20

我可以获得Billable或不可计费但不能合计。

有人可以建议如何继续这种情况吗?

此致

拉​​吉

2 个答案:

答案 0 :(得分:2)

分组应该给出你需要的东西:

SELECT Custername, Prjtid, 
SUM(CASE WHEN Billable_Status = 'Billable' THEN Hours ELSE 0 END ),
SUM(CASE WHEN Billable_Status = 'Non-Billable' THEN Hours ELSE 0 END )
FROM table
GROUP BY Custername,Prjtid;

答案 1 :(得分:0)

可能不在主题上,但以下内容适用于Mysql,

  

选择a.custername,a.Prjtid,a.billable_hours AS Billable   ,b.not_billable_hours AS NonBillable FROM(SELECT   custername,Prjtid,sum(Hours)AS billable_hours FROM table WHERE   Billable_Status ='Billable'GROUP BY custername)AS a,(SELECT   custername,Prjtid,sum(Hours)AS not_billable_hours FROM table WHERE   status ='Non-Billable'GROUP BY custername)AS b WHERE   a.custername = b.custername;