我在SQL Server 2005中有一個我想要彙總數據的表,這裏是一個例子。如何結合聚合SQL查詢和普通查詢?
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
未填寫?
任何指針將不勝感激。
您需要先按數據分組,然後應用數據透視。 – ClearLogic