| Author |
Topic |
|
arusu
Yak Posting Veteran
60 Posts |
Posted - 2010-06-28 : 12:31:45
|
| I am trying to do this query:SELECT sum(datediff(s, min(le.event_time), max(le.event_time))FROM log_event le INNER JOIN equipment e ON le.equipment_id = e.equipment_idbasically what i am trying to accomplish is this. I have a table that has a bunch of dates and times in which an equipment performs something. Each equipment has a connect time and a disconnect time and i want to see how long all of the equipment have connected.example:equip 1 - connect; event_time = '6/15/2009 12:50'equip 1 - random message; event_time = '6/15/2009 12:52'equip 1 - disconnect; event_time = '6/15/2009 12:55'total connect time = 5 minutesequip 2 - connect; event_time = '6/16/2009 12:45'equip 2 - disconnect; event_time = '6/16/2009 12:55'total connect time = 10 minutesso i want my query to return 15 minutes because thats the total connect time if you add up both equipmentthe problem is that i get this message:Cannot perform an aggregate function on an expression containing an aggregate or a subquery.i understand completely what this message means but i don't see a way around this. Any suggestions? |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
arusu
Yak Posting Veteran
60 Posts |
Posted - 2010-06-28 : 12:57:27
|
| ok so i have one table log_event with columnslog_event_id; event_time; equipment_idanother table equipment with columnsequipment_id; name |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2010-06-28 : 13:05:24
|
Something like this should work:SELECT SUM(datediff(MINUTE, MinTime, MaxTime)) AS TotalMinutesFROM( SELECT min(le.event_time) AS MinTime, max(le.event_time) AS MaxTime FROM log_event GROUP BY equipment_id) unless you need to have the MIN a Connect event and the MAX be a disconnect event. Then that changes thing s a little. |
 |
|
|
arusu
Yak Posting Veteran
60 Posts |
Posted - 2010-06-28 : 13:12:42
|
| With that, i get "Incorrect syntax near ')'."but i think that it may work otherwise. I will look into it too but just in case i don't see it, i wanted to reply |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
arusu
Yak Posting Veteran
60 Posts |
Posted - 2010-06-28 : 13:34:32
|
| sorry, im kind of embarrassed to ask but i don't understand what you mean by that |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2010-06-28 : 13:38:01
|
| post some sample dataor answer thiswhen do you record rows in the even_logWhen a machine starts...you eneter a rowwhen a machine stops you eneter a rowWhen a machine starts...you eneter a rowwhen a machine stops you eneter a rowWhen a machine starts...you eneter a rowwhen a machine stops you eneter a rowWhen a machine starts...you eneter a rowwhen a machine stops you eneter a rowis my guessBrett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxAdd yourself!http://www.frappr.com/sqlteam |
 |
|
|
arusu
Yak Posting Veteran
60 Posts |
Posted - 2010-06-28 : 13:52:17
|
| yes you are correct in that logic just one add on that doesnt even matter. row is added for a connect, for any activity and for a disconnect. So i test this and it does work: SELECT min(event_time) AS MinTime, max(event_time) AS MaxTime FROM log_event group by equipment_idbut when i put it all together:SELECT SUM(datediff(s, MinTime, MaxTime))FROM( SELECT min(event_time) AS MinTime, max(event_time) AS MaxTime FROM log_event group by equipment_id)i get the following error:Msg 102, Level 15, State 1, Line 12Incorrect syntax near ')' |
 |
|
|
Sachin.Nand
2937 Posts |
Posted - 2010-06-28 : 13:55:35
|
| [code]SELECTSUM(datediff(s, MinTime, MaxTime))FROM(SELECTmin(event_time) AS MinTime,max(event_time) AS MaxTimeFROMlog_eventgroup byequipment_id)T[/code]Limitations live only in our minds. But if we use our imaginations, our possibilities become limitless. PBUH |
 |
|
|
arusu
Yak Posting Veteran
60 Posts |
Posted - 2010-06-28 : 13:58:57
|
| It works!thank you so muchi was wondering though, what is that T for? |
 |
|
|
Sachin.Nand
2937 Posts |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2010-06-28 : 15:45:58
|
whatdya mean it works?/*equip 1 - connect; event_time = '6/15/2009 12:50'equip 1 - random message; event_time = '6/15/2009 12:52'equip 1 - disconnect; event_time = '6/15/2009 12:55'total connect time = 5 minutesequip 1 - connect; event_time = '6/15/2009 18:50'equip 1 - random message; event_time = '6/15/2009 18:52'equip 1 - disconnect; event_time = '6/15/2009 18:55'total connect time = 5 minutesequip 2 - connect; event_time = '6/16/2009 12:45'equip 2 - disconnect; event_time = '6/16/2009 12:55'total connect time = 10 minutesequip 2 - connect; event_time = '6/16/2009 20:45'equip 2 - disconnect; event_time = '6/16/2009 20:55'total connect time = 10 minutesWhat you asked: so i want my query to return 15 minutes because thats the total connect time if you add up both equipmentWhat you really asked: so i want the total connect time if you add up both equipment*/CREATE TABLE #myTable99 (log_event_id int IDENTITY(1,1), event_time datetime, equipment_id int, event_message varchar(2000))GOINSERT INTO #myTable99(event_time, equipment_id, event_message)SELECT '6/15/2009 12:50', 1, 'Start' UNION ALLSELECT '6/15/2009 12:52', 1, 'Random' UNION ALLSELECT '6/15/2009 12:55', 1, 'Stop' UNION ALLSELECT '6/15/2009 18:50', 1, 'Start' UNION ALLSELECT '6/15/2009 18:52', 1, 'Random' UNION ALLSELECT '6/15/2009 18:55', 1, 'Stop' UNION ALLSELECT '6/16/2009 12:45', 2, 'Start' UNION ALLSELECT '6/16/2009 12:55', 2, 'Stop' UNION ALLSELECT '6/16/2010 20:45', 2, 'Start' UNION ALLSELECT '6/16/2010 20:55', 2, 'Stop'GOSELECT * FROM #myTable99GOSELECTSUM(datediff(mi, MinTime, MaxTime))FROM(SELECTmin(event_time) AS MinTime,max(event_time) AS MaxTimeFROM#myTable99group byequipment_id)TDROP TABLE #myTable99GO Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxAdd yourself!http://www.frappr.com/sqlteam |
 |
|
|
|