我在 SQL Server 2005 中有一个要从中聚合数据的表,这是一个示例。
JobName ProductionCounter StartDate EndDate CounterName Stage
----------------------------------------------------------------------------------------------------------
200 23 2012-08-30 13:45:00 Kilograms Final
200 2 2012-08-30 13:45:00 Bars Final
[...]
200 46 2012-08-30 13:45:00 2012-08-30 17:23:00 Kilograms Final
200 4 2012-08-30 13:45:00 2012-08-30 17:23:00 Bars Final
300 20 2012-08-30 13:45:00 Kilograms 1st
300 10 2012-08-30 18:12:00 Bars 1st
[...]
300 40 2012-08-30 18:12:00 2012-08-30 19:17:00 Kilograms 1st
300 20 2012-08-30 18:12:00 2012-08-30 19:17:00 Bars 1st
我想将其格式化为以下
JobName TotalKilos TotalBars StartDate EndDate Stage
-----------------------------------------------------------------------------------------------
200 69 6 2012-08-30 13:45:00 2012-08-30 17:23:00 Final
300 60 30 2012-08-30 18:12:00 2012-08-30 19:17:00 1st
这让我意识到我对 SQL 一无所知。我已经尝试以下作为开始
SELECT JobName, SUM(ProductionCounter)
FROM vwOeeInterval
WHERE (CounterName = 'Kilos')
GROUP BY JobName
SELECT JobName, SUM(ProductionCounter)
FROM vwOeeInterval
WHERE (CounterName = 'Bars')
GROUP BY JobName
我的问题是:
如何将这些组合到一个查询中?
我将如何查询诸如
StartDate
和之类的列Stage
?特别是EndDate
在工作完成之前没有填写的地方?
任何指针将不胜感激。