简体   繁体   English

SQL-SSRS 报告

[英]SQL- SSRS Report

I am building a SSRS report and the data looks like as shown in the attached image.我正在构建一个 SSRS 报告,数据如下图所示。

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.如何修改我的 sql 脚本以获得所需的 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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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