在T-SQL中获取每日消费

时间:2015-02-10 04:11:54

标签: sql sql-server tsql

我的SQL QUERY的一致性存在问题。我想获得我的查询中指定范围的每日每日消费量。我的每日数据已经完成,但如果我运行此查询,则表示我缺少数据。

SELECT c.serialNo AS 'serialNo',  
  CONVERT(VARCHAR,b.[datetime],101) AS 'dteTime'  ,
  CONVERT(VARCHAR,(CONVERT(MONEY,(MAX(a.normalenergy)))),1) AS 'prevnormalEnergy' , 
  CONVERT(VARCHAR,(CONVERT(MONEY,(MAX(b.normalenergy)))),1) AS 'presnormalEnergy' , 
  CONVERT(VARCHAR,(CONVERT(MONEY,((MAX(b.normalenergy) - MAX(a.normalenergy))))),1) AS 'presnormalConsumption'
FROM 
  Billing a, 
  Billing b, 
  tblMeterInfo c     
WHERE 
  a.[datetime] = b.[datetime] -1 AND 
  b.[datetime] = a.[datetime] + 1
AND 
  a.serialNo = b.serialNo AND 
  b.serialNo = c.serialNo AND 
  a.serialNo IN ('212583533') AND 
  b.[datetime] between '8/7/2014' AND '8/15/2014'
GROUP BY 
  b.serialNo, 
  c.serialNo,
  c.meterName,
  CONVERT(VARCHAR,a.[datetime],101),  
  CONVERT(VARCHAR,b.[datetime],101) 
ORDER BY c.serialNo, CONVERT(VARCHAR,b.[datetime],101)

输出缺失日期 08/08/2014 08/09/2014

serialNo|dteTime|prevnormalEnergy|presnormalEnergy|presnormalConsumption

212583533|**08/07/2014**|10,928.41|11,067.81|139.40
212583533|**08/10/2014**|11,367.71|11,533.49|165.78
212583533|**08/11/2014**|11,533.49|11,702.62|169.13
212583533|**08/12/2014**|11,702.62|11,843.80|141.18
212583533|**08/13/2014**|11,843.80|11,989.22|145.42
212583533|**08/14/2014**|11,989.22|12,132.30|143.08

如果我删除了WHERE子句中的 - 1 + 1 ,它会显示所有结果,但我无法获得每个日期的每日消费

输出WHERE a.[datetime] = b.[datetime] AND b.[datetime] = a.[datetime]

serialNo|dteTime|prevnormalEnerg|presnormalEnergy|presnormalConsumption

212583533|**08/07/2014**|11,067.81|11,067.81|0.00
212583533|**08/08/2014**|11,214.35|11,214.35|0.00
212583533|**08/09/2014**|11,367.71|11,367.71|0.00
212583533|**08/10/2014**|11,533.49|11,533.49|0.00
212583533|**08/11/2014**|11,702.62|11,702.62|0.00
212583533|**08/12/2014**|11,843.80|11,843.80|0.00
212583533|**08/13/2014**|11,989.22|11,989.22|0.00
212583533|**08/14/2014**|12,132.30|12,132.30|0.00

0 个答案:

没有答案