每年使用不同输出的tSQL总和联合查询

时间:2018-08-01 15:03:20

标签: sql sql-server tsql ssms

因此,我环顾了整个网站,发现了类似的问题,这些问题的答案可以帮助我达到这一点,但没有任何解决我的问题的方法。实际的查询有5个不同的Union,但是为了简单起见,我在这里只显示2个:

SELECT 
    'Natural Gas (Stationary)' AS GHGType, 
    Year,
    ROUND(SUM(CO2), 0) AS CO2, 
    ROUND(SUM(CH4), 0) AS CH4, 
    ROUND(SUM(N2O), 0) AS N2O, 
    ROUND(SUM(TotalCO2e), 0) AS TotalCO2e, 
    ROUND(SUM(TotalCO2eMetric), 0) AS TotalCO2eMetric, 
    1 AS zOrder
FROM
    vw_NaturalGas
WHERE        
     CO2 IS NOT NULL
GROUP BY 
     Year

UNION

SELECT 
    'Mobile (Fleet)' AS GHGType, 
    Year, 
    ROUND(SUM(CO2), 0) AS CO2, 
    ROUND(SUM(CH4), 0) AS CH4, 
    ROUND(SUM(N2O), 0) AS N2O, 
    ROUND(SUM(TotalCO2e), 0) AS TotalCO2e,
    ROUND(SUM(TotalCO2eMetric), 0) AS TotalCO2eMetric, 
    2 AS zOrder
FROM 
    vw_MobileData
WHERE 
    CO2 IS NOT NULL
GROUP BY 
    Year

这将返回以下输出:

|GHGType                 |Year|CO2|CH4|N2O|TotalCO2e|TotalCO2eMetric|
+------------------------+----+---+---+---+---------+---------------+
|Natural Gas (Stationary)|2016|12 |14 |61 |3465     |126            |
|Natural Gas (Stationary)|2017|22 |24 |43 |10       |1204           |
|Mobile (Fleet)          |2016|5  |17 |23 |31       |832            |
|Mobile (Fleet)          |2017|25 |57 |73 |11       |713            |

我需要的是一个新的GHGType(范围1),它汇总了每年的天然气和移动领域(因此2016和2017年将有范围1):

|GHGType                 |Year|CO2|CH4|N2O|TotalCO2e|TotalCO2eMetric|
+------------------------+----+---+---+---+---------+---------------+
|Natural Gas (Stationary)|2016|12 |14 |61 |3465     |126            |
|Natural Gas (Stationary)|2017|22 |24 |43 |10       |1204           |
|Mobile (Fleet)          |2016|5  |17 |23 |31       |832            |
|Mobile (Fleet)          |2017|25 |57 |73 |11       |713            |
|Scope 1                 |2016|17 |21 |84 |3496     |958            |
|Scope 1                 |2017|47 |38 |117|21       |1917           |

我尝试使用以下查询执行此操作:

SELECT 
    'Scope 1' AS GHGType, 
    Year, 
    SUM(Scope1.CO2), 
    SUM(Scope1.CH4), 
    SUM(Scope1.N2O), 
    SUM(Scope1.TotalCO2e), 
    SUM(Scope1.TotalCO2eMetric), 
    5 as zOrder 
FROM
    (SELECT 
        'Natural Gas (Stationary)' AS GHGType, 
        Year,
        ROUND(SUM(CO2), 0) AS CO2, 
        ROUND(SUM(CH4), 0) AS CH4, 
        ROUND(SUM(N2O), 0) AS N2O, 
        ROUND(SUM(TotalCO2e), 0) AS TotalCO2e, 
        ROUND(SUM(TotalCO2eMetric), 0) AS TotalCO2eMetric, 
        1 AS zOrder
    FROM
        vw_NaturalGas
    WHERE        
         CO2 IS NOT NULL
    GROUP BY 
         Year

    UNION

    SELECT 
        'Mobile (Fleet)' AS GHGType, 
        Year, 
        ROUND(SUM(CO2), 0) AS CO2, 
        ROUND(SUM(CH4), 0) AS CH4, 
        ROUND(SUM(N2O), 0) AS N2O, 
        ROUND(SUM(TotalCO2e), 0) AS TotalCO2e,
        ROUND(SUM(TotalCO2eMetric), 0) AS TotalCO2eMetric, 
        2 AS zOrder
    FROM 
        vw_MobileData
    WHERE 
        CO2 IS NOT NULL
    GROUP BY 
        Year) Scope1 
