sql中按小时分组的日期时间

mi7gmzs6  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(678)

我想按小时分组,在选择列“时间”中,我想显示完整的时间格式(例如:12:15:56 pm),但当我运行may代码时,会显示以下错误。
错误:列“vflowrate\u hr.time”在选择列表中无效,因为它既不包含在聚合函数中,也不包含在group by子句中。

select Sum(Value) As FlowRateSum, Dir.PlantAddress, PlantType, FlowRateNo,vFlowRate_hr.Date , time 
from vFlowRate_hr Inner Join DIR ON vFlowRate_hr.FlowRateID = DIR.ID
where FlowRateID = @FlowRateID and DIR.PlantType = @PlantType
and vFlowRate_hr.Date BETWEEN @FromDate AND @ToDate
group by FlowRateNo,Dir.PlantAddress,PlantType,vFlowRate_hr.Date, DATEPART(Hour, time)
eqqqjvef

eqqqjvef1#

大概,你想要的是时间:

select Sum(Value) As FlowRateSum, Dir.PlantAddress, PlantType, FlowRateNo, vFlowRate_hr.Date,
       datepart(hour, time)
 from vFlowRate_hr Inner Join
      DIR
      ON vFlowRate_hr.FlowRateID = DIR.ID
 where FlowRateID = @FlowRateID and DIR.PlantType = @PlantType and
       vFlowRate_hr.Date BETWEEN @FromDate AND @ToDate
 group by Dir.PlantAddress, PlantType, FlowRateNo, vFlowRate_hr.Date,
       datepart(hour, time)

然后 select 将匹配 group by .
请注意,我更改了 group by 因此列的顺序与 select --更容易验证。
此外,您应该限定所有列引用,以便清楚它们来自何处。按原样,查询仍将运行,但它的可维护性和可更新性较差,因为列引用可能不明确。

相关问题