简体   繁体   中英

SQL- SSRS Report

I am building a SSRS report and the data looks like as shown in the attached image.

I need to show the card number separately like primary and secondary based on the flag into new columns along with effective dates. If a customer has more than one card, i want to show the latest card information.

How can i modify my sql script to get the desired output.

Thank you. 在此处输入图像描述

You can use conditional aggregation. I think this is the logic you want:

select cif_id, staff_id, package, segment,
       max(case when seqnum = 1 and primary_card_flag = 'Y' then card_number end),
       max(case when seqnum = 1  and primary_card_flag = 'N' then card_number end),
       max(case when seqnum = 1 and primary_card_flag = 'Y' then valid_from_date end),
       max(case when seqnum = 1 and primary_card_flag = 'N' then valid_from_date end)
from (select t.*,
             row_number() over (partition by cif_id, staff_id, package, segment, primary_card_flag
                                order by valid_from_date desc
                               ) as seqnum
      from t
     ) t
group by cif_id, staff_id, package, segment order;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM