如何组合聚合SQL查询和普通查询?

时间:2012-08-31 13:48:44

标签: sql sql-server-2005

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

我的问题是:

  1. 如何将这些组合成一个查询?

  2. 如何查询StartDateStage等列?特别是在作业完成之前未填写EndDate的地方?

  3. 任何指针都会非常感激。

5 个答案:

答案 0 :(得分: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)就可以了。

答案 1 :(得分: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

答案 2 :(得分: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'就不会起作用。

答案 3 :(得分: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
    FROM vwOeeInterval
    GROUP BY JobName;

然后,结束其余部分,或许:

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;

答案 4 :(得分: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