我試圖在不直接使用 pivot 函式的情況下對資料進行透視。
我有一個簡單的表t1,它有:
ID Employee Name
100 Amit
100 Rohan
101 Rohit
102 Pradnya
我的預期輸出是:
100 101 103
2 1 1
我想在不使用樞軸的情況下實作這一點。我嘗試使用:
SELECT *
FROM (SELECT CASE
WHEN id = '101' THEN '101'
END,
CASE
WHEN id = '102' THEN '102'
END,
CASE
WHEN id = '103' THEN '103'
END,
Count(*) cnt
FROM t1
GROUP BY CASE
WHEN id = '101' THEN '101'
END,
CASE
WHEN id = '102' THEN '102'
END,
CASE
WHEN id = '102' THEN '102'
END);
如何在沒有樞軸的情況下實作輸出?
uj5u.com熱心網友回復:
你有點在那里,請嘗試以下操作:
with s as (
select id, Count(*) cnt
from t
group by id
)
select
max(case when id=100 then cnt end) as '100',
max(case when id=101 then cnt end) as '101',
max(case when id=102 then cnt end) as '102'
from s
見示例小提琴
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/353908.html
上一篇:按計數和日期的SQL分組
