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.
Author |
Topic |
esthera
Master Smack Fu Yak Hacker
1410 Posts |
Posted - 2007-10-22 : 10:13:24
|
i have a table calledparticipantswith a participantidnameofprogramthen i have a table called placements with a placementidparticipantidstatusthere can be a number of placements per participanthow can i query the total number of placements per nameofprogram taking the nameofprogram from participant and looking at the latest record for that participant in placements and if it is a status of 1 or 2 then count it - if the latest record has any other status then don't count it. |
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2007-10-22 : 10:40:09
|
How do you determine the latest record?select nameofprogram, count(distinct pl.participantid)from participants pleft join placements plon p.participantid = pl.participantidand pl.status in ((1,2)group by nameofprogrammaybeselect nameofprogram, count(distinct pl.participantid)from participants pleft join (select participantid, placementid = max(placementid) from placements group by participantid) plmaxon p.participantid = pl.participantidleft join placements pon plmax.participantid = pl.participantidand plmax.placementid = pl.placementidand pl.status in ((1,2)group by nameofprogram==========================================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. |
 |
|
|
|
|