TSQL - Using pivot to adjust results

%3CLINGO-SUB%20id%3D%22lingo-sub-2576300%22%20slang%3D%22en-US%22%3ETSQL%20-%20Using%20pivot%20to%20adjust%20results%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2576300%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20All%2C%20In%20my%20sql%20table%2C%20I%20have%20a%20results%20like%20this%20..%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22TheTallman_0-1626974649216.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F297694iBD3B7C0362415134%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22TheTallman_0-1626974649216.png%22%20alt%3D%22TheTallman_0-1626974649216.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Ebut%20in%20requirement%2C%20i%20need%20output%20in%20this%20way%20..%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22TheTallman_0-1626975040946.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F297698i18A99ED8B8F203E8%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22TheTallman_0-1626975040946.png%22%20alt%3D%22TheTallman_0-1626975040946.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2576300%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAnalytics%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EData%20Warehouse%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Occasional Contributor

Hi All, In my sql table, I have a results like this ..

TheTallman_0-1626974649216.png

 

but in requirement, i need output in this way ..

 

TheTallman_0-1626975040946.png

 

 

 

2 Replies

@TheTallman , I can't see anything in your "micro"-screenshots.

Please post table design as DDL, some sample data as DML statement and the expected result.

Hi,
You can use below T-SQL, if you have fix 6 columns for order sequence from 1 to 6.
SELECT [Identity No] AS medical_claims_line_Id, ISNULL([Order_Code_1],0) AS [Order_Code_1],
ISNULL([Order_Code_2],0) AS [Order_Code_2], ISNULL([Order_Code_3],0) AS [Order_Code_3],ISNULL([Order_Code_4],0) AS [Order_Code_4],ISNULL([Order_Code_5],0) AS [Order_Code_5],ISNULL([Order_Code_6],0) AS [Order_Code_6]
FROM (
SELECT [Identity No], 'Order_Code_' + CAST(Order AS VARCHAR) AS Order, [Order Code]
FROM table
) AS tbl
PIVOT ( MAX([Order Code] )
FOR Order IN (
[Order_Code_2],[Order_Code_3],[Order_Code_4],[Order_Code_5],[Order_Code_6]
)
) AS pvt