Аналитические функции обрабатываются после агрегирования (где-из-группы путем-olap-qualify-order by), поэтому вы не можете применить агрегирование к результату OVER, вы должны вложить его в производную таблицуили общее табличное выражение:
SELECT
Sum(
CASE WHEN (status_previous='Test1' AND status_current='Test2')
OR (status_previous='Test3' AND status_current='Test2')
OR (status_previous='Test4' AND status_current='Test2')
THEN 1
ELSE 0
END) AS "Total_Change"
FROM
(
SELECT col1, col2,
Max(creation_dt_utc)
Over(PARTITION BY col1,col2,col3
ORDER BY creation_dt
ROWS BETWEEN 1 Preceding AND 1 Preceding) AS previous_creation_dt,
(creation_dt - prev_creation_dt) DAY(4) TO SECOND(6) AS time_difference,
Extract(DAY From time_difference) * 24*60 + Extract(HOUR From time_difference) * 60 + Extract(MINUTE From time_difference) AS Total_Minutes
FROM myTable
WHERE Extract(YEAR From year_column)=2017 -- the result of EXTRACT is an INTEGER, not a string
QUALIFY Total_Minutes<30
) AS dt
Но так как вам нужен только счет, вы можете переместить CASE в QUALIFY:
SELECT Count(*) AS "Total_Change"
FROM
(
SELECT col1, col2,
Max(creation_dt_utc)
Over(PARTITION BY col1,col2,col3
ORDER BY creation_dt
ROWS BETWEEN 1 Preceding AND 1 Preceding) AS previous_creation_dt,
(creation_dt - prev_creation_dt) DAY(4) TO SECOND(6) AS time_difference,
Extract(DAY From time_difference) * 24*60 + Extract(HOUR From time_difference) * 60 + Extract(MINUTE From time_difference) AS Total_Minutes
FROM myTable
WHERE Extract(YEAR From year_column)=2017 -- the result of EXTRACT is an INTEGER, not a string
QUALIFY Total_Minutes<30
AND ( (status_previous='Test1' AND status_current='Test2')
OR (status_previous='Test3' AND status_current='Test2')
OR (status_previous='Test4' AND status_current='Test2')
)
) AS dt
Edit:
Логика CASEможет быть дополнительно упрощено до:
CASE WHEN status_current='Test2' and status_previous IN ('Test1','Test3','Test4')
THEN 1
ELSE 0
END
или, возможно,
CASE WHEN status_current='Test2' and status_previous <>'Test2'
THEN 1
ELSE 0
END