【问题标题】:Is there a way to join the below 2 queries有没有办法加入以下2个查询
【发布时间】:2020-02-06 06:08:58
【问题描述】:

我正在尝试加入以下 2 个查询。虽然以下两个查询都使用相同的表,但我无法获得正确的结果。。

在此查询中,我正在检查表 1 中是否存在满足条件 m1.condition 为 1 的条目,并且对于该条目,对表 2 进行查询,即使 5 分钟后表 2 中也没有条目,然后获取该条目的计数。

您看到的 30 分钟的日期检查是获取表中半小时前处理的所有条目。

SELECT count(*) AS TOTALCOUNT, 
       SELECT TO_CHAR(amount, '$999,999,999,999,999.99') AS TOTALVALUE 
from table1 m1 
LEFT JOIN table2 m ON m.id=m1.id 
where m1.condition='1' 
  and amount BETWEEN 1000 and 25000 
  and (m1.DATE <= (select to_char((select systimestamp - interval '0 00:05' day to minute from dual),'dd-MON-yy HH.MI.SS AM TZD') from dual)) 
  and m1.DATE <= (select to_char((select systimestamp - interval '0 00:30' day to minute from dual),'dd-MON-yy HH.MI.SS AM TZD') from dual) 
  and m1.DATE <= systimestamp

类似地,在表 2 中的以下查询中存在一些条件.. 所以基于此我正在执行一些操作。

SELECT COALESCE(SUM (CASE when m.f = 'Converted' then 1 else 0 END),0) AS CCOUNT, 
       COALESCE(SUM (CASE when m.f = 'Do Not Convert' then 1 else 0 END),0) AS NCOUNT, 
       count(m. id) AS TOTALCOUNT, 
       TO_CHAR(COALESCE (SUM(CASE when m.f = 'C' then (amount) END),0), '$999,999,999,999,999.99') AS CONVERTED, 
       TO_CHAR(COALESCE (SUM(CASE when m.f = 'D' then (amount) END),0), '$999,999,999,999,999.99') AS NONCONVERTED, 
       TO_CHAR(COALESCE (SUM(CASE when m.f <> '0' then (amount) END),0), '$999,999,999,999,999.99') AS TOTAL 
FROM table1 ml 
JOIN table2 m ON m.id=ml.id 
             and amount BETWEEN 1000 and 25000 
             and m1.DATE <= (select to_char((select systimestamp - interval '0 30:00' day to minute from dual),'dd-MON-yy HH.MI.SS AM TZD') from dual) 
             and m1.DATE < systimestamp;

我必须结合上述两个查询..但我做不到。

【问题讨论】:

  • 阅读条件聚合。另外,请格式化您的问题,以便其他人可以阅读。
  • 类似下面的表2中的查询有一些条件。条件相差太大。第二个查询中的 WHERE 子句的一部分是否丢失了,所以第二个中的 WHERE 成为连接条件的一部分?
  • 第一个查询中的 5 分钟条件是多余的,因为至少 30 分钟前的行隐含至少 5 分钟前。为什么将m1.DATE 与字符串进行比较? m1.DATE 列的数据类型是什么? amount 列在哪个表中?
  • 这两个表有什么关系? ON m.id=m1.id 似乎没有多大意义。这些表不太可能共享相同的唯一 ID。那么,是ON m.id=m1.m_id 还是ON m.m1_id=m1.id?或者,换句话说:我可以每 m1 行获得超过 1 个 m 行吗?
  • 金额在表 1 中,条件没有不同。在第二个查询中,我试图根据表 2 中的决策条件从表 1 中获取行。一旦找到一个决定,我就会得到它的id,然后在表1中得到对应的值..

标签: sql oracle join left-join


【解决方案1】:

过滤器之间唯一真正的区别是m1.condition = '1',所以只需添加

   count(case condition when '1' then 1 end) as condition_1_totalcount, 
   sum(case condition when '1' then amount end) as condition_1_totalvalue 

到您的第二个查询。在我的简短测试中,它看起来不错:

-- sample data
with 
  table1(id, date_, condition) as (
    select 1, date '1990-02-19', '1' from dual union all
    select 2, date '1990-02-19', '7' from dual union all
    select 3, date '1990-02-19', '1' from dual union all
    select 4, date '1990-02-19', '1' from dual ),
  table2(id, f, amount) as (
    select 1, 'C', 3000 from dual union all
    select 2, 'D', 3500 from dual union all
    select 3, 'D', 4000 from dual union all
    select 4, 'D', 5000 from dual )

-- query
select sum (case when m.f = 'Converted' then 1 else 0 end) ccount, 
       sum (case when m.f = 'Do Not Convert' then 1 else 0 end) as ncount, 
       count(m.id) as totalcount, 
       sum(case when m.f = 'C' then (amount) end) as converted, 
       sum(case when m.f = 'D' then (amount) end) as nonconverted, 
       sum(case when m.f <> '0' then (amount) end) as total,
       count(case condition when '1' then 1 end) as condition_1_totalcount, 
       sum(case condition when '1' then amount end) as condition_1_totalvalue 
  from table1 ml
  join table2 m on m.id = ml.id 
  where ml.date_ <= systimestamp - interval '30' minute
    and amount between 1000 and 25000 

我不完全理解为什么这个5 min 部分出现在第一个查询中。超过 30 分钟的行也超过 5 分钟,所以它在 cmets 中是多余的。并且比当前时间戳更早。如果您需要以这种方式区分值,您也可以将此条件放在case whens 中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-27
    • 2021-12-15
    • 1970-01-01
    • 2021-08-19
    • 2022-01-05
    • 2014-02-21
    • 1970-01-01
    • 2016-11-29
    相关资源
    最近更新 更多