Click here to Skip to main content
15,909,440 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Below is the requirement:
itemid	Att Name1 Att Value 1	Att Name2	Att value 2  Att Name 3 Att Val 3
4	XYZ	   A	        Color	      White           PQR       aaaaa
Like these there are 100 plus Att Name and Att Value in seperate columns

Below is the output requied:
Itemid  XYZ   Color   pqr
1       A     white   aaaa

Please help

What I have tried:

Pivot can serve the purpose i thing although not sure
Posted
Updated 11-Jun-19 0:29am
v2

Based on your expected output (where the values from the row are the column names) you do need to use PIVOT. However, because of the table schema you will need to use either UNPIVOT or UNION to get the data into the form you need first. That result will need to be either in a sub-query, a temporary table or a common table expression.

Because this is homework I will not give you the full code here, but here are the steps you need to take …

1. Get the data into the format
itemid	Attrib	AttValue
4	XYZ	A
4	Color	White
4	PQR	aaaaa
I would use UNPIVOT[^] to do this. There is a worked example here - SQL Banana: SQL UNPIVOT on Multiple Columns[^]. Take careful note of the WHERE clause in the example.

If you don't use UNPIVOT then you will need to select each attribute name one by one and UNION the results.

2. You can put the results into a Sub-Query[^], a temporary table[^], a table variable[^] or a Common Table Expression (CTE)[^]. My personal preference is a CTE but a temporary table will do.

3. You then need to PIVOT (See CP article Simple Way To Use Pivot In SQL Query[^] ) that data to get the result you want.

Hint: Pivots always needs some sort of cumulative function in the PIVOT clause. In this case I would use MAX().

Give it a go and if you are still having problems, respond with the code you have tried and I will try to guide you further.
 
Share this answer
 
Comments
Maciej Los 17-Jun-19 8:20am    
5ed!
chints786 19-Jun-19 0:08am    
Thanks CHiLL60 and apologies for the late response.

I was able to get the result using unionall

SELECT TOP 1 'itemid', [Att Name 1], [Att Name 2], [Att Name 3]
from TEST
UNION ALL
SELECT [itemid], [Att Value 1], [Att Value 2], [Att Value 3]
from TEST

Thanks a lot for your help!!!
CHill60 19-Jun-19 7:30am    
That is completely the wrong way to go about this. You don't select "headers" as a data row - that only looks ok if you paste the results into Excel without the column headers. Given that a lot of other students have been posting similar questions and the fact that you mentioned PIVOT yourself should give you a clue that you will fail this assignment with what you have done.
chints786 22-Jul-19 13:10pm    
apology for the delay. yes you are right. but after getting the desired result i had copied to excel and then I had a macro created which transposed the data as per my need.
You can use the SQL CASE statement, see: SQL CASE Statement[^]
And: CASE (Transact-SQL) - SQL Server | Microsoft Docs[^]
For an example with SUBSTRING() see: sql - Can we use a CASE...WHEN inside SUBSTRING? - Stack Overflow[^]
You can also use CHARINDEX(), see: SQL Server CHARINDEX() Function[^]
 
Share this answer
 
v4
Comments
chints786 11-Jun-19 5:39am    
how is it possible with case?
RickZeeland 11-Jun-19 5:45am    
You will also need to use SUBSTRING(), I will update the solution ...
chints786 11-Jun-19 6:01am    
Can you write the code and give?
RickZeeland 11-Jun-19 6:37am    
The solution with UNPIVOT by CHill is probably more elegant, you should try that first.

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