0

我在 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. 我将如何查询诸如StartDate和之类的列Stage?特别是EndDate在工作完成之前没有填写的地方?

任何指针将不胜感激。

4

5 回答 5

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) 就可以了。

于 2012-08-31T13:59:50.503 回答
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
于 2012-08-31T13:50:30.997 回答
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

注意:Stage 仅按字母顺序工作,如果您有一个更大的起始字母,那么“F”可能无法正常工作。

于 2012-08-31T13:53:58.537 回答
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;
于 2012-08-31T13:55:05.860 回答
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
于 2012-08-31T14:13:29.317 回答