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)
 Converting a column of data into a single string within a GroupBy clause

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2001-03-28 : 17:29:17
Stephen writes "I have a two column table that relates two databases, Consents and Wells. A single consent may relate to more than one well and a single well may relate to more than one consent. A sample of the data in this table may be:

ConsentNo, WellNo
CRC001001 M35/5423
CRC001001 M35/5424
CRC001001 M35/5425
CRC001002 L34/1254
CRC001021 M36/0987
CRC001021 M36/5424
...

We often generate letters to consent holders and include consent information, including referenced wells. For this, I need to group the records on ConsentNo and would like to append each related WellNo into a string field to produce:

ConsentNo Wells
CRC001001 M35/5423, M35/5424, M35/5425
CRC001002 L34/1254
CRC001021 M36/0987, M36/5424
...

I could use a cursor within a stored procedure but I'm interested to know how this could be done with a single SQL statement.

Running SQL Server 7.0 on Win NT 4.0"
   

- Advertisement -