由DatePart分组的SQL Server平均值(Avg)

时间:2016-08-25 11:40:09

标签: sql-server group-by

我试图用VB语言运行以下命令(一些基于Microsoft的程序开发程序)。 sql行必须为每小时创建一个列的平均值(在本例中为Tag1)。

> sql = " Select DATEPART(hh, Time_Stamp) as Hour, AVG(Tag1) as Mean
> From TREND001 Where CONVERT(DATETIME2, Time_Stamp, 120) >= Convert
> (DATETIME2, '" & startDate & "', 120) AND CONVERT(DATETIME2,
> Time_Stamp, 120) < DATEADD (DAY,1, CONVERT (DATETIME2, '" & endDate &
> "', 120) ) Group BY DATEPART(hh, Time_Stamp)"

结果是:

  

列'TREND001.Time_Stamp'在选择列表中无效,因为它   不包含在聚合函数或GROUP BY中   子句。

1 个答案:

答案 0 :(得分:0)

希望这有帮助

Select DATEPART(hh, Time_Stamp) as Hour, AVG(Tag1) as Mean From TREND001 Where CONVERT(DATETIME2, Time_Stamp, 120) >= Convert (DATETIME2, '" & startDate & "', 120) AND CONVERT(DATETIME2, Time_Stamp, 120) < DATEADD (DAY,1, CONVERT (DATETIME2, '" & endDate & "', 120) ) Group BY DATEPART(hh, Time_Stamp)