Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 SQL QUERY

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2004-09-27 : 09:03:51
George writes "How can I make this Query?

Heres the Original Table
Field1 Field2
a b
a c
b a
b b
b c
c a

query result would be:

Field1 Field2
a a & c
b a & b & c
c a"

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2004-09-27 : 09:47:49
http://www.sqlteam.com/item.asp?ItemID=11021

Corey
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2004-09-27 : 10:01:22
Yeah, that's better....

BUT...here's the foundation from the original...just cut and paste into Query Analyzer to see it work...


USE Northwind
GO

SET NOCOUNT ON
CREATE TABLE myTable99(Col1 char(1), Col2 char(1))
GO

INSERT INTO myTable99(Col1,Col2)
SELECT 'a', 'b' UNION ALL
SELECT 'a', 'c' UNION ALL
SELECT 'b', 'a' UNION ALL
SELECT 'b', 'b' UNION ALL
SELECT 'b', 'c' UNION ALL
SELECT 'c', 'a'
GO

DECLARE @Col1 char(1), @MIN_Col1 char(1), @Col2 char(1), @Col2List varchar(8000)
DECLARE @myTable99 table (Col1 char(1),Col2List char(8000))

SELECT @Col1 = MAX(Col1), @MIN_Col1 = MIN(Col1) FROM myTable99

WHILE @Col1 >= @MIN_Col1
BEGIN
SELECT @Col2List = COALESCE(@Col2List + ', ', '') + Col2
FROM myTable99 l
WHERE l.Col1 = @Col1

INSERT INTO @myTable99(Col1,Col2List)
SELECT @Col1, @Col2List

SELECT @Col1 = MAX(Col1), @Col2List = Null
FROM myTable99
WHERE Col1 < @Col1
END

SELECT * FROM @myTable99 ORDER BY Col1
GO

SET NOCOUNT OFF
DROP TABLE myTable99
GO





Brett

8-)
Go to Top of Page
   

- Advertisement -