某些选定记录SQL的平均值

时间:2014-09-09 02:02:17

标签: sql sql-server sql-server-2008

我有下表:

ID     myDate       myTime     Value
1      2014-06-01   00:00:00   100
2      2014-06-01   01:00:00   125
3      2014-06-01   02:00:00   132
4      2014-06-01   03:00:00   139
5      2014-06-01   04:00:00   145
6      2014-06-01   05:00:00   148
FF.
24     2014-06-01   23:00:00   205
25     2014-06-02   00:00:00   209
26     2014-06-02   01:00:00   215
27     2014-06-02   02:00:00   223
FF.
48     2014-06-02   23:00:00   280
49     2014-06-03   00:00:00   290
50     2014-06-03   01:00:00   302
FF.
71     2014-06-03   22:00:00   389
72     2014-06-03   23:00:00   400
73     2014-06-04   00:00:00   405
FF.

我希望Average第二天01:00:00到00:00:00之间的所有记录。例如:

我想知道' 2014-06-01'的平均价值。平均值为2014-06-01 01:00:00至2014-06-02 00:00:00。

如何使用SQL select语句执行此操作?

谢谢。

2 个答案:

答案 0 :(得分:1)

我开始从日期减去一小时到#34;修复"他们排队等待几天。添加日期和时间,然后减去一个小时。

然后这是一个简单的小组。这将适用于MS SQL Server。

WITH CorrectedDates AS (
  SELECT 
  DateAdd(hh,-1,DateAdd(hh,DatePart(hh,myTime),myDate)) AS newDate, Value
  FROM table1
  )
SELECT CAST(newDate AS DATE) AS MainDate, AVG(Value)
FROM CorrectedDates cd
GROUP BY CAST(newDate AS DATE)

要只获取一个日期,只需在FROM和GROUP BY子句之间添加以下内容:

WHERE NewDate = '2014-6-1' 

答案 1 :(得分:0)

我有自己的答案:查看fiddle

if (OBJECT_ID('[TempDB]..[#DataTable]') is not null)
    drop table #DataTable
create table #DataTable (
    [Date] date,
    [Time] time,
    Value int
)
declare @inputDate date
set @inputDate = '2014-6-1'

insert into #DataTable values
(cast('2014-6-1' as date), '0:0', 100),
(cast('2014-6-1' as date), '1:0', 200),
(cast('2014-6-1' as date), '2:0', 300),
(cast('2014-6-1' as date), '3:0', 400),
(cast('2014-6-1' as date), '4:0', 500),
(cast('2014-6-1' as date), '5:0', 600),
(cast('2014-6-1' as date), '6:0', 700),
(cast('2014-6-1' as date), '7:0', 800),
(cast('2014-6-1' as date), '8:0', 900),
(cast('2014-6-1' as date), '9:0', 1000),
(cast('2014-6-1' as date), '10:0', 1100),
(cast('2014-6-1' as date), '11:0', 1200),
(cast('2014-6-1' as date), '12:0', 1300),
(cast('2014-6-1' as date), '13:0', 1400),
(cast('2014-6-1' as date), '14:0', 1500),
(cast('2014-6-1' as date), '15:0', 1600),
(cast('2014-6-1' as date), '16:0', 1700),
(cast('2014-6-1' as date), '17:0', 1800),
(cast('2014-6-1' as date), '18:0', 1900),
(cast('2014-6-1' as date), '19:0', 2000),
(cast('2014-6-1' as date), '20:0', 2100),
(cast('2014-6-1' as date), '21:0', 2200),
(cast('2014-6-1' as date), '22:0', 2300),
(cast('2014-6-1' as date), '23:0', 2400),
(cast('2014-6-2' as date), '0:0', 2500),
(cast('2014-6-2' as date), '1:0', 2600),
(cast('2014-6-2' as date), '2:0', 2700),
(cast('2014-6-2' as date), '3:0', 2800),
(cast('2014-6-2' as date), '4:0', 2900),
(cast('2014-6-2' as date), '5:0', 3000),
(cast('2014-6-2' as date), '6:0', 3100),
(cast('2014-6-2' as date), '7:0', 3200),
(cast('2014-6-2' as date), '8:0', 3300),
(cast('2014-6-2' as date), '9:0', 3400),
(cast('2014-6-2' as date), '10:0', 3500),
(cast('2014-6-2' as date), '11:0', 3600),
(cast('2014-6-2' as date), '12:0', 3700),
(cast('2014-6-2' as date), '13:0', 3800),
(cast('2014-6-2' as date), '14:0', 3900),
(cast('2014-6-2' as date), '15:0', 4000),
(cast('2014-6-2' as date), '16:0', 4100),
(cast('2014-6-2' as date), '17:0', 4200),
(cast('2014-6-2' as date), '18:0', 4300),
(cast('2014-6-2' as date), '19:0', 4400),
(cast('2014-6-2' as date), '20:0', 4500),
(cast('2014-6-2' as date), '21:0', 4600),
(cast('2014-6-2' as date), '22:0', 4700),
(cast('2014-6-2' as date), '23:0', 4800)

select @inputDate Tanggal, avg(d.Value) [Rata-rata]
from #DataTable d
where
    (d.[Date] = @inputDate and d.[Time] <> '0:0')
    or (d.[Date] = DATEADD(day, 1, @inputDate) and d.[Time] = '0:0')

谢谢。