如何在SQL Server 2005中平均/总结一天中的数据

时间:2011-09-16 21:15:26

标签: sql-server-2005 aggregate-functions aggregation

我正在尝试在一天内平均SQL Server 2005中的数据。如果我使用简单查询

,这就是我的数据库看起来像这样
SELECT timestamp, FEED
FROM ROASTER_FEED
ORDER timestamp

数据:

timestamp               Feed    
02/07/2011 12:00:01     1246   
02/07/2011 12:00:01     1234    
02/07/2011 12:00:01     1387    
02/07/2011 12:00:02     1425   
02/07/2011 12:00:03     1263   
...    
02/07/2011 11:00:01     1153    
02/07/2011 11:00:01     1348    
02/07/2011 11:00:01     1387    
02/07/2011 11:00:02     1425    
02/07/2011 11:00:03     1223    
....   
03/07/2011 12:00:01     1226    
03/07/2011 12:00:01     1245    
03/07/2011 12:00:01     1384    
03/07/2011 12:00:02     1225    
03/07/2011 12:00:03     1363

当某人选择一个月中的某个日期时,我不知道如何对Feed进行平均,并且只显示当天的平均值/总和。

例如,在结果中,如果我选择日期为02/07/2011。它会给我这样的东西:

02/07/2011 1234 (average value/or sum)

8 个答案:

答案 0 :(得分:2)

一种可能性,如果您需要经常这样做:将日,月,年的三个计算列添加到您的表中。这些列是根据timestamp列自动计算的,它们只是整数值,因此很容易在GROUP BY中使用。

为此,请使用以下T-SQL语句:

ALTER TABLE dbo.ROASTER_FEED ADD TSDay AS DAY(timestamp) PERSISTED
ALTER TABLE dbo.ROASTER_FEED ADD TSMonth AS MONTH(timestamp) PERSISTED
ALTER TABLE dbo.ROASTER_FEED ADD TSYear AS YEAR(timestamp) PERSISTED

现在,您可以根据自己的意愿轻松选择数据:

SELECT TSDay, TSMonth, TSYear, SUM(FEED)   -- use AVG(FEED) for average values
FROM dbo.ROASTER_FEED
WHERE TSYear = 2011 AND TSMonth = 8   -- or whatever you want to grab from the table!
ORDER BY timestamp
GROUP BY TSDay, TSMonth, TSYear

答案 1 :(得分:1)

嗯,我不是一个SQL人,但我希望有类似的东西:

SELECT SUM(ValueColumn) AS Total, AVG(ValueColumn) AS Average FROM YourTableName
WHERE RecordedTime >= @StartTime AND RecordedTime < @EndTime

(其中@StartTime和@EndTime是调用代码填写的参数)。

(我不清楚如果没有找到记录会发生什么......值得一试。)

答案 2 :(得分:1)

早上7:40是00:00后的460分钟

下午7:40是00:00后的1180分钟

midnigth是00:00后的1440分钟

DATEPART(hh,SomeDate)* 60 + DATEPART(mi,SomeDate)为您提供给定SomeDate 00:00后的分钟数

所以,你可以使用:

SELECT
    AVG(Temperature) As Dayshift
FROM Drye_data
WHERE DATEPART(hh, TimeStamp)*60 + DATEPART(mi, TimeStamp) BETWEEN 460 AND 1180
      AND @SelectedDate = CAST(FLOOR(CAST(TimeStamp AS FLOAT)) AS DATETIME)

和夜班:

    SELECT
    AVG(Temperature) As Nigthshift
FROM Drye_data
WHERE (
        (DATEPART(hh, TimeStamp)*60 + DATEPART(mi, TimeStamp) BETWEEN 0 AND 460)
        AND @SelectedDate = DATEADD(dd, 1, CAST(FLOOR(CAST(TimeStamp AS FLOAT)) AS DATETIME))
      )
      OR 
      (
         (DATEPART(hh, TimeStamp)*60 + DATEPART(mi, TimeStamp) BETWEEN 1180 AND 1440) AND @SelectedDate = CAST(FLOOR(CAST(TimeStamp AS FLOAT)) AS DATETIME)
      )

答案 3 :(得分:0)

SELECT COUNT(timestamp) as NumValues, 
       SUM(Feed) as ValuesSum, 
       AVG(Feed) as Average

  FROM ROASTER_FEED

 WHERE timestamp BETWEEN '1/1/2011'  AND '9/15/2011'

答案 4 :(得分:0)

要获得一天的平均Feed,您可以使用此功能。

declare @Date datetime

set @Date = '20110702'

select @Date as [timestamp], avg(FEED) as AvgFeed
from ROASTER_FEED
where [timestamp] >= @Date and 
      [timestamp] < dateadd(day, 1, @Date)

答案 5 :(得分:0)

试试这个:

SELECT timestamp, sum(FEED)
FROM ROASTER_FEED
WHERE timestamp=....
GROUP BY timestamp
如果您处理大量数据,它可能会很慢,在这种情况下,您应该考虑一个额外的表,其中包含每天的预先计算的值。

答案 6 :(得分:0)

@marc_s遇到了使用计算列来处理此问题的首选解决方案,但要在运行中进行,请将时间戳数据转换为剪切时间组件。在我的示例中,我将其转换为包含0填充日期的10个字符的字段。

;
WITH ROASTER_FEED ([timestamp],[Feed]) AS
(
SELECT CAST('02/07/2011 12:00:01' AS datetime),1246   
UNION ALL SELECT '02/07/2011 12:00:01',1234    
UNION ALL SELECT '02/07/2011 12:00:01',1387    
UNION ALL SELECT '02/07/2011 12:00:02',1425   
UNION ALL SELECT '02/07/2011 12:00:03',1263   

UNION ALL SELECT '02/07/2011 11:00:01',1153    
UNION ALL SELECT '02/07/2011 11:00:01',1348    
UNION ALL SELECT '02/07/2011 11:00:01',1387    
UNION ALL SELECT '02/07/2011 11:00:02',1425    
UNION ALL SELECT '02/07/2011 11:00:03',1223    

UNION ALL SELECT '03/07/2011 12:00:01',1226    
UNION ALL SELECT '03/07/2011 12:00:01',1245    
UNION ALL SELECT '03/07/2011 12:00:01',1384    
UNION ALL SELECT '03/07/2011 12:00:02',1225    
UNION ALL SELECT '03/07/2011 12:00:03',1363
)
SELECT
    -- Shear off the time value by forcing the date into date values
    -- If this was SQL Server 2008+, we'd just cast to date type and
    -- be done with it
    -- Chart for convert options
    -- http://msdn.microsoft.com/en-us/library/ms187928.aspx
    CONVERT(char(10), RF.[timestamp], 121) AS [timestamp]
,   COUNT(1) AS row_counts
,   SUM(RF.Feed) as ValuesSum 
,   AVG(RF.Feed) as Average
FROM 
    ROASTER_FEED RF
GROUP BY
    CONVERT(char(10), RF.[timestamp], 121)    

结果

timestamp   row_counts   ValuesSum  Average
2011-02-07  10           13091      1309
2011-03-07  5            6443       1288

答案 7 :(得分:0)

从这里查看一些教程 http://www.smallsql.de/doc/sql-functions/date-time/index.html 我一直在寻找类似的东西,发现该网站很有用,并认为它可能有所帮助