My goal is to get the customer balance report but I am stuck in my query where I can get the doc type of a certain column. I have my query
SELECT
ROW_NUMBER() OVER( ORDER BY DR.id ) AS 'NO.' ,
CONVERT(VARCHAR(13), CAST(DR.doc_date AS DATE), 100) AS 'DOC DATE',
SR.net_total,
SR.dell_col_charge,
SR.CDW,
sp.amount
FROM
[dbo].[doc_customer] DC
LEFT JOIN [dbo].[doc_rent] DR ON DR.doc_sourced_customer_id = DC.id
LEFT JOIN [dbo].[slip_rent] SR ON SR.doc_sourced_doc_rent_id = DR.id
LEFT JOIN [dbo].[slip_rent_payment] SP ON SP.doc_sourced_rent_id = DR.id
as u can see in my query it returning the net total, charge, cdw, and amount but my goal is to show them in 1 column in a different rows. u can see on the image I attached that's my goal of my query.
anyone have the idea ? thanks.
I like to use apply
for unpivoting:
SELECT ROW_NUMBER() OVER( ORDER BY DR.id ) AS [NO.] ,
CONVERT(VARCHAR(13), CAST(DR.doc_date AS DATE), 100) AS [DOC DATE],
v.*
FROM [dbo].[doc_customer] DC LEFT JOIN
[dbo].[doc_rent] DR
ON DR.doc_sourced_customer_id = DC.id LEFT JOIN
[dbo].[slip_rent] SR
ON SR.doc_sourced_doc_rent_id = DR.id LEFT JOIN
[dbo].[slip_rent_payment] SP
ON SP.doc_sourced_rent_id = DR.id OUTER APPLY
(VALUES ('net_total', SR.net_total),
('dell_col_charge', SR.dell_col_charge),
('CDW', SR.CDW),
('amount', sp.amount)
) v(which, val);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.