1

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.

enter image description here

anyone have the idea ? thanks.

0

1 Answer 1

2

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);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks ! I have now the idea. But can I ask something why it's returning string like this "----" below of the name of the columns ? I'm unfamiliar with that.
That is how the application you are using for querying works. SQL Server Management Studio does this when you put the results in text mode rather than in a grid.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.