Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
SQL
Attribute_SubID  AttributeID  Attribute_SubID
3                      2                 xyz
4                      2                 abc
3                      2                 def


Above is my table structure and i want to show result like belwo
SQL
3     4
xyz   abc
def   --
Posted
Updated 27-Jun-14 1:32am
v2

Hi,

I would suggest you to use a dynamic PIVOT with partitioning to transpose data. Here's how to do that:
SQL
CREATE TABLE #DataTable (Attribute_SubID INT, AttributeID INT, Attribute_Sub NVARCHAR(50));

INSERT INTO #DataTable (Attribute_SubID, AttributeID, Attribute_Sub) VALUES (3, 2, 'xyz'), (4, 2, 'abc'), (3, 2, 'def');


DECLARE @ColumnsTable TABLE (Col VARCHAR(10));

INSERT INTO @ColumnsTable (Col)
SELECT DISTINCT Attribute_SubID FROM #DataTable;

DECLARE @SQL VARCHAR(MAX);

SET @SQL = ';WITH Data
AS
(
	SELECT Attribute_SubID, Attribute_Sub, ROW_NUMBER() OVER (PARTITION BY Attribute_SubID
																ORDER BY Attribute_SubID) AS RowNumber
	FROM #DataTable
)
SELECT ' + (SELECT STUFF((SELECT ', ISNULL([' + Col + '], ''--'') AS [' + Col + ']'
                                   FROM @ColumnsTable
                                   FOR XML PATH('')), 1, 2, '')) +
'
FROM Data
PIVOT
(
	MAX(Attribute_Sub)
	FOR Attribute_SubID IN
	(' + (SELECT STUFF((SELECT ', [' + Col + ']'
                                   FROM @ColumnsTable
                                   FOR XML PATH('')), 1, 2, '')) + ')
) AS pvt;';

--SELECT @SQL;

EXEC(@SQL);


DROP TABLE #DataTable;

Result:
XML
3	4
xyz	abc
def	--
 
Share this answer
 
v2
Comments
Aadyakumar 28-Jun-14 0:51am    
It is very much helpful solution , i also faced same situation where dynamic number of columns needed .I had solved it my self and it is some thing like this
Andrius Leonavicius 28-Jun-14 1:00am    
I'm glad to hear this. :)
Aadyakumar 28-Jun-14 2:20am    
Please check this post and help me if you can .

http://www.codeproject.com/Questions/790861/Csharp-Program-with-Sql-Stored-Procedure-with-pivo
Andrius Leonavicius 28-Jun-14 11:25am    
Sorry, but without code it's not possible to tell what is wrong...
Aadyakumar 30-Jun-14 8:25am    
revisit the link i had put the code there..
Try this :

SQL
create table ##tbl_test1(Attribute_SubID bigint,AttributeID bigint,Attribute_Sub nvarchar(max))

insert into ##tbl_test1(Attribute_SubID,AttributeID,Attribute_Sub)  values(3,2,'xyz')
insert into ##tbl_test1(Attribute_SubID,AttributeID,Attribute_Sub)  values(4,2,'abc')
insert into ##tbl_test1(Attribute_SubID,AttributeID,Attribute_Sub)  values(3,2,'def')
select case when Attribute_SubID=3 then Attribute_Sub end ,case when Attribute_SubID=4 then Attribute_Sub end From ##tbl_test1
 
Share this answer
 

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