2012-08-31 190 views
0

我在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 

我的問題是:

  1. 如何合併到一條查詢?

  2. 我如何查詢列如StartDateStage?特別是在作業完成之前EndDate未填寫?

任何指針將不勝感激。

+0

您需要先按數據分組,然後應用數據透視。 – ClearLogic

回答

1

在這裏你去:

SELECT 
JobName, 
SUM(CASE CounterName WHEN 'Kilos' THEN ProductionCounter ELSE 0 END) AS SumKilos, 
SUM(CASE CounterName WHEN 'Bars' THEN ProductionCounter ELSE 0 END) AS SumBars, 
MIN(StartDate), 
MAX(COALESCE(EndDate, 'undefined date')), 
MAX(Stage) 
FROM vwOeeInterval 
WHERE CounterName IN ('Kilos', 'Bars') 
GROUP BY JobName; 

不知道這MAX(COALESCE(EndDate, 'undefined date'))作品在SQL Server中,因爲數據類型可能會發生衝突。當你想替換NULL值時,你只需要COALESCE()。如果它不起作用,只需執行MAX(EndDate)即可。

0

您可以將它們組合起來

SELECT 
    JobName, CounterName, SUM(ProductionCounter), 
    MIN(StartDate), MAX(EndDate), MAX(Stage) 
FROM vwOeeInterval 
GROUP BY JobName , CounterName 

HAVING MAX(EndDate) > SomeDate OR MAX(EndDate) is Null 
0

篩選他們試試這個,也許這就是你需要:

SELECT JobName, SUM(TotalKilos), SUM(ProductionCounter), MAX(StartDate) , MAX(EndDate), MAX(Stage) 
    GROUP BY JobName , CounterName, StartDate, EndDate 
    --Optional: 
    WHERE EndDate IS NOT NULL 

注意:第一階段只在工作字母順序,如果你有更大的起始字母,那麼'F'就不起作用。然後

SELECT JobName, 
     SUM(CASE WHEN CounterName = 'Kilos' THEN ProductionCounter ELSE 0 END) AS TotalKilos, 
     SUM(CASE WHEN CounterName = 'Bars' THEN ProductionCounter ELSE 0 END) AS TotalBars 
    FROM vwOeeInterval 
    GROUP BY JobName; 

,包了休息,也許是:

0

要結合你的查詢

SELECT JobName, 
     SUM(CASE WHEN CounterName = 'Kilos' THEN ProductionCounter ELSE 0 END) AS TotalKilos, 
     SUM(CASE WHEN CounterName = 'Bars' THEN ProductionCounter ELSE 0 END) AS TotalBars, 
     MIN(StartDate) AS StartDate, 
     MAX(EndDate) AS EndDate, 
     MAX(Stage) AS Stage 
    FROM vwOeeInterval 
    GROUP BY JobName; 
+0

匿名downvoter會照顧解釋他們的反對嗎? –

0
select jobname, max(bars) as "TotalBars", max(kg) as "Total Kilos", sd as "Start date", ed as "End date" from(
SELECT JobName, if(countername="bars",SUM(ProductionCounter),null) as "bars" , if(countername="kilograms",SUM(ProductionCounter),null) as "kg" , min(startdate) as sd, max(enddate) as ed 
FROM vwOeeInterval 
GROUP BY JobName , CounterName) t 
group by jobname