Based on your narrative:
SELECT
test.*,
CASE
WHEN ROW_NUMBER() OVER (PARTITION BY col1,dt,col4 ORDER BY col3) = 1
THEN (dt-MIN(dt) OVER (PARTITION BY col1 ORDER BY dt,col4,col3 ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING))
- CASE
WHEN col3-MIN(col3)
OVER (PARTITION BY col1
ORDER BY dt,col4,col3
ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING) <= 0
THEN 1
ELSE 0
END
END
FROM testDieter
↧