鉴于此输入数据:
SEQ_ID SP_ID MASK STRING G_ID DEPTH PRESSURE
97 52361796 1 1 1 5.37 1941.0
97 52361796 1 1 2 5.77 1986.0
97 52361796 1 1 3 6.65 1922.0
....
97 52361800 1 1 1 5.76 1926.0
97 52361800 1 1 2 5.39 1922.0
97 52361800 1 1 3 6.85 2092.0
如何将其转置为:
SP_ID MASK P_01 ... P_027 D_01 D_027
52321798 2 0 ... 2084.0 ... 0 ... 2084.0 ...
52361796 1 1941.0 ... 0 ... 5.37 ... 0 ...
52361800 1 1926.0 ... 0 ... 5.76 ... 0 ...
到目前为止代码创建了以上输出:
CREATE TEMP VIEW IF NOT EXISTS vInteractive AS
SELECT
SP_ID
, MASK
, SUM(CASE WHEN G_ID=1 THEN PRESSURE END) AS P_01
, SUM(CASE WHEN G_ID=27 THEN PRESSURE END) AS P_27
, SUM(CASE WHEN G_ID=1 THEN DEPTH END) AS D_01
, SUM(CASE WHEN G_ID=27 THEN DEPTH END) AS D_27
FROM vGun
GROUP BY SP_ID
ORDER BY SP_ID
;
我想知道如果不是用这种蛮力的方法来编码n行,我可以用递归CTE做更聪明的事情。
我知道G_ID的最大值可以为递归设置上限。