GROUP BY 
    GHGType, Year, zorder

但是,与其汇总列,不如将它们与GHGType范围1重复。

3 个答案:

答案 0 :(得分:0)

我认为,如果先合并数据然后使用grouping sets,您会发现这更容易:

select coalesce(GHGType, 'Scope') as GhGType, Year,
       sum(CO2) as CO2, . . .
from ((select 'Natural Gas (Stationary)' AS GHGType, Year, CO2, CH4, N20,
              TotalCO2e, TotalCO2eMetric, 1 AS zOrder
       from vw_NaturalGas
       where CO2 is not null
      ) union all
      (select 'Mobile (Fleet)' AS GHGType, Year, CO2, CH4, N20,
              TotalCO2e, TotalCO2eMetric, 1 AS zOrder
       from vw_NaturalGas
       where CO2 is not null
      ) union all
      . . .
     ) v
group by grouping sets ( (GHGType, Year), (Year) )
order by zorder, year;

答案 1 :(得分:0)

我只是将您选择的数据转储到临时表中,并对所有汇总后的列求和,这将使您的数据更易于使用。

-m32

然后,它应该与基于#tempTable的查询插入数据一样容易。如果我了解您要执行的操作,这就是我会执行的操作。

create table #tempTable
(
    GHGType varchar(50),
    Year int,
    CO2 int,
    ....etc..
)

insert into #tempTable (GHGType, Year, CO2)
SELECT 
    'Natural Gas (Stationary)' AS GHGType, 
    Year,
    ROUND(SUM(CO2), 0) AS CO2, 
    ROUND(SUM(CH4), 0) AS CH4, 
    ROUND(SUM(N2O), 0) AS N2O, 
    ROUND(SUM(TotalCO2e), 0) AS TotalCO2e, 
    ROUND(SUM(TotalCO2eMetric), 0) AS TotalCO2eMetric, 
    1 AS zOrder
into #tempTable
FROM
    vw_NaturalGas
WHERE        
     CO2 IS NOT NULL
GROUP BY 
     Year

答案 2 :(得分:0)

SELECT
     'Natural Gas (Stationary)' + 'Mobile (Fleet)' AS GHGType
    ,temp.Year
    ,SUM(temp.CO2) AS CO2
    ,SUM(temp.CH4) AS CH4
    ,SUM(temp.N2O) AS N2O
    ,SUM(temp.TotalCO2e) AS TotalCO2e
    ,SUM(temp.TotalCO2eMetric) AS TotalCO2eMetric
FROM
  (
    SELECT 
         'Natural Gas (Stationary)' AS GHGType
        , Year
        , round(sum(CO2), 0) AS CO2
        , round(sum(CH4), 0) AS CH4
        , round(sum(N2O), 0) AS N2O
        , round(sum(TotalCO2e), 0) AS TotalCO2e
        , round(sum(TotalCO2eMetric), 0) AS TotalCO2eMetric
        , 1 AS zOrder
   FROM            
       vw_NaturalGas
   WHERE        
       CO2 IS NOT NULL
   GROUP BY Year

   UNION

   SELECT 
        'Mobile (Fleet)' AS GHGType
        , Year
        , round(sum(CO2), 0) AS CO2
        , round(sum(CH4), 0) AS CH4
        , round(sum(N2O), 0) AS N2O
        , round(sum(TotalCO2e), 0) AS TotalCO2e
        , round(sum(TotalCO2eMetric), 0) AS TotalCO2eMetric
        , 2 AS zOrder
   FROM 
        vw_MobileData
   WHERE 
       CO2 IS NOT NULL
   GROUP BY Year
  )
  AS temp
GROUP BY 
  temp.Year