Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Select Statement 1

Status
Not open for further replies.

stroyer74

IS-IT--Management
Jan 22, 2003
70
US
Looking for help on a SQL select statement. I'm using MS SQL 2000.

Table1 includes
field 1 as int: ID
field 2 as int: DrawingID
field 3 as varchar: AffectedModel

The same DrawingID may be listed multiple times with a different AffectedModel listed each time. I would like to do a select statement that retrieves distinct DrawingIDs and combines the AffectedModels into one string.

For example, the table data may include:

ID DrawingID AffectedModel
---------------------------------
1 1234 Model1
2 1234 Model2


The select statement would return

DrawingID AffectedModel
----------------------------
1234 Model1, Model2



Part two of the question is that the end user of this would like it to determine how many AffectedModels are involved for each DrawingID. If the count is greater than 2, return "See Index" instead of the actual models.

For example, the table data may include:

ID DrawingID AffectedModel
---------------------------------
1 1234 Model1
2 1234 Model2
3 4321 Model9
4 4321 Model8
5 4321 Model7


The select statement would return

DrawingID AffectedModel
----------------------------
1234 Model1, Model2
4321 See Index


Any ideas? Thanks in advance,
Sheldon
 
1. use a function to concatenate
see

2.
select id, case when count(*) > 2 then 'see index' else dbo.csvtbl(id) end
from tbl
group by id


======================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
 
Many thanks nigelrivett! That helped explain the concept I was trying to figure out. From your example, it was pretty easy to move it over to my database and make it work with my live data.

Thanks again,
Sheldon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top