Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

SQL – Fill down from a specific row based on criteria

I am trying to solve this in T-SQL using SSMS 2010.

I have a table below with a list of transactions for each policy. One of the rows for each policy has a ‘Y’ as being a policy that had a certain ‘issue’. I would like to create a new column to mark the row for ‘Y’ and the rows following for the same policy with a 1.

Basically for each policy, an issue occurred at some stage within the life cycle of the policy and I want to identify the PolicyVersion which is marked as a ‘Y’ and any subsequent rows for the same policy after the row marked ‘Y’.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I can’t quite figure out the correct partition

PolicyNum TransactionDate PolicyVersion IssueFoundOnThisVersion DesiredResult
A12345 2023-01-01 1 N 0
A12345 2023-06-01 2 Y 1
A12345 2023-07-01 3 N 1
A12345 2024-08-01 4 N 1
A12346 2023-02-01 1 N 0
A12346 2023-03-01 2 N 0
A12346 2023-07-01 3 N 0
A12346 2023-08-01 4 Y 1
A12346 2024-01-01 5 N 1

>Solution :

We can use SUM() as a window function here:

SELECT
    PolicyNum,
    TransactionDate,
    PolicyVersion,
    IssueFoundOnThisVersion,
    CASE WHEN SUM(CASE WHEN IssueFoundOnThisVersion = 'Y' THEN 1 ELSE 0 END)
        OVER (PARTITION BY PolicyNum ORDER BY PolicyVersion) > 0
         THEN 1 ELSE 0 END AS DesiredResult
FROM yourTable
ORDER BY
    PolicyNum,
    TransactionDate;

The SUM() logic above does a rolling sum, per policy, incrementing by one each time there is an issue with a certain policy.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading