2015-09-10 110 views
0

我正在編制一份SSRS報告,顯示人們因會費而欠的債務。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-- 眼下只有這樣,才能知道哪些項目是,是由該項目的描述;即該項目名稱將是會員費,或會員遲交費或會員退款,這最終產生不同的行項目。

回答

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 
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;