I can’t understand why my SQL doesn’t work
INSERT INTO per_sec_without_gaps
SELECT
row_number() OVER() AS Index,
UTCDateTime AS TimeKey,
(SELECT CASE WHEN CAST(without_gaps.UTCDateTime AS TIME) > CAST(min(UTCDateTime) AS TIME)
THEN first(UTCTradeDate) ELSE last(UTCTradeDate) END FROM per_sec),
CAST(UTCDateTime AS TIME),
(SELECT CASE WHEN CAST(without_gaps.LocalDateTime AS TIME) > CAST(min(LocalDateTime) AS TIME)
THEN first(LocalTradeDate) ELSE last(LocalTradeDate) END FROM per_sec),
CAST(LocalDateTime AS TIME),
(SELECT last(Ticker) FILTER(Ticker IS NOT NULL AND UTCDateTime <= TimeKey) FROM per_sec),
(SELECT last(BidPrice) FILTER(BidPrice IS NOT NULL AND UTCDateTime <= TimeKey) FROM per_sec),
(SELECT last(AskPrice) FILTER(AskPrice IS NOT NULL AND UTCDateTime <= TimeKey) FROM per_sec),
NULL,
(SELECT last(BidQuantity) FILTER(BidQuantity IS NOT NULL AND UTCDateTime <= TimeKey) FROM per_sec),
(SELECT last(AskQuantity) FILTER(AskQuantity IS NOT NULL AND UTCDateTime <= TimeKey) FROM per_sec),
NULL
FROM without_gaps
The most strange thing is that sometimes it works correct but sometime not, I mean with the same input it can produce different output and I have no idea what depends on it
I try different approaches – Window function JOIN before do it but nothing works