我在 SQL 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
I want to format this into the following
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(KiloCounter)
FROM vwOeeInterval
GROUP BY JobName
SELECT JobName, SUM(BarCounter)
FROM vwOeeInterval
GROUP BY JobName
我的问题是:
如何将这些组合到一个查询中?
如何查询 StartDate 和 Stage 等列?特别是在工作完成之前没有填写结束日期?
任何指针将不胜感激。