I have a query that looks like this:
SELECT OrganizationName, OrganizationID, ReceivableStatus, InvoiceFee
FROM v_InvoicesFreelanceOutstanding
ORDER BY OrganizationID
The data from this may look like this:
OrganizationName OrganizationID ReceivableStatus InvoiceFee
-------------------------------------------------- ---------------------------
Company A 139 60-90 days 672.00
Company A 139 60-90 days 1800.00
Company A 139 over 90 days 1440.00
Company B 264 Current 3559.38
Company B 264 60-90 days 3785.50
Company C 271 60-90 days 446.25
Company C 271 over 90 days 637.50
Company C 271 over 90 days 1126.25
What I want to ultimately display looks something like this (for the data above):
Company Current 30-60 days 60-90 days over 90 days Total
-------------------------------------------------- ---------------------------
Company A 0 0 2472.00 0 2472.00
Company B 3559.38 0 3785.50 0 7344.88
Company C 0 0 446.25 1763.75 2210.00
My SQL-fu is not enough to pass me this:
SELECT
MAX(OrganizationName) as OrganizationName,
OrganizationID,
ReceivableStatus,
SUM(InvoiceFee) as TotalDue
FROM v_InvoicesFreelanceOutstanding
GROUP BY OrganizationID, ReceivableStatus
Which shows something like this (again, from the above data):
OrganizationName OrganizationID ReceivableStatus TotalDue
-------------------------------------------------- ---------------------------
Company A 139 60-90 days 2472.00
Company A 139 over 90 days 1440.00
Company B 264 Current 3559.38
Company B 264 60-90 days 3785.50
Company C 271 60-90 days 446.25
Company C 271 over 90 days 1763.75
? .
, , 2- (Current, 30-60 days, 60-90 days, over 90 days), , ReceivableStatus.
: , . PIVOT, , .