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 |
supe
Starting Member
4 Posts |
Posted - 2007-12-18 : 10:04:43
|
I have a table of outboundcalls, I need a query to return the ID's of the last three calls for each OutboundNoID on the same row, there could be more or less then six.The closest I've gotton is:select obc1.ID,obc2.ID, obc3.IDfrom Outboundcalls obc1 left outer join Outboundcalls obc2 onobc1.OutboundNoID = obc2.OutboundNoID and obc1.endtime > obc2.endtimeleft outer join Outboundcalls obc3 on obc2.OutboundNoID = obc3.OutboundNoID and obc2.endtime > obc3.endtimeorder by obc1.ID descwhich returns:6574 4810 NULL6574 5398 48106573 5397 48076573 4807 NULL6572 4796 NULL6572 5393 47966571 4465 28126571 4465 3639The first and second rows show the problem I'm having. The second row is the only result I want for ID 6574 although the first row does satisfy the query, since 4810 is in fact before 6574.I've searched up down, far and close, PLEASE HELP! |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2007-12-18 : 10:10:59
|
Cross post:-[url]http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=94375[/url] |
 |
|
supe
Starting Member
4 Posts |
Posted - 2007-12-18 : 10:16:49
|
Thankyou and sorry for posting twice but this is a sql 2000 db, which doesnt support row_number. |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2007-12-18 : 10:51:41
|
Ok do like this in 2000CREATE TABLE #temp(RowNo int IDENTITY(1,1),ID int,OutboundNoID int,RowCount int,Seq int)INSERT INTO #temp(ID,OutboundNoID)SELECT ID,OutboundNoIDFROM Outboundcalls ORDER BY OutboundNoID,ID DESC UPDATE tSET t.RowCount=tmp.RowCountFROM #temp tINNER JOIN(SELECT OutboundNoID,COUNT(ID) AS 'RowCount'FROM Outboundcalls GROUP BY OutboundNoID)tmpON tmp.OutboundNoID=t.OutboundNoIDUPDATE tSET t.Seq=t.RowNo-Dec.decrFROM #temp tINNER JOIN ( SELECT t1.OutboundNoID,t1.RowNo,SUM(tmp.RowCount) AS 'decr' FROM #temp t1 INNER JOIN ( SELECT OutboundNoID,RowCount AS 'RowCount' FROM temp GROUP BY OutboundNoID,RowCount )tmp ON tmp.OutboundNoID>t1.OutboundNoID GROUP BY t1.OutboundNoID,t1.RowNo)DecON t.OutboundNoID=Dec.OutboundNoIDAND t.RowNo=Dec.RowNoSELECT t1.ID,t2.ID,t3.ID FROM #temp t1INNER JOIN #temp t2ON t1.OutboundNoID =t2.OutboundNoID AND t2.Seq=2INNER JOIN #temp t3ON t1.OutboundNoID =t3.OutboundNoID AND t3.Seq=3WHERE t1.Seq=1 |
 |
|
supe
Starting Member
4 Posts |
Posted - 2007-12-19 : 13:55:27
|
I can see what you're trying to do, but your seq column in your second update isnt doing what you want it to. The seq column is unique for every row and works more like a row count starting at negative total calls to positive total calls. |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-12-19 : 15:21:55
|
Some sample data would be great!We know your expected output. E 12°55'05.25"N 56°04'39.16" |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-12-19 : 15:31:01
|
Or try this-- Initialize staging areaDECLARE @Stage TABLE (RowID INT IDENTITY(1, 1), OutboundNoID INT, ID INT)INSERT @Stage ( OutboundNoID, ID )SELECT OutboundNoID, IDFROM OutboundCallsORDER BY EndTime DESC-- Show the expected outputSELECT q.OutboundNoID, MAX(CASE s.RowID - q.MinRowID WHEN 0 THEN s.ID ELSE 0 END), MAX(CASE s.RowID - q.MinRowID WHEN 1 THEN s.ID ELSE 0 END), MAX(CASE s.RowID - q.MinRowID WHEN 2 THEN s.ID ELSE 0 END)FROM ( SELECT OutboundNoID, MIN(RowID) AS MinRowID, MIN(RowID) + 2 AS MaxRowID FROM @Stage GROUP BY OutboundNoID HAVING COUNT(*) >= 3 ) AS qINNER JOIN @Stage AS s ON s.OutboundNoID = q.OutboundNoIDWHERE s.RowID BETWEEN q.MinRowID AND q.MaxRowID AND s.RowID - q.MinRowID BETWEEN 0 AND 2 E 12°55'05.25"N 56°04'39.16" |
 |
|
|
|
|
|
|