all groups > sql server programming > march 2007 >
You're in the

sql server programming

group:

How group a column to show side-by-side?


How group a column to show side-by-side? Ronald S. Cook
3/31/2007 3:30:10 PM
sql server programming:
This is nasty question but...

How can I take the following data from a table:

ID ItemNumber Type
1 9830302 CD
2 9830302 Cassette

And run a select statement to get me:

ID ItemNumber Type
1 9830302 CD/Cassette

Thanks,
Ron

Re: How group a column to show side-by-side? xyb
3/31/2007 5:09:53 PM
On 4=D4=C21=C8=D5, =C9=CF=CE=E75=CA=B130=B7=D6, "Ronald S. Cook" <r...@west=
[quoted text, click to view]

em,you have post the sample data and the acquire is not very clear
although :)
You can use one select statement to implement this select at this
issue but can not catch it
when you want multi rows return,i think you can use udf to do this
trick.
some code here for your problem,hope this can help you.
create function u_concatedstring(@ItemNumber numeric(??))
returns varchar(8000)
as
declare @ret varchar(8000)
select @ret =3D ''
select @ret =3D @ret + Type + '/' from tablename
where ItemNumber =3D @ItemNumber<where clause here>
go

select min(ID) ,ItemNumber ,cType =3D concatedstring(ItemNumber )
from table
group by ItemNumber
Re: How group a column to show side-by-side? Uri Dimant
4/1/2007 12:00:00 AM
Ron
----sql server 2005

Use demo

GO

CREATE TABLE DBO.Test (ID INT,ItemNumber INT, [Type] VARCHAR(20))

INSERT INTO DBO.Test VALUES (1,9830302,'CD')

INSERT INTO DBO.Test VALUES (2,9830302,'Cassette')



SELECT ItemNumber,

( SELECT m2.[Type] + ','

FROM DBO.Test m2

WHERE m2.ItemNumber = m1.ItemNumber

ORDER BY [Type]

FOR XML PATH('') ) AS [Type]

FROM DBO.Test m1

GROUP BY ItemNumber;

[quoted text, click to view]

Re: How group a column to show side-by-side? Uri Dimant
4/1/2007 12:00:00 AM
People keep back what is the version they are using , so by default it
becames SQL Server 2005 :-))





[quoted text, click to view]

Re: How group a column to show side-by-side? Steve Dassin
4/1/2007 12:34:35 AM
I knew somebody would find the right path -:)

[quoted text, click to view]

Re: How group a column to show side-by-side? Ronald S. Cook
4/1/2007 3:04:54 PM
And that's my version.. Thanks for the reply.


[quoted text, click to view]

AddThis Social Bookmark Button