3

我来自 MySQL 并尝试在 SQL Server 2005 上编写 T-SQL 代码,但我发现它完全不同。

这就是我想要做的(使用 MySQL)

select sum(datapoint) as sum, date(mytimestamp) as date
from datalog
where datapoint = '27'
group by date

即获取按日期汇总和分组的数据列表。

4

2 回答 2

1

在标记 sql server 2008 时回答

select sum(datapoint) as [sum], CAST(mytimestamp AS DATE) as [date]
from datalog
where datapoint = '27'
group by CAST(mytimestamp AS DATE)

对于 SQL Server 2005

SELECT   CONVERT(VARCHAR(11), timestamp, 111) DATE, 
         SUM(datapoint) totalDatapoint
FROM     table1
GROUP BY CONVERT(VARCHAR(11), timestamp, 111)
ORDER BY DATE ASC

SQLFiddle 演示

于 2012-10-15T00:48:53.990 回答
0

转换为 varchar 不是一个好的解决方案,只需从时间戳中删除 timepart 并按如下方式分组:

select sum(datapoint) as sum, dateadd(d, 0, datediff(d, 0, mytimestamp)) as date
from datalog
where datapoint = '27'
group by datediff(d, 0, mytimestamp)
于 2012-10-15T07:34:18.340 回答