SSRS将多行合并为一行

时间:2015-09-10 19:41:01

标签: ssrs-2012

我正在整理一份SSRS报告,该报告将显示人们欠会费的情况。

现在我每人得几行不同,具体取决于他们是否有会费,滞纳金或退款。

我需要能够将这些显示为一行而不是三行。

我的列表目前显示为:

Member ID | Name | Date | Dues | Refund | Late Fee | Inv ID | Total
123456789 | John | 2015 | $150 | ------ | -------- | INV 01 | $150
123456789 | John | 2015 | ---- | ------ | -----$10 | INV 01 |  $10
987654321 | Jane | 2015 | $150 | ------ | -------- | INV 02 | $150

我希望它显示为:

Member ID | Name | Date | Dues | Refund | Late Fee | Inv ID | Total
123456789 | John | 2015 | $150 | ------ | -----$10 | INV 01 | $160
987654321 | Jane | 2015 | $150 | ------ | -------- | INV 02 | $150

- Clarification-- 现在,判断项目是哪一种的唯一方法是通过项目的描述;即,项目名称将是会员费,或会员延迟费或会员退款,最终会产生不同的行项目。

2 个答案:

答案 0 :(得分:0)

然后您需要汇总数据:

select member_id, name, date, sum(dues) dues
     , sum(refund) refund, sum(late_fee) late_fee
     , inv_id, sum(total) total
  from your_table
 group by member_id, name, date, inv_id;

答案 1 :(得分:0)

我终于使用了以下SQL:

WITH cte_Intial AS (
    SELECT
        c.firstname
        ,c.lastname
        ,c.new_memberid
        ,s.new_invid
        ,CASE
            WHEN sd.productidname LIKE 'Late%' THEN NULL
            ELSE s.totalamount
        END AS "Dues"
        ,CASE
            WHEN sd.productidname LIKE 'Late%' THEN s.totalamount
            ELSE null
        END AS "LateFee"
        ,s.new_purchasedate
        ,s.name
        ,sd.productidname
    FROM
        Filteredcontact AS c
        INNER JOIN FilteredSalesOrder AS s ON c.new_memberid = s.new_memberid
        INNER JOIN FilteredSalesOrderDetail AS sd ON s.salesorderid = sd.salesorderid
    WHERE
        c.new_divisioncode = 'A' AND sd.productidname LIKE '%Dues%' AND
        DATEPART(YEAR, CAST(s.new_purchasedate AS DATE)) = 2015
)
SELECT
    firstname
    ,lastname
    ,new_memberid
    ,ISNULL(MAX(Invoice), '')/* + ' ' + ISNULL(MAX(Late), '')*/ as Invoice
    ,MAX(Dues) as Dues
    ,MAX(LateFee) as LateFee
    ,new_purchasedate
    ,name
FROM 
    cte_Intial
GROUP BY
    firstname
    ,lastname
    ,new_memberid
    ,new_purchasedate
    ,name