2

从 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

我能够获得计费或不可计费但不能一起获得。

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

问候,

拉吉

4

2 回答 2

2

Group by 应该提供您需要的内容:

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;
于 2013-03-25T12:00:32.783 回答
0

可能不是主题,但以下适用于 Mysql,

SELECT 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;

于 2013-03-27T07:34:39.337 回答