这是使用COUNT
with的一种方法CASE
- 难以PIVOT
用于此类功能。
SELECT
Org,
COUNT(CASE WHEN Tipas = 'buy' AND Diena = 'Monday' THEN 1 END) Monday_Buy_Cnt,
COUNT(CASE WHEN Tipas = 'sell' AND Diena = 'Monday' THEN 1 END) Monday_Sell_Cnt,
COUNT(CASE WHEN Tipas = 'create' AND Diena = 'Monday' THEN 1 END) Monday_Create_Cnt,
COUNT(CASE WHEN Tipas = 'buy' AND Diena = 'Tuesday' THEN 1 END) Tuesday_Buy_Cnt,
COUNT(CASE WHEN Tipas = 'sell' AND Diena = 'Tuesday' THEN 1 END) Tuesday_Sell_Cnt,
COUNT(CASE WHEN Tipas = 'create' AND Diena = 'Tuesday' THEN 1 END) Tuesday_Create_Cnt,
...
FROM Testine T
GROUP BY Org
SQL 小提琴演示
然后您可以相应地添加其余的操作/天。有点长,但应该工作。
如果您需要每个组织每个操作多行,则将其添加到您的 group by 并从 CASE 语句中删除。例如:
SELECT
Org,
Tipas,
COUNT(CASE WHEN Diena = 'Monday' THEN 1 END) Monday_Cnt,
COUNT(CASE WHEN Diena = 'Tuesday' THEN 1 END) Tuesday_Sell_Cnt,
...
FROM Testine T
GROUP BY Org, Tipas
另一个小提琴