Click here to Skip to main content
15,903,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is my example:
Date        Itemname      qty     price 
01/09/2016  ABC           100     1000
02/09/2016  PQR           200     500
02/09/2016  ABC           50      500
04/09/2016  XYZ           10      100
05/09/2016  ABC           50      500

I want result:
Date             ABC           PQR           XYZ
             qty  price     qty  price      qty  price

01/09/2016   100  1000      
02/09/2016   50   500       200   500       
04/09/2016   50   100                       10    100


Item names are not fix.I have an unknown number of Item names.

What I have tried:

DECLARE @cols AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX)

select @cols = STUFF((SELECT ',' + QUOTENAME(ColumnName) 
                    from yourtable
                    group by ColumnName, id
                    order by id
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

set @query = N'SELECT ' + @cols + N' from 
             (
                select value, ColumnName
                from yourtable
            ) x
            pivot 
            (
                max(value)
                for ColumnName in (' + @cols + N')
            ) p '

exec sp_executesql @query;
Posted
Updated 21-Sep-16 5:42am
v3
Comments
Karthik_Mahalingam 20-Sep-16 9:04am    
post your expected result in table format. its not clear.

Following on from Solution 1 and your comment to it...

The first thing to do is to get the results into the format you require for the fixed data you have shown in your example.

One way of doing that would be to UNPIVOT your data first and then PIVOT using a dummy column for the qty and price for each ItemName e.g.
SQL
;with CTE as
(
	SELECT * FROM 
		(SELECT [Date], qty, price, itemname
		FROM yourTable) p
		UNPIVOT
	   (value FOR what IN (qty, price)
	)AS unpvt
)
select [Date],	ABCqty,ABCprice,
		PQRqty,PQRprice,
		XYZqty,XYZprice
from
(
	select [Date],value, ItemName + what as itemwhat
	from CTE
) qry
pivot 
(
	max(value)
    for itemwhat in ([ABCqty],[PQRqty],[XYZqty],[ABCprice],[PQRprice],[XYZprice])
) as pvt
order by [Date]

Once you know that is working then use the technique you have already used in the "What I have tried" section of your question - generate the SQL dynamically based on the actual data content of the table instead of the hard-coded column names - here is a snippet of that (I'll leave the rest to you)
SQL
select @cols = STUFF((SELECT ',' + QUOTENAME(ItemName + 'qty') +  ',' + QUOTENAME(ItemName + 'price') 
    from yourtable
    group by ItemName
    FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)') ,1,1,'')


If you want it displayed precisely as you have in your expected results then you should do that in your presentation layer, not the query itself
 
Share this answer
 
Comments
Maciej Los 21-Sep-16 12:26pm    
Well explained!
Member 12749874 24-Sep-16 3:57am    
thank you very much ..its works for known number of columns.
I have list out all items name then its works.
 
Share this answer
 
Comments
Member 12749874 20-Sep-16 8:50am    
Its work only when i have fix number columns.
But I have no fix number columns.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900