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 |
|
parabolam
Starting Member
4 Posts |
Posted - 2009-07-23 : 08:46:04
|
| hey all, i have a database which i query with SQL to show when a door is opened and closed. i want the query only to show if the door is currently open. the datatable shows data like the following:FRIDGE TIME ACTION ALARMFRIDGE3 27/06/2009 09:07 OPEN DOOR3FRIDGE3 15/07/2009 15:40 CLOSED DOOR3FRIDGE3 15/07/2009 15:40 OPEN DOOR4FRIDGE3 15/07/2009 15:41 CLOSED DOOR4FRIDGE1 24/06/2009 23:59 OPEN DOOR3FRIDGE1 01/07/2009 00:31 OPEN DOOR3FRIDGE1 03/07/2009 02:23 OPEN DOOR2FRIDGE1 03/07/2009 02:24 CLOSED DOOR2FRIDGE1 08/07/2009 05:41 OPEN DOOR3FRIDGE1 17/07/2009 18:34 CLOSED DOOR3FRIDGE2 04/07/2009 17:14 OPEN DOOR3FRIDGE2 22/07/2009 01:03 CLOSED DOOR3FRIDGE2 22/07/2009 01:48 OPEN DOOR3What i want to show is if the last action was open and to show the door as open.. so the data thrown back should only be:FRIDGE TIME ACTION ALARMFRIDGE2 22/07/2009 01:48 OPEN DOOR3Is this doable using SQL or how would i do it? |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-07-23 : 08:50:23
|
| select FRIDGE ,TIME ,ACTION ,ALARM from your_table as t1where time=(select max(time) from your_table where action='OPEN')MadhivananFailing to plan is Planning to fail |
 |
|
|
parabolam
Starting Member
4 Posts |
Posted - 2009-07-24 : 06:36:41
|
| im ot too sure where to put that code into my code. see my code below:SELECT FRIDGE AS FRIDGE ,TIME_OCCURED AS TIME ,ACTION AS ACTION ,ALARM AS ALARMFROM ALARM_HISTORYWHERE AND FRIDGE Like 'FRIDGE%' AND ALARM Like '%OPEN%' |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-07-24 : 06:51:09
|
| SELECT FRIDGE AS FRIDGE,TIME_OCCURED AS TIME,ACTION AS ACTION,ALARM AS ALARMFROM ALARM_HISTORYWHERE AND FRIDGE Like 'FRIDGE%' AND ALARM Like '%OPEN%' AND time=(SELECT max(time)FROM ALARM_HISTORYWHERE AND FRIDGE Like 'FRIDGE%' AND ALARM Like '%OPEN%')MadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|