Help with sql statment
Hello, I thought I had this licked but am stuck on the last bit.
I am using asp.net 2.0 with a sql database using sql server 2005 and have a table with 4 columns and am trying to make a filter for a pneumatic tube delivery system that is used where I work.
The table looks like this:
ID(key) TubeDestination TimeSent ContainsItems
1-------- 88 -------------20:00 -----yes
2-------- 88 -------------21:00 -----no
3-------- 88 -------------22:00 -----yes
4------- 100 -------------20:00 -----yes
5------- 100 -------------21:00 -----yes
6--------100 -------------22:00 -----yes
7--------150 -------------21:00 -----yes
8--------150--------------23:00-----yes
I'd like to display the most recent tube destination and time sent for each tube station but only if it has been more than one hour since a tube has been sent to a location
For the above table if it was currently 23:30 I would hope for the below return:
TubeDestination TimeSent
--88------------- 22:00
--100------------ 22:00
I'm thought the following was right but then realized it will show the most recent tube destination and time that is greater than one hour but will show a that destination that was sent more than an hour ago even if a more recent tube was sent within the past hour.
SELECT TubeDestination, MAX(TimeSent) AS TimeSent
FROM Table1
WHERE TimeSent < dateadd(hh,-1,getdate())
GROUP BY TubeDestination
as an example...the 88 and 100 are right but the 150 is showing up and I don't want it to at all since there was a more recent tube sent at 2300.
TubeDestination TimeSent
--88------------- 22:00
--100------------ 22:00
--150-------------21:00
Every nested query I use seems to end up using two columns which is a no-no. If anyone has some ideas or a solution I'd love the help. Thanks Dan
|