【问题标题】:db2 query for multiple casesdb2 查询多种情况
【发布时间】:2018-10-03 12:50:20
【问题描述】:

我在下面有三个查询(除了 WHERE 子句的第一行之外相同),它们都可以在我的脚本中完美运行。第一个查询客户的订单,第二个查询分配给代表的所有订单,第三个查询整个公司的所有订单周期。

同样,它们都可以在给定各自变量的情况下工作(所有变量都来自同一页面),但我正在尝试为所有 3 种情况填充表格中的列。

有没有一种方法可以组合这些并创建一个查询,为每个子句提供相同的值?

所以,我希望一个查询返回所有 6 列。这是在 db2 上运行的,所以我不知道最好的方法,但我可以创建一个更大的基于 CASE 的查询吗?

//query on orders for this customer
SELECT
    count(*) as sales_180Cust,
    180/count(*) as velocityCust

FROM orders g
    inner join dates i
    on g.date1 = i.acyyyymmdd
WHERE g.cust = $customer
AND g.frm = $frm
AND g.cvr = $cvr
AND g.clr = $clr
AND i.aciso between current_Date - 180 DAY AND current_Date;

//orders belonging to representative
SELECT
    count(*) as sales_180Rep,
    180/count(*) as velocityRep

FROM orders g
    inner join dates i
    on g.date1 = i.acyyyymmdd
WHERE g.rep = $rep
AND g.frm = $frm
AND g.cvr = $cvr
AND g.clr = $clr
AND i.aciso between current_Date - 180 DAY AND current_Date;

//query across ALL orders
SELECT
    count(*) as sales_180Company,
    180/count(*) as velocityCompany

FROM orders g
    inner join dates i
    on g.date1 = i.acyyyymmdd
WHERE g.frm = $frm
AND g.cvr = $cvr
AND g.clr = $clr
AND i.aciso between current_Date - 180 DAY AND current_Date;

【问题讨论】:

  • 样本数据和期望的结果真的很有帮助。
  • 我很快就能弄到这里
  • DDL 也会有所帮助
  • 你厌倦了UNION吗?!

标签: sql db2 case


【解决方案1】:

这将是另一种方法

WITH CTE AS (
    SELECT  g.cust
    ,       g.rep
    FROM orders g
        inner join dates i
        on g.date1 = i.acyyyymmdd
    WHERE 
        g.frm = $frm
    AND g.cvr = $cvr
    AND g.clr = $clr
)
SELECT
    count(*)     as sales_180Company
,   180/count(*) as velocityCompany
,   'cust' as query
FROM CTE
WHERE cust = $customer
UNION ALL
SELECT
    count(*)     as sales_180Company
,   180/count(*) as velocityCompany
,   'rep' as query
FROM CTE
WHERE rep = $rep
UNION ALL
SELECT
    count(*)     as sales_180Company
,   180/count(*) as velocityCompany
,   'all' as query
FROM CTE

返回例如

 SALES_180COMPANY VELOCITYCOMPANY QUERY
 ---------------- --------------- -----
                3              60 cust
                2              90 rep
                5              36 all

【讨论】:

    【解决方案2】:
    select q1.*, q2.*, q3.*
    from 
      (select count(*) as sales_180Cust,    180/count(*) as velocityCust    from table(values 1) t(i)) q1
    , (select count(*) as sales_180Rep,     180/count(*) as velocityRep     from table(values 1, 2) t(i)) q2
    , (select count(*) as sales_180Company, 180/count(*) as velocityCompany from table(values 1, 2, 3) t(i)) q3
    

    我修改了你的 FROM 和 WERE 子句来表达这个想法。

    【讨论】:

    • 我相信一旦我得到 WHERE 子句,这应该对我有用。谢谢!
    • 我正在尝试这个,但它不允许 t 令牌:SELECT q7.* from (select count(*) as sales_180Rep, 180/count(*) as velocityRep from gOrders g inner join dates i on g.extd1d = i.acyyyymmdd (WHERE g.cstnoc = :dealer_id AND g.framec = :frame AND g.covr1c = :cover AND g.colr1c = :color AND i.ACISO between current_Date - 180 DAY AND current_Date) t(i)) q7;
    • 您使用 Data Studio 吗?这可以帮助您使语法正确,因为它强调了无效的 SQL 片段。
    • SELECT q7.* from ( select count(*) as sales_180Rep, 180/count(*) as velocityRep from gOrders g inner join dates i on g.extd1d = i.acyyyymmdd WHERE g.cstnoc = :dealer_id AND g.framec = :frame AND g.covr1c = :cover AND g.colr1c = :color AND i.ACISO between current_Date - 180 DAY AND current_Date ) q7;
    【解决方案3】:

    有很多方法可以做到这一点。 UNION 将是一种显而易见的方式。 GROUPING SETs 更聪明一点。

    create table orders(date1 int, rep int, cust int,frm int, cvr int, clr int, aci int);
    create table dates(acyyyymmdd int, aciso date);
    create variable $frm int default 1;
    create variable $cvr int default 1;
    create variable $clr int default 1;
    create variable $customer int default 1;
    create variable $rep int default 1;
    insert into orders values (1,0,0,1,1,1,1), (1,1,1,1,1,1,1), (1,2,1,1,1,1,1), (1,3,1,1,1,1,1), (1,1,2,1,1,1,1);
    insert into dates values (1, current date); 
    

    然后这个

    SELECT
        count(*)     as sales_180Company
    ,   180/count(*) as velocityCompany
    ,   g.cust
    ,   g.rep 
    FROM orders g
        inner join dates i
        on g.date1 = i.acyyyymmdd
    WHERE 
        g.frm = $frm
    AND g.cvr = $cvr
    AND g.clr = $clr
    AND i.aciso between current_Date - 180 DAY AND current_Date
    GROUP BY GROUPING SETS ( (), (cust), (rep) )
    HAVING (cust = $customer AND rep is null) 
    OR     (cust is null AND rep = $rep)
    OR     (cust is null AND rep is null)
    

    给这个

     SALES_180COMPANY VELOCITYCOMPANY CUST REP
     ---------------- --------------- ---- ----
                    5              36 NULL NULL
                    3              60    1 NULL
                    2              90 NULL    1
    

    【讨论】:

    • 但这仍然只为一个选项提供销售/速度,我需要所有 3 个选项(总共 6 个)
    • 不,它会为您提供所有三个的结果,只是在 3 个单独的行中。前 2 列中有 6 个值。最后两列告诉你哪种情况是哪种情况
    【解决方案4】:

    或者这个...在 SQL 中通常有多种方式来处理事情

    WITH CTE AS (
        SELECT  g.cust
        ,       g.rep
        FROM orders g
            inner join dates i
            on g.date1 = i.acyyyymmdd
        WHERE 
            g.frm = $frm
        AND g.cvr = $cvr
        AND g.clr = $clr
    ) , CUST AS (
    SELECT
        count(*)     as sales_180Company
    ,   180/count(*) as velocityCompany
    FROM CTE
    WHERE cust = $customer
    ) , REP AS (
    SELECT
        count(*)     as sales_180Company
    ,   180/count(*) as velocityCompany
    FROM CTE
    WHERE rep = $rep
    ) , ALL AS (
    SELECT
        count(*)     as sales_180Company
    ,   180/count(*) as velocityCompany
    FROM CTE
    )
    SELECT * FROM CUST, REP, ALL
    

    重新调整

     SALES_180COMPANY VELOCITYCOMPANY SALES_180COMPANY VELOCITYCOMPANY SALES_180COMPANY VELOCITYCOMPANY
     ---------------- --------------- ---------------- --------------- ---------------- ---------------
                    3              60                2              90                5              36
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-25
      相关资源
      最近更新 更多