Query group by data in SQL Server - cursor?

Hello Experts,
Need this.
Data(changed data)
Keycol data
1      O1
1      O2
1      O3
2      O4
3      O5
3      O5
7      O2
7      O1
Output I need below

Keycol data       LineNo
1      O1,O2,O3    1
2      O4          2
3      O5          3
7      O1,O2       4
possible in sql query? I need to generate  sequential number for a new column.
DO i need a loop or cursor forthis?
Thank you
aakshi jainAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Pawan KumarDatabase ExpertCommented:
Loop is not required for this. Let me just write a query for you.
Pawan KumarDatabase ExpertCommented:
Hi,
Please try this-

--

CREATE TABLE Teststr
(
	Keycol INT , data VARCHAR(100)
)
GO

INSERT INTO Teststr VALUES
(1      ,'O1'),
(1      ,'O2'),
(1      ,'O3'),
(2      ,'O4'),
(3      ,'O5'),
(3      ,'O5'),
(7      ,'O2'),
(7      ,'O1')
GO

SELECT * , ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) [LineNo] FROM 
(
	SELECT DISTINCT Keycol  , STUFF 
					((
					SELECT DISTINCT ', ' +  data
					FROM Teststr b
					WHERE ( a.Keycol = b.Keycol )				
					FOR XML PATH('')
					) ,1,2,'') 
					AS Data 
	FROM Teststr a
)k

--

Open in new window


OUTPUT

/*------------------------
OUTPUT
------------------------*/
Keycol      Data                                                                                                                                                                                                                                                             LineNo
----------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- --------------------
1           O1, O2, O3                                                                                                                                                                                                                                                       1
2           O4                                                                                                                                                                                                                                                               2
3           O5                                                                                                                                                                                                                                                               3
7           O1, O2                                                                                                                                                                                                                                                           4

(4 row(s) affected)

Open in new window


Hope it helps!

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
aakshi jainAuthor Commented:
Thank you
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.