Full Outer Join、Coalesce 和 Group By(天哪!)

Full Outer Join, Coalesce, and Group By (Oh My!)

我将分两部分提出这个问题,因为我的逻辑可能有偏差,如果是这样的话,语法并不重要。

  1. 我有 10 个问题。每个查询 returns 月份、供应商和计数( 一些指标 )。查询使用各种表、连接等。并非所有 month/supplier 组合都存在于每个查询的输出中。我想将这些组合成一个数据集,可以在 Excel.
  2. 中导出和旋转

我希望输出如下所示:

Month | Supplier | Metric1 |Metric2 |..| Metric 10
2018-01 | Supp1 | _value_ | _value_ |...| _value_ |
2018-01 | Supp2 | NULL | _value_ |...| NULL

最好/最简单/最有效的方法是什么?

  1. 我已经尝试了各种方法来实现上述目标,但我似乎无法完全正确地理解语法。我想做一个非常简单的测试用例并以此为基础,但我在数据库上只有 select 权限,所以我无法测试它。我能够创建一个至少不会导致任何波浪形红色错误行的查询,但是将相同的逻辑应用于更大的问题是行不通的。

这是我得到的:

 create table test1(name varchar(20),credit int);
 insert into test1 (name, credit) values ('Ed',1),('Ann',1),('Jim',1),('Ed',1),('Ann',1);
 create table test2 (name varchar(10), debit int);
 insert into test2 (name, debit) values ('Ann',1),('Sue',1),('Sue',1),('Sue',1);

 select
    coalesce(a.name, b.name) as name,
    cred,
    deb
 from
    (select name, count(credit) as cred
        from test1 
        group by name) a
    full outer join 
    (select name, count(debit) as deb
        from test2
        group by name) b  on 
     a.name =b.name;

我走的路对吗?

更新:根据 Gordon 的输入,我在前两个查询中尝试了这个:

select Month, Supp,
    sum(case when which = 1 then metric end) as Exceptions,
    sum(case when which = 2 then metric end) as BackOrders
from (
        (
         select Month, Supp, metric, 1 as which
         from (
                select (convert(char(4),E.PostDateTime,120)+'-'+convert(char(2),E.PostDateTime,101)) as Month, E.TradingPartner as Supp, count(distinct(E.excNum)) as metric
                from db..TrexcMangr E
                where (E.DSHERep in ('AVR','BTB') OR E.ReleasedBy in ('AVR','BTB')) AND year(E.PostDateTime) >= '2018'
              ) a
         )

         union all

        (
         select Month, Supp, metric, 2 as which
         from (
                select (convert(char(4),T.UpdatedDateTime,120)+'-'+convert(char(2),T.UpdatedDateTime,101)) as Month, P.Supplier as Supp, count(*) as metric
                from db1..trordertext T
                    inner join mdid_Tran..trOrderPO P on P.PONum = T.RefNum
                where T.TextType = 'BO' AND (T.CreatedBy in ('AVR','BTB') OR T.UpdatedBy in ('AVR','BTB')) AND year(UpdatedDateTime) >=2018
              ) b
        )
     ) q
group by Month, Supp

...但是我得到了一组错误。

一种方法使用 union allgroup by:

select month, supplier,
       sum(case when which = 1 then metric end) as metric_01,
       sum(case when which = 2 then metric end) as metric_02,
       . . .
from ((select Month, Supplier, Metric, 1 as which
       from (<query1>) q
       . . .
      ) union all
      (select Month, Supplier, Metric, 2 as which
       from (<query2>) q
       . . .
      ) union all
      . . .
     ) q
group by month, supplier;
SELECT
    CalendarMonthStart,
    Supp,
    SUM(CASE WHEN metric_id = 1 THEN metric END) as Exceptions,
    SUM(CASE WHEN metric_id = 2 THEN metric END) as BackOrders
FROM
(
    SELECT
        DATEADD(month, DATEDIFF(month, 0, E.PostDateTime), 0)   AS CalendarMonthStart,
        E.TradingPartner            AS Supp,
        COUNT(DISTINCT(E.excNum))   AS metric,
        1                           AS metric_id
    FROM
        db..TrexcMangr E
    WHERE
        (      E.DSHERep    in ('AVR','BTB')
            OR E.ReleasedBy in ('AVR','BTB')
        )
        AND E.PostDateTime >= '2018-01-01'
    GROUP BY
        1, 2

    UNION ALL

    SELECT
        DATEADD(month, DATEDIFF(month, 0, T.UpdatedDateTime), 0)   AS CalendarMonthStart,
        T.UpdatedDateTime,
        P.Supplier                  AS Supp,
        COUNT(*)                    AS metric,
        2                           AS metric_id
    FROM
        db1..trordertext T
    INNER JOIN
        mdid_Tran..trOrderPO P
            ON  P.PONum = T.RefNum
    WHERE
        (      T.CreatedBy in ('AVR','BTB')
            OR T.UpdatedBy in ('AVR','BTB')
        )
        AND T.TextType = 'BO'
        AND T.UpdatedDateTime >= '2018-01-01'
    GROUP BY
        1, 2
)
   combined
GROUP BY
    CalendarMonthStart,
    Supp