Oracle SQL 合并两个查询

时间:2021-08-01 22:51:40

标签: sql oracle

我有这个查询:

with rws as (
    select c.ID as P_ID, c.FIRST_NAME || ' ' || c.LAST_NAME as PATIENT, to_char(s.SESSION_DATE, 'MON-YYYY') as SESSION_MONTH,
    to_char(s.SESSION_DATE, 'DD-MM-YYYY') as SESSION_DATE,
        row_number () over (
            partition by CLIENTS_ID
            order by c.ID asc
        ) rn
    from   SESSIONS s inner join CLIENTS c
    on c.ID = s.CLIENTS_ID
    where c.ACTIVE = 2
)
  select * from rws
  where  rn <= 1
  order  by P_ID asc;

结果如下: enter image description here

我也有这个查询:

select c.ID as P_ID, c.FIRST_NAME || ' ' || c.LAST_NAME as PATIENT, count(s.Id) as Qty, sum(s.AMOUNT) as PAID,
sum(s.PRICE-s.AMOUNT) as Dif
from SESSIONS s inner join CLIENTS c
on c.ID = s.CLIENTS_ID
where c.ACTIVE =2
and s.STATUS = 5
group by c.ID, c.FIRST_NAME || ' ' || c.LAST_NAME
order by P_ID;

结果如下: enter image description here

两个查询的患者 ID 相同。 是否可以将这两个查询合二为一并得到这个结果?

enter image description here

感谢@Sam Ware,这是正确的查询:

with rws as (
    select p.ID as P_ID, p.FIRST_NAME || ' ' || p.LAST_NAME as PATIENT, to_char(s.SESSION_DATE, 'MON-YYYY') as SESSION_MONTH,
    to_char(s.SESSION_DATE, 'DD-MM-YYYY') as SESSION_DATE,
        row_number () over (
            partition by CLIENTS_ID
            order by p.ID asc
        ) rn
    from   SESSIONS s inner join CLIENTS p
    on p.ID = s.CLIENTS_ID
    where p.ACTIVE = 2
), transactions as (
    select p.ID as P_ID, count(s.Id) as Qty, sum(s.AMOUNT) as PAID,
        sum(s.PRICE-s.AMOUNT) as Dif
    from SESSIONS s inner join CLIENTS p
        on p.ID = s.CLIENTS_ID
    where p.ACTIVE =2
        and s.STATUS = 5
    group by p.ID
    order by P_ID
)
  select r.*, t.Qty, t.PAID, t.Dif
  from rws r
  inner join transactions t
    on r.p_id = t.p_id
  where r.rn <= 1
  order by r.P_ID asc;

2 个答案:

答案 0 :(得分:1)

with rws as (
    select c.ID as P_ID, c.FIRST_NAME || ' ' || c.LAST_NAME as PATIENT, to_char(s.SESSION_DATE, 'MON-YYYY') as SESSION_MONTH,
    to_char(s.SESSION_DATE, 'DD-MM-YYYY') as SESSION_DATE,
        row_number () over (
            partition by CLIENTS_ID
            order by c.ID asc
        ) rn
    from   SESSIONS s inner join CLIENTS c
    on c.ID = s.CLIENTS_ID
    where c.ACTIVE = 2
), paid_stats as (
    select c.ID as P_ID, c.FIRST_NAME || ' ' || c.LAST_NAME as PATIENT, count(s.Id) as Qty, sum(s.AMOUNT) as PAID,
        sum(s.PRICE-s.AMOUNT) as Dif
    from SESSIONS s inner join CLIENTS c
        on c.ID = s.CLIENTS_ID
    where c.ACTIVE =2
        and s.STATUS = 5
    group by c.ID, c.FIRST_NAME || ' ' || c.LAST_NAME
    order by P_ID
),
  select rws.*, paid_stats.Qty, paid_stats.PAID, paid_stats.Dif
  from rws
  join paid_stats 
    on rws.p_id = paid.p_id
  where  rn <= 1
  order  by P_ID asc;

答案 1 :(得分:0)

您没有提供任何样本数据,因此很难测试,但您可以在分析函数中使用条件聚合:

with rws as (
  select c.ID as P_ID,
         c.FIRST_NAME || ' ' || c.LAST_NAME as PATIENT,
         to_char(s.SESSION_DATE, 'MON-YYYY') as SESSION_MONTH,
         to_char(s.SESSION_DATE, 'DD-MM-YYYY') as SESSION_DATE,
         ROW_NUMBER()
           OVER (PARTITION BY CLIENTS_ID ORDER BY c.ID ASC) AS rn,
         COUNT(CASE s.STATUS WHEN 5 THEN s.ID)
           OVER (PARTITION BY c.ID) AS Qty,
         SUM(CASE s.STATUS WHEN 5 THEN s.AMOUNT)
           OVER (PARTITION BY c.ID) AS Paid,
         SUM(CASE s.STATUS WHEN 5 THEN s.PRICE - s.AMOUNT)
           OVER (PARTITION BY c.ID) AS DIF
  from   SESSIONS s
         inner join CLIENTS c
         on c.ID = s.CLIENTS_ID
  where c.ACTIVE = 2
)
SELECT *
FROM   rws
WHERE  rn = 1
ORDER BY P_ID asc;

注意:这里假设 c.ID 是主键,不需要在 PARTITION BY 子句中包含名称;如果有必要,那么您需要将其添加到[并查看您的数据模型,看看它是否可以改进])。