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 |
|
AskSQLTeam
Ask SQLTeam Question
0 Posts |
Posted - 2003-09-15 : 07:22:57
|
| Anders writes "Please help an almost complete SQL newbieI need only one thing. When I do this:select service.per_id, service.res_priority, res_activation_time, loaner.name from service,loanerwhere service.marc_id=116752 AND res_type is not null and loaner.per_id=service.per_id order by 2,3I get a perfect list sorted in the correct manner, of customers queueing for books in a library system. Something like:Per_id / Res_priority Res_activation_time etc.745 / 0 389 / 5102 / 5999 / 9I would like to extract on which row any of the Per_id's appear, based on this per_id, after being sorted and all. This to be able to tell any customer "You are nr X out of Y customers waiting for this book." Where X equals the number of the row for a specific per_id, and Y is the total of all rows. (I can accomplish the last manouver with a count(*). )Perhaps the answer is already in your db, but in this case I just didn't get it." |
|
|
Stoad
Freaky Yak Linguist
1983 Posts |
Posted - 2003-09-15 : 08:28:40
|
| Something like this:select s.per_id, s.res_priority, s.res_activation_time, l.name,(select count(*) from service ss wheress.marc_id=s.mark_id andss.res_type is not null andss.res_priority<=s.res_priority andss.res_activation_time<=s.res_activation_time) as Nofrom service s inner join loaner l on s.per_id=l.per_idwhere s.marc_id=116752 AND s.res_type is not nullorder by 2,3 |
 |
|
|
dsdeming
479 Posts |
Posted - 2003-09-15 : 08:34:17
|
| Try something like this:select identity(int, 1, 1) as id, service.per_id, service.res_priority, res_activation_time, loaner.name into #tempfrom service,loanerwhere service.marc_id=116752 AND res_type is not null and loaner.per_id=service.per_id order by 2,3select @iCount = @@ROWCOUNTselect @iPosition = id from #temp where per_id = @PersonToQueryForprint 'You are number ' + cast( @iPosition as varchar( 12 )) + ' of ' + cast( @iCount as varchar( 12 )) + ' customers waiting for this book.'Dennis |
 |
|
|
|
|
|
|
|