Quantcast
Channel: Teradata Forums - All forums
Viewing all articles
Browse latest Browse all 27759

Find the two pre/post/both the Dates - response (2) by dnoeth

$
0
0
Nice puzzle :-) This should work, there are probably other solutions, but this one needs only a single STAT fubnction step: SELECT maturity, timeperiod, COUNT(CASE WHEN timeperiod <= maturity THEN 1 END) OVER (PARTITION BY maturity) AS cnt1, COUNT(CASE WHEN timeperiod > maturity THEN 1 END) OVER (PARTITION BY maturity) AS cnt2, ROW_NUMBER() OVER (PARTITION BY maturity ORDER BY timeperiod) AS rn, rn - CASE WHEN cnt1 > 0 AND cnt2 > 0 THEN cnt1 -1 WHEN cnt1 > 0 THEN cnt1-2 ELSE cnt1 END AS x FROM tab QUALIFY x IN (1,2) Dieter

Viewing all articles
Browse latest Browse all 27759

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>