Re: need help to write sql query

Those NULLs complicate the logic a bit:

SELECT ...
Row_Number ()
Over (PARTITION BY CalimNo
ORDER BY loaddatekey) AS rn,
Min(Tvalue)
Over (PARTITION BY CalimNo
ORDER BY loaddatekey
ROWS BETWEEN 1 Preceding AND 1 Preceding) AS prev_val
FROM mytable
QUALIFY rn = 1
OR Tvalue <> prev_val
OR (Tvalue IS NULL AND prev_val IS NOT NULL)
OR (Tvalue IS NOT NULL AND prev_val IS NULL)

Or you might use COALESCE if you know a value which will never exist:

SELECT ...
Min(Coalesce(Tvalue, 'ööö'))
Over (PARTITION BY CalimNo
ORDER BY loaddatekey
ROWS BETWEEN 1 Preceding AND 1 Preceding) AS prev_val
FROM vt
QUALIFY
prev_val <> Coalesce(Tvalue, 'ööö')
OR prev_val IS NULL

Re: need help to write sql query

Those NULLs complicate the logic a bit:

SELECT ...
Row_Number ()
Over (PARTITION BY CalimNo
ORDER BY loaddatekey) AS rn,
Min(Tvalue)
Over (PARTITION BY CalimNo
ORDER BY loaddatekey
ROWS BETWEEN 1 Preceding AND 1 Preceding) AS prev_val
FROM mytable
QUALIFY rn = 1
OR Tvalue <> prev_val
OR (Tvalue IS NULL AND prev_val IS NOT NULL)
OR (Tvalue IS NOT NULL AND prev_val IS NULL)

Or you might use COALESCE if you know a value which will never exist:

SELECT ...
Min(Coalesce(Tvalue, 'ööö'))
Over (PARTITION BY CalimNo
ORDER BY loaddatekey
ROWS BETWEEN 1 Preceding AND 1 Preceding) AS prev_val
FROM vt
QUALIFY
prev_val <> Coalesce(Tvalue, 'ööö')
OR prev_val IS NULL