2个独立的左联接查询将无法一起工作

时间:2018-12-20 21:11:51

标签: sql left-join sqlplus

最终,我希望我的输出成为一个透视查询,类似于下面我很愿意做的事情

例如

      Date CO  RU ER AB

    1/1/18  5  20  0  0

    2/1/18  0   5  0  0

    3/1/18  0   0  0  0

    4/1/18  1   0  0  0

但是,要达到这一点,我要用零填充不存在数据的数据集

该表保存的数据类似于以下内容

Date/time Process_type Status

1/1/18 10:05 150 RU
2/1/18 14:00 150 CO
4/1/18 18:00 100 ER

在任何一天都不会有任何处理。

我编写了2个查询,其目的是填补数据中的空白。即。在不存在该组合的情况下,使用零计数填充日期和状态。

  1. 这是一个日期范围查询,可确保如果没有信息的日期的计数为零,则返回零。这将提供类似于下面的内容

    Date     Count
    
    1/1/18    25
    2/1/18     5
    3/1/18     0
    4/1/18     1
    
  2. 这是一个状态查询,可确保如果不存在零则返回零。

    status count
    
    AB 0
    RU 2
    CO 25
    ER 0
    

我想加入2个查询,以便如果计数为零,则日期和状态都将为零。

Date Status Count

1/1/18 AB 0
1/1/18 CO 0
1/1/18 ER 0
1/1/18 RU 0
2/1/18 AB 0
2/1/18 CO 6
3/1/18 ER 0
4/1/18 RU 1

当我将它们加入查询3时,它将无法运行并出现以下错误。我尝试了几种不同的方法,都没有喜悦。

  

错误报告-

     

SQL错误:ORA-00904:“ TD”。“ TMP_DATE”:无效的标识符

     
      
  1. 00000-“%s:无效的标识符”
  2.   

查询1

-- 
-- Working out dates with nulls if zero count
--
with tmp_dates as (
    select trunc(sysdate) - level + 1 as tmp_date
    from dual
    connect by level <= 5
)
select
    count(pi.crtd_tstmp),
    td.tmp_date
from 
    tmp_dates td
    left join procedure_instance pi 
        on (td.tmp_date = trunc(pi.crtd_tstmp) and proc_oid = 150)
group by 
    td.tmp_date
order by 
    tmp_date;

查询2

--
-- Working with Categories with zero if no category
--
with status_table as (
    select 'CO' as instanceid from dual union
    select 'RU' as instanceid from dual union
    select 'ER' as instanceid from dual union
    select 'AB' as instanceid from dual
)
select
    count(pi.crtd_tstmp),
    st.instanceid
from 
    status_table st
    left join procedure_instance pi
        on (st.instanceid = pi.stat and proc_oid = 150)
group by 
    st.instanceid
order by
    st.instanceid;

查询3

--
-- join together
--
with tmp_dates as (
    select trunc(sysdate) - level + 1 as tmp_date
    from dual
    connect by level <= 5
),
status_table as (
    select 'CO' as instanceid from dual union
    select 'RU' as instanceid from dual union
    select 'ER' as instanceid from dual union
    select 'AB' as instanceid from dual
)
select
    count(pi.crtd_tstmp),
    td.tmp_date,
    st.instanceid
from 
    tmp_dates td,
    status_table st
    left join procedure_instance pi 
        on (td.tmp_date = trunc(pi.crtd_tstmp) and proc_oid = 150)
    left join procedure_instance pi 
        on (st.instanceid = pi.stat and proc_oid = 150)
group by 
    td.tmp_date,
    st.instanceid
order by 
    tmp_date;

3 个答案:

答案 0 :(得分:0)

也许这样的话在from子句中只有一个表?

with tmp_dates as (
    select trunc(sysdate) - level + 1 as tmp_date
    from dual
    connect by level <= 5
),status_table as (select 'CO' as instanceid from dual union
select 'RU' as instanceid from dual union
select 'ER' as instanceid from dual union
select 'AB' as instanceid from dual)
select count(pi.crtd_tstmp), td.tmp_date, st.instanceid
from tmp_dates td
left join procedure_instance pi on (td.tmp_date = trunc(pi.crtd_tstmp) and proc_oid = 150)
right join status_table st on (st.instanceid = pi.stat and proc_oid = 150)
group by td.tmp_date,st.instanceid
order by tmp_date;

答案 1 :(得分:0)

我决定以另一种方式解决这个问题,我使用数据透视表来填充流程法规,而不是使用查询2和第二个联接。现在完全可以按需运行了。

select Process_created, 
    nvl(Complete, 0) as Complete,
    nvl(Error, 0) as Error,
    nvl(Running, 0) as Running,
    nvl(Abort, 0) as Abort
       from(
with tmp_dates as (
    select trunc(sysdate) - level + 1 as tmp_date
    from dual
    connect by level <= 30
)
select
    count(pi.crtd_tstmp) as number_of,
    td.tmp_date as Process_created,
    pi.stat as status
from 
    tmp_dates td
    left join procedure_instance pi 
        on (td.tmp_date = trunc(pi.crtd_tstmp) and proc_oid = 150)
group by 
    td.tmp_date,pi.stat
order by 
    tmp_date)src
    pivot(
sum(number_of)
for status in ('CO' as Complete, 'ER' as Error, 'RU' as Running, 'AB' as Abort )) piv order by process_created;

答案 2 :(得分:-1)

尝试一下,让我知道您遇到什么错误

--
-- join together
--
WITH tmp_dates
AS (
    SELECT trunc(sysdate) - LEVEL + 1 AS tmp_date
    FROM dual connect BY LEVEL <= 5
    )
    ,status_table
AS (
    SELECT 'CO' AS instanceid FROM dual

    UNION

    SELECT 'RU' AS instanceid FROM dual

    UNION

    SELECT 'ER' AS instanceid FROM dual

    UNION

    SELECT 'AB' AS instanceid FROM dual
    )
SELECT count(pi.crtd_tstmp)
    ,td.tmp_date
    ,st.instanceid
FROM tmp_dates td
    ,status_table st
LEFT JOIN procedure_instance pi ON (
        td.tmp_date = trunc(pi.crtd_tstmp)
        AND proc_oid = 150
        )
LEFT JOIN procedure_instance pi2 ON (
        st.instanceid = pi2.stat
        AND proc_oid = 150
        )
GROUP BY td.tmp_date
    ,st.instanceid
ORDER BY td.tmp_date;