You could join to the 2 tables separately (and GROUP BY
) and then join them together:
WITH dates (dat) AS
( SELECT
TO_DATE('02.05.2014','dd.mm.yyyy') + ROWNUM - 1 AS dat
FROM
all_objects
WHERE
ROWNUM <= TO_DATE('02.05.2014','dd.mm.yyyy')
- TO_DATE('02.05.2014','dd.mm.yyyy') + 1
)
, ret (dat, return_amount) AS
( SELECT
d.dat,
COALESCE(SUM(r.return_amount), 0) AS return_amount
FROM dates d
LEFT JOIN returns r ON r.RETURN_DATE = d.dat
GROUP BY
d.dat
)
, ord (dat, order_amount) AS
( SELECT
d.dat,
COALESCE(SUM(o.order_amount), 0) AS order_amount
FROM dates d
LEFT JOIN orders o ON o.ORDER_DATE = d.dat
GROUP BY
d.dat
)
SELECT
r.dat,
r.return_amount,
o.order_amount
FROM
ret r
JOIN ord o ON o.dat = r.dat
ORDER BY
r.dat ;
Or use inline subqueries:
SELECT
d.dat,
COALESCE( ( SELECT SUM(r.return_amount)
FROM returns r
WHERE r.dat = d.dat
), 0)
AS return_amount,
COALESCE( ( SELECT SUM(o.order_amount)
FROM orders o
WHERE o.dat = d.dat
), 0)
AS order_amount
FROM
( SELECT
TO_DATE('02.05.2014','dd.mm.yyyy') + ROWNUM - 1 AS dat
FROM
all_objects
WHERE
ROWNUM <= TO_DATE('02.05.2014','dd.mm.yyyy')
- TO_DATE('02.05.2014','dd.mm.yyyy') + 1
) d
ORDER BY
d.dat ;
very close only the GROUP BY Columns forgotten
Try this:
select
PTC_DIAGNOSIS.PATIENT_ID,
PT_BASIC.PATIENT_CODE,
PT_BASIC.NAME_FIRST,
PT_BASIC.NAME_LAST
from PTC_DIAGNOSIS
inner join PT_BASIC on PTC_DIAGNOSIS.PATIENT_ID=PT_BASIC.PATIENT_ID
where PTC_DIAGNOSIS.create_date>'12/01/2015'
group by
PTC_DIAGNOSIS.PATIENT_ID,
PT_BASIC.PATIENT_CODE,
PT_BASIC.NAME_FIRST,
PT_BASIC.NAME_LAST
having COUNT(*)=1
Best Answer
Something like this: