我有查詢返回以下結果:解決方法:SUM命令內使用LAG命令
-- Get current int_type and compare with next int_type. If they are different, put 1. Then sum all results
select CASE WHEN int_type <> LAG(int_type) OVER (ORDER BY id) THEN 1 END as next_int_type
from data
Output:
0
0
0
1
1
1
0
0
0
0
0
1
0
0
0
1
0
1
使用SUM命令的內部時滯命令的問題:
select SUM(CASE WHEN int_type <> LAG(int_type) OVER (ORDER BY id) THEN 1 END) as next_int_type
from data
Error: ERROR: aggregate function calls cannot contain window function calls
我如何可以總結上述結果在一個查詢中?
解決方法:
CREATE TEMP TABLE Temp AS
select SUM(CASE WHEN int_type <> LAG(int_type) OVER (ORDER BY id) THEN 1 END) as next_int_type
from data
select SUM(CASE WHEN int_type <> next_int_type THEN 1 ELSE 0 END) Total
from Temp ;
Output: 6
什麼數據庫系統與Google合作?請添加合適的標籤。 –