我有一个包含列的表:
ID_LOG (this is the pk) | TS (type=datetime) | VALUE (type=double) | ID_CHANNEL (unique).
此表每15分钟从不同的频道记录值。这些值来自一些计数器,因此它们总是在增加
现在我想得到特定频道去年每一天的最大值和最小值(所以净差额)之间的差异。
类似的东西:
SELECT MAX(T1.value) - MIN(T1.value), T1.ts
FROM dbo.[LOG] as T1
WHERE T1.ts > DATEADD(year, -1, GETDATE()) and
T1.ID_CHANNEL=x //[x being the channel i want]
GROUP BY SOMEDATEFUNCITON(T1.ts) //[SOMEDATEFUNCTION is the function that groups results by date not taking in consideration the time of timestamp but only year-month-day]
我不关心效率,因为我每年只会做一次或两次查询。 这可能吗?你怎么做? 非常感谢!
答案 0 :(得分:1)
您可以使用CAST(T1.ts AS DATE)
截断时间戳。替换你拥有的东西:
SELECT MAX(T1.valore) - MIN(T1.valore), CAST(T1.ts AS DATE)
...
GROUP BY CAST(T1.ts AS DATE)
虽然根据您的架构,TS
已经DATE
,所以您不需要这样做。您是否要告诉我们TS
是DATETIME
?
答案 1 :(得分:1)
SELECT convert(char(10), creationDate, 120), COUNT(custID) AS totalRegistered
FROM dbo.SecureSignInUser
WHERE (CreationDate BETWEEN '11/1/2004' AND '11/30/2004'
GROUP BY convert(char(10), creationDate, 120)
检查这个日期格式.. http://msdn.microsoft.com/en-us/library/ms187928.aspx
根据您的数据库日期格式转换/转换您的函数返回日期值格式。