Rows Into Columns and Grouping

后端 未结 5 872
慢半拍i
慢半拍i 2021-02-06 07:28

I have a query that looks like this:

SELECT OrganizationName, OrganizationID, ReceivableStatus, InvoiceFee
FROM v_InvoicesFreelanceOutstanding
ORDER BY Organizat         


        
5条回答
  •  臣服心动
    2021-02-06 07:55

    PIVOT sucks. It has horrible syntax and isn't a PIVOT in the pivot table sense i.e. you have to know exactly how many columns will result in advance.

    It's probably easier to do a cross-tab report.

    SELECT 
    OrganizationName,
    OrganizationID,
    SUM(CASE WHEN ReceivableStatus       = '30-60 days' THEN InvoiceFee ELSE 0 END) AS [30 - 60 Days],
    SUM(CASE WHEN ReceivableStatus       = '60-90 days' THEN InvoiceFee ELSE 0 END) AS [60 - 90 Days],
    SUM(CASE WHEN ReceivableStatus       = '90-120 days' THEN InvoiceFee ELSE 0 END) AS [90 - 120 Days]
    
    FROM
    v_InvoicesFreelanceOutstanding
    GROUP BY OrganizationID
    

提交回复
热议问题