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

how to find the start dates from the Transaction history - response (1) by sam_dhse

$
0
0

I am sure you have figured out a solution by now.If not , something of this sort should work.
 
SELECT
CUSTOMER,
MIN(start_date) AS start_d,
MAX(end_date) AS end_d,
RANK() OVER(PARTITION BY CUSTOMER ORDER BY end_d DESC) AS rnk QUALIFY rnk=1
FROM
    (
    SELECT
    CUSTOMER,
    start_date,
    end_date,
    SUM(CASE WHEN start_date - prev_end_date > 1 THEN 1 ELSE 0 END) OVER
    (PARTITION BY CUSTOMER
    ORDER BY start_date
    ROWS UNBOUNDED PRECEDING) AS grp
    FROM
        (
        SELECT
        CUSTOMER,
        start_date,
        end_date,
        MIN(end_date) OVER
        (PARTITION BY
        ORDER BY end_date
        ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING) AS prev_end_date
        FROM table
        ) dt
    ) dt
GROUP BY CUSTOMER, grp


Viewing all articles
Browse latest Browse all 27759

Trending Articles



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