0

ProductID | id | TaxDescription|
2009001 | 1 | Excise
2009001 | 2 | Edu Cess on Exc
2009001 | 3 | S.HS.EduCess-Ex
2009002 | 1 | Excise
2009002 | 2 | Edu Cess on Exc
2009003 | 1 | Excise
2009003 | 2 | Edu Cess on Exc
2009003 | 3 | S.HS.EduCess-Ex
2009003 | 4 | VAT


I want Output Should
-----------------------
2009001 |1,2,3| Excise,Edu Cess on Exc,S.HS.EduCess-Ex,
2009002 |1,2| Excise,Edu Cess on Exc,S.HS.EduCess-Ex,VAT,
2009003 |1,2,3,4| Excise,Edu Cess on Exc,S.HS.EduCess-Ex,VAT,Octori

what will be the sql query to get above output in sql server 2005
please Solve my thread I am in very much need
The above output is possible or not

5
Contributors
6
Replies
7
Views
6 Years
Discussion Span
Last Post by crishjeny
0

Create the functions:

create function idToRow(@ProductID as int)
	returns Varchar(500)
as begin
	
	declare @temp varchar(100)
	set @temp=''
	
	select @temp+=CAST(id as varchar)+',' from yourTableName where ProductID=@ProductID
	
	return @temp
	
end

and TaxDescriptionToRow(same like idToRow), then call it like:

select ProductID,idToRow(ProductID),TaxDescriptionToRow(ProductID) from @table group by ProductID
0

SELECT DISTINCT STUFF( (SELECT ', ' + [category] from [tblCategory] FOR XML PATH('')),1,1,'') as [category] FROM [tblCategory]
MSSQL Merge multiple rows in single row with string
store the row values into a variable using COALESCE in SQL.

DECLARE @str VARCHAR(100)
SELECT @str = COALESCE(@str + ', ', '') + [category]
FROM [tblCategory]
SELECT category= @str

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.