合并三个查询/三个字段的值?

时间:2019-06-14 10:27:32

标签: oracle oracle-sqldeveloper

我需要创建一个查看以下内容的报告:

  • 下午5点之前完成的新订单
  • 下午5点以后的新订单
  • 当天完成订单

(当天分类为在下午5点之前下达并在晚上7点之前下达订单)

表格应采用以下格式:


日期-类型-前5个-后5个-总计-同一天

理想情况下,所有信息都应显示在一行上,每天应为两行,其中类型= CO的一行,而类型= DO的另一行。

当前,在运行查询时,它会返回包含其他行的数据,其中某些行包含空值。

我已经尝试过的方法是:

  • 按值简单分组
  • 创建三个查询并对结果运行联合
  • 每个案例的子查询

以下代码用于试图简单分组的查询。

SELECT
  COUNT(ORDER_ID),
  TO_CHAR(CREATION_DATE, 'DD/MM/YYYY'),
  CASE
    WHEN ORDER_TYPE = 31
    THEN 'CO'
    ELSE 'DO'
  END AS "TYPE" ,
  CASE
    WHEN TO_CHAR(CREATION_DATE, 'HH24') < 17
    THEN NVL(COUNT(ORDER_ID),0)
  END AS "Pre_5",
  CASE
    WHEN TO_CHAR(CREATION_DATE, 'HH24') >= 17
    THEN NVL(COUNT(ORDER_ID),0)
  END AS "After_5",
  CASE
    WHEN TO_CHAR(shipped_date, 'DD/MM/YYYY') = TO_CHAR(CREATION_DATE,
      'DD/MM/YYYY')
    AND TO_CHAR(CREATION_DATE, 'HH24') < 17
     AND TO_CHAR(shipped_date, 'HH24') <= 19
    THEN COUNT(ORDER_ID)
   END AS "SAME DAY"
FROM
  ORDER_HEADER
WHERE
   CLIENT_ID       = '*****'
 AND creation_date > sysdate -7
GROUP BY
  ORDER_TYPE,
  TO_CHAR(CREATION_DATE, 'DD/MM/YYYY'),
  TO_CHAR(CREATION_DATE, 'HH24'),
  TO_CHAR(SHIPPED_DATE, 'HH24'),
  TO_CHAR(SHIPPED_DATE, 'DD/MM/YYYY')

Pre_5的代码

SELECT
      COUNT(ORDER_ID) AS PRE_5,
      CASE
    WHEN ORDER_TYPE = 31
    THEN 'CO'
    ELSE 'DO'
  END AS "TYPE",
  TO_CHAR(CREATION_DATE, 'DD/MM/YYYY')  
    FROM
      ORDER_HEADER
    WHERE
      TO_CHAR(CREATION_DATE, 'HH24') < 17
    AND CLIENT_ID                    ='*****'
    AND creation_date                > sysdate -7
    GROUP BY ORDER_TYPE, TO_CHAR(CREATION_DATE, 'DD/MM/YYYY')
    ORDER BY TO_CHAR(CREATION_DATE, 'DD/MM/YYYY')

After_5的代码

SELECT
      COUNT(ORDER_ID) AS AFTER_5,
      CASE
    WHEN ORDER_TYPE = 31
    THEN 'CO'
    ELSE 'DO'
  END AS "TYPE",
  TO_CHAR(CREATION_DATE, 'DD/MM/YYYY')
    FROM
      ORDER_HEADER
    WHERE
      TO_CHAR(CREATION_DATE, 'HH24') >= 17
    AND CLIENT_ID                     ='*****'
    AND creation_date                 > sysdate -1
       GROUP BY ORDER_TYPE, TO_CHAR(CREATION_DATE, 'DD/MM/YYYY')
    ORDER BY TO_CHAR(CREATION_DATE, 'DD/MM/YYYY')

同一天的代码

SELECT
      COUNT(ORDER_ID) AS SAME_DAY,
      CASE
    WHEN ORDER_TYPE = 31
    THEN 'CO'
    ELSE 'DO'
  END AS "TYPE",
  TO_CHAR(CREATION_DATE, 'DD/MM/YYYY')
    FROM
      ORDER_HEADER
    WHERE
      TO_CHAR(shipped_date, 'DD/MM/YYYY' ) = TO_CHAR(CREATION_DATE,
      'DD/MM/YYYY')
    AND TO_CHAR(CREATION_DATE, 'HH24') < 17
    AND TO_CHAR(shipped_date, 'HH24') <= 19
    AND CLIENT_ID='*****' AND creation_date > sysdate -7
           GROUP BY ORDER_TYPE, TO_CHAR(CREATION_DATE, 'DD/MM/YYYY')
    ORDER BY TO_CHAR(CREATION_DATE, 'DD/MM/YYYY')

理想情况下,我需要将来自三个单独查询的信息显示在一行中,并按日期和类型分组。

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

使用CTE怎么样?这样的事情(为了简化它,我删除了用于这些查询的大部分代码):

with 
  pre_5 as
    (select count(Order_id) as pre_5, 
            type,
            creation_date
     from ...
    ),
  after_5 as
    (select count(order_id) as after_5,
            type,
            creation_date
     from ...
    ),
  same_day as
    (select count(order_id) as same_day,
            type,
            creation_date
     from ...
    )
select
  p.creation_date,
  p.type,
  sum(p.pre_5)    pre_5,
  sum(a.after_5)  after_5,
  sum(s.same_day) same_day
from pre_5 p join after_5  a on a.creation_date = p.creation_date
                            and a.type = p.type
             join same_day s on s.creation_date = p.creation_date
                            and s.type = p.type
group by p.creation_date,
         p.type;