| Author |
Topic |
|
sorin3
Starting Member
5 Posts |
Posted - 2009-07-14 : 11:22:18
|
| I have a simple table with 3 fields: ID, Tag1, DataID Tag1 Data________________________________1 2 6/1/2009 22:002 1 6/1/2009 23:003 2 6/2/2009 6:004 3 6/2/2009 7:005 2 6/2/2009 8:006 2 6/2/2009 9:007 2 6/2/2009 10:008 1 6/2/2009 11:009 2 6/2/2009 11:3010 1 6/2/2009 13:0011 2 6/2/2009 14:0012 2 6/2/2009 15:0013 2 6/2/2009 16:0014 1 6/2/2009 17:0015 2 6/2/2009 18:0016 1 6/2/2009 19:0017 1 6/2/2009 20:0018 2 6/2/2009 21:0019 2 6/2/2009 22:0020 3 6/2/2009 23:0021 1 6/2/2009 23:5922 2 6/3/2009 1:0023 3 6/3/2009 2:0024 2 6/3/2009 3:0025 3 6/3/2009 4:0026 2 6/3/2009 5:0027 3 6/3/2009 6:0028 2 6/3/2009 7:0029 3 6/3/2009 8:0030 2 6/3/2009 9:0031 3 6/3/2009 10:0032 2 6/3/2009 11:0033 1 6/3/2009 11:5534 2 6/3/2009 22:0035 2 6/3/2009 23:00Must I do a average AVG(Tag1) after 3 hourly intervals over a period of time.Hourly intervals are: 10.30 PM - 6.30 AM, (represents schimbul1) 06.30 AM - 14.30 PM, (represents schimbul2) 14.30 PM - 22.30 PM, (represents schimbul3)I tried this:SELECT AVG(CASE WHEN DATEPART(hour, DataTime) BETWEEN DATEPART(hour,'10:30:00 PM') AND DATEPART(hour,'06:30:00 AM') THEN Tag1 ELSE NULL END) AS Schimbul_1, AVG(CASE WHEN DATEPART(hour, DataTime) BETWEEN DATEPART(hour,'6:30:00 AM') AND DATEPART(hour,'2:30:00 PM') THEN Tag1 ELSE NULL END) AS Schimbul_2, AVG(CASE WHEN DATEPART(hour, DataTime) BETWEEN DATEPART(hour,'2:30:00 PM') AND DATEPART(hour,'10:30:00 PM') THEN Tag1 ELSE NULL END) AS Schimbul_3FROM dbo.TableTestWHERE (DataTime BETWEEN '6/1/2009 10:00:00 PM' AND '6/3/2009 11:00:00 PM')GROUP BY DATEPART(DAY,DataTime)The result looks like:Schimbul_1 Schimbul_2 Schimbul_3__________________________________________________________NULL NULL 1.0NULL 2.2222222222222223 1.7777777777777777NULL 1.7142857142857142 2.0Note:For schimbul1 and schimbul2, belonging on the same day, is well calculated. The problem is schimbul1 - time which belongs to two days.Can help me someone in this problem?Thanks. |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-07-14 : 19:25:15
|
[code]Schimbul_1 = AVG(CASE WHEN DBO.F_TIME_FROM_DATETIME(DataTime) >= '22:30' OR DBO.F_TIME_FROM_DATETIME(DataTime) < '06:30' THEN Tag1 ELSE NULL END),Schimbul_2 = AVG(CASE WHEN DBO.F_TIME_FROM_DATETIME(DataTime) >= '06:30' OR DBO.F_TIME_FROM_DATETIME(DataTime) < '14:30' THEN Tag1 ELSE NULL END),Schimbul_3 = AVG(CASE WHEN DBO.F_TIME_FROM_DATETIME(DataTime) >= '14:30' OR DBO.F_TIME_FROM_DATETIME(DataTime) < '22:30' THEN Tag1 ELSE NULL END)http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=65358&SearchTerms=F_TIME_FROM_DATETIME[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
sorin3
Starting Member
5 Posts |
Posted - 2009-07-15 : 02:46:52
|
| Thanks for answer.I have tested your code.Unfortunately it is not calculated correctly.The code...SELECT Schimbul_1 = AVG(CASE WHEN DBO.F_TIME_FROM_DATETIME(DataTime) >= '22:00' OR DBO.F_TIME_FROM_DATETIME(DataTime) < '06:00' THEN Tag1 ELSE NULL END) FROM dbo.TableTestWHERE (DataTime BETWEEN '6/1/2009 10:00:00 PM' AND '6/3/2009 11:00:00 PM')return this:Schimbul_1-----------------2.0833333333333335...which is not the average value for Tag1 (time interval '22:00'-'06:00').The code...SELECT Tag1, DataTimeFROM dbo.TableTestWHERE DBO.F_TIME_FROM_DATETIME(DataTime) >= '22:00' OR DBO.F_TIME_FROM_DATETIME(DataTime) < '06:00'return this:Tag1 DataTime2.0 2009-06-01 22:00:00.0001.0 2009-06-01 23:00:00.0002.0 2009-06-02 22:00:00.0003.0 2009-06-02 23:00:00.0001.0 2009-06-02 23:59:00.0002.0 2009-06-03 01:00:00.0003.0 2009-06-03 02:00:00.0002.0 2009-06-03 03:00:00.0003.0 2009-06-03 04:00:00.0002.0 2009-06-03 05:00:00.0002.0 2009-06-03 22:00:00.0002.0 2009-06-03 23:00:00.000where AVG(Tag1) is not the average value for Tag1 (time interval '22:00'-'06:00').I wrong somewhere?Thanks again. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-07-15 : 03:17:55
|
[code]select (2.0 + 1.0 + 2.0 + 3.0 + 1.0 + 2.0 + 3.0 + 2.0 + 3.0 + 2.0 + 2.0 + 2.0) / 12[/code]gives 2.083333What's the average value for the above are you expecting ? KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
sorin3
Starting Member
5 Posts |
Posted - 2009-07-15 : 03:35:48
|
| For time interval '22:00'-'06:00' must I have select(2.0 + 1.0 + 2.0)/3 where AVG(Tag1)=1.666for the above table (TableTest)...SELECT AVG(Tag1) AS Tag1FROM dbo.TableTestWHERE (DataTime BETWEEN '6/1/2009 10:00:00 PM' AND '6/2/2009 6:00:00 AM')Tag1----1.666 |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-07-15 : 03:53:16
|
quote: 10.30 PM - 6.30 AM, (represents schimbul1)06.30 AM - 14.30 PM, (represents schimbul2)14.30 PM - 22.30 PM, (represents schimbul3)
for time 6:30 AM you want to include in schimbul1 or schimbul2 ?or BOTH ?Note in my query, i specify for schimbul1 >= '22:30' OR < '06:30' which exclude it from schimbul1 and put 06:30 into schimbul2 KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
sorin3
Starting Member
5 Posts |
Posted - 2009-07-15 : 04:48:39
|
| The 3 time intervals are a working day.So, 10.30 PM - 6.30 AM, represents tour 1 (alias schimbul1)06.30 AM - 14.30 PM, represents tour 2 (alias schimbul2)14.30 PM - 22.30 PM, represents tour 3 (alias schimbul3)'10.30 PM - 6.30 AM'=8 hours'06.30 AM - 14.30 PM'=8 hours'14.30 PM - 22.30 PM'=8 hours-----------------------------Total = 24 hours - a working day.Note: - tour 1 (alias schimbul1) - time which belongs to two days neighboring '10.30 PM - 6.30 AM'. - yes, tour 1 must be >=22:30 and <=06:30 tour 2 must be >=06:30 and <=14:30 tour 3 must be >=14:30 and <=22:30 ORDER BY Data (for "TableTest" above)My "SELECT" must to calculate the average for the three time intervals (tour 1, tour 2 and tour 3) where Data is the time interval selected by user. By example Data BETWEEN '6/1/2009 10:00:00 PM' AND '6/2/2009 6:00:00 AM'Thanks. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-07-15 : 05:05:38
|
quote: Originally posted by sorin3 The 3 time intervals are a working day.So, 10.30 PM - 6.30 AM, represents tour 1 (alias schimbul1)06.30 AM - 14.30 PM, represents tour 2 (alias schimbul2)14.30 PM - 22.30 PM, represents tour 3 (alias schimbul3)'10.30 PM - 6.30 AM'=8 hours'06.30 AM - 14.30 PM'=8 hours'14.30 PM - 22.30 PM'=8 hours-----------------------------Total = 24 hours - a working day.Note: - tour 1 (alias schimbul1) - time which belongs to two days neighboring '10.30 PM - 6.30 AM'. - yes, tour 1 must be >=22:30 and <=06:30 tour 2 must be >=06:30 and <=14:30 tour 3 must be >=14:30 and <=22:30 ORDER BY Data (for "TableTest" above)My "SELECT" must to calculate the average for the three time intervals (tour 1, tour 2 and tour 3) where Data is the time interval selected by user. By example Data BETWEEN '6/1/2009 10:00:00 PM' AND '6/2/2009 6:00:00 AM'Thanks.
If you have a record with time 06:30 you want to consider it as tour1 or tour2 ? It does not make sense to consider under both tour1 and tour2. You will be double counting. KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
sorin3
Starting Member
5 Posts |
Posted - 2009-07-15 : 06:31:14
|
| Ok, is not very important this aspect.Must only to calculate corect.Yes, we can consider:tour 1 must be >= 22:30 and < 06:30tour 2 must be >= 06:30 and < 14:30tour 3 must be >= 14:30 and < 22:30Thank you for your time. |
 |
|
|
|