如何在 SQL Server 2005 中对一天内的数据进行平均/求和
我正在尝试对 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
:当有人选择一个月中的日期并且它仅显示当天的平均值/总和时,我不知道如何对提要进行平均。
例如,在结果中,如果我选择日期为 02/07/2011。它会给我这样的东西:
02/07/2011 1234 (average value/or sum)
I'm trying to average data in SQL Server 2005 in a day. Here is what my database look like this if I use simple query as
SELECT timestamp, FEED
FROM ROASTER_FEED
ORDER timestamp
Data:
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
I don't know how to average the feed when someone select a date in a month and it give display the average/sum of that day only.
For example, in the outcome, if I select day as 02/07/2011. It would give me something like this:
02/07/2011 1234 (average value/or sum)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
从这里查看一些教程
http://www.smallsql.de/doc/sql-functions /日期时间/index.html
我一直在寻找类似的东西,发现该网站很有用,并认为它可能会有所帮助
Check out some of the tutorials from here
http://www.smallsql.de/doc/sql-functions/date-time/index.html
ive been looking for something simialr and found that site useful and thought it may help
如果您需要足够频繁地执行此操作,则一种可能性是:将日、月、年的三个计算列添加到表中。这些列是根据
timestamp
列自动计算的,并且它们只是整数值,因此它们很容易在GROUP BY
中使用。为此,请使用以下 T-SQL 语句:
现在,您可以根据您希望的任何一天轻松选择数据:
One possibility, if you need to do this often enough: add three computed columns for day, month, year to your table. Those columns are computed automatically based on the
timestamp
column, and they're just integer values, so they're easy to use in aGROUP BY
.To do this, use these T-SQL statements:
Now, you can easily select your data based on any day you wish:
上午 7:40 是 00:00 后 460 分钟
7:40pm 是 00:00 后 1180 分钟
午夜是 00:00 后 1440 分钟
并且 DATEPART(hh, SomeDate)*60 + DATEPART(mi, SomeDate) 为您提供分钟数对于给定的 SomeDate 的 00:00 之后
,您可以使用:
对于夜班:
7:40am is 460 minutes after 00:00
7:40pm is 1180 minutes after 00:00
midnigth is 1440 minutes after 00:00
And DATEPART(hh, SomeDate)*60 + DATEPART(mi, SomeDate) gives you the amount of minutes after 00:00 for a given SomeDate
So, you could use:
and for the Nightshift:
好吧,我不是一个 SQL 专家,但我期望类似的东西:(
其中 @StartTime 和 @EndTime 是由调用代码填充的参数)。
(我不清楚如果没有找到记录会发生什么......值得检查。)
Well I'm not a SQL guy, but I'd expect something like:
(where @StartTime and @EndTime are parameters filled in by the calling code).
(It's not clear to me what will happen if no records are found... worth checking out.)
试试这个:
如果您正在处理大量数据,它可能会很慢,在这种情况下,您应该考虑使用一个额外的表来保存每天的预先计算的值。
try this:
it could be slow if you are handling alot of data, in this case you should considering an additional table which holds the pre-calculated values for each day.
@marc_s 想到了使用计算列来处理此问题的首选解决方案,但要即时执行此操作,请转换时间戳数据以剪掉时间部分。在我的示例中,我将其转换为包含 0 填充日期的 10 个字符字段。
结果
@marc_s hit upon the preferred solution of using a computed columns to handle this but to do it on-the-fly, cast your timestamp data to shear off the time component. In my example, I cast it to a 10 character field that contains the 0 padded date.
Results in
要获得一天的平均饲料,您可以使用它。
To get the average feed for one day you can use this.