sql - Using Multiple CASE Statements -
Is it possible to use multiple CASE statements to return the side-by-side (same line) results? I have read through many related posts, but it does not appear particularly addressed.
I have 2 sample tables: Transactions
Rowwide Tracks No. 1 12345 2 23456 3 34567 4 45678 5 56789 6 67890 7 78901
and document
RowID TrxNo DocNo 1 12345 1 2 12345 2 3 12345 3 4 23456 1 5 34567 1 6 34567 2 7 45678 1 8 45678 29 5678 9 1 10 56789 2 11 56789 3 Based on these tables, this query SELECT T.TrxNo, is the case when D.DocNo = 1 then D.DocNo else 0 end Doc1, as the case when D.DocNo = 2 then D.DocNo or 0 end Doc2, as the case when D.DocNo = 3 then D.DocNo or Doc3 FROM [dbo] as the 0 end. [Transaction] Join T upper part [DBO]. [Document] D on D. TRCNO = T. TxNo
Returns
Trucks No Doc 1 Dock 2 Doc 3 12345 1 0 12345 0 2 0 12345 0 0 3 23456 1 0 34567 1 0 0 34567 0 2 0 45678 1 0 45678 0 2 0 56789 1 0 56789 0 2 56789 0 0 3 67890 0 0 78901 0 0
I do it
TrxNo Doc1 Doc2 Doc3 12345 1 2 3 23456 1 0 34567 1 2 0 45678 1 2 56789 1 2 3 67890 Want to return 0 0 0 78901 0 0 0
This is my first post - I hope the information is clearly presented.
Yes but you also need to collect:
Select T.TrxNo, max (case when D.DocNo = 1 then and D.DocNo 0 end) Doc1, Max (Case WH NHD.DocNo = 2 then Doc2, then Max D. DocNo 0 ) (The case is when D.DocNo = 3 then D.DocNo or 0 end) [dbo] as Doc3 [Transaction] Join T-Outer External [dbo] [Documents] D on D. TRCNO = T. TRCNO Group by T. TCN NOO;
Comments
Post a Comment