0

我正在寻找给定月份和所有设备代码的最后一个仪表读数,这些设备代码在字段meter_number中的值为“1”。

SELECT [Company_Code]
  ,[Equipment_Code]
  ,[Meter_Number]      
  ,Max([Transaction_Date]) Last_Tran     
  ,max([meter_reading]) 
FROM [Forefront].[dbo].[EC_METER_HISTORY_MC] 

where [Meter_Number] = '1' and [Transaction_Date] < '20130101'
group by [Company_Code]
  ,[Equipment_Code]
  ,[Meter_Number]

给我带来了结果,但我只想要meter_reading 字段,所以我可以在水晶报告中使用它。

4

2 回答 2

1

如果您想要由 确定的最后读数,MAX([Transaction_Date])您可以通过仅检索一条记录并按以下顺序对记录进行排序来做到这一点[Transaction_Date]

SELECT TOP 1
  [Meter_Reading]
FROM [Forefront].[dbo].[EC_METER_HISTORY_MC] 
WHERE [Meter_Number] = '1' AND [Transaction_Date] < '20130101'
ORDER BY [Transaction_Date] DESC

由于您只返回一条记录并定义了一条[Meter_Number],因此无需执行GROUP BY.

于 2013-01-29T19:36:59.360 回答
0
SELECT     
  max([meter_reading]) 
FROM [Forefront].[dbo].[EC_METER_HISTORY_MC] 

where [Meter_Number] = '1' and [Transaction_Date] < '20130101'
group by [Company_Code]
  ,[Equipment_Code]
  ,[Meter_Number]
于 2013-01-29T19:25:44.137 回答