2 个独立的左连接查询不能一起工作

2 independent left join queries won't work together

最终我希望我的输出是一个类似于下面我正在做的table的数据透视查询

例如

      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

但是,为了达到这一点,我想在没有数据的地方用零填充我的数据集

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

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 个查询,其目的是填补数据中的空白。 IE。在该组合不存在的情况下,用零计数填充天数和状态。

  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 中加入它们时,它不会 运行 并出现以下错误。我尝试了几种不同的方法,但都不愉快。

Error report -

SQL Error: ORA-00904: "TD"."TMP_DATE": invalid identifier

  1. 00000 - "%s: invalid identifier"

查询 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;

也许像这样,在 from 子句中只有一个 table?

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;

试试这个,让我知道你遇到了什么错误

--
-- 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;

我决定以不同的方式解决这个问题,我使用数据透视来填充流程法规,而不是使用查询 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;