I have to put everything in one sql statement for a report because of the reporting software I'm using. However, Oracle SQL doesn't seem to be letting me create an inner select with a single-group result even though it has only 1 result in the end product. (All the other results are single-group functions). Here's my SQL:
select
COUNT(*) as TOTAL_RXS,
SUM(rx_tx.brand_acquisition_cost) as TOTAL_COST,
SUM(case rx_tx.drug_dispensed
when 'B' then (rx_tx.brand_price - rx_tx.brand_discount)
when 'G' then (rx_tx.generic_price - rx_tx.generic_discount)
end)
as TOTAL_PRICE,
SUM(case rx_tx.drug_dispensed
when 'B' then (rx_tx.brand_price - rx_tx.brand_discount - rx_tx.brand_acquisition_cost)
when 'G' then (rx_tx.generic_price - rx_tx.generic_discount - rx_tx.brand_acquisition_cost)
end)
as TOTAL_PROFIT
, (select
SUM(tx_tp.balance_due_from_tp)
from eps2.tx_tp
join eps2.rx_tx on rx_tx.id = tx_tp.rx_tx_id
where 1=1
-- This results in CLAIM STATUS being 'F'
AND rx_tx.fill_date is not NULL
AND rx_tx.returned_date is not NULL
AND rx_tx.reportable_sales_date is NULL
AND (tx_tp.paid_status like '%PAID%'
OR tx_tp.paid_status like '%PART%') ) as TOTAL_RECEIVABLES
from eps2.vw_rx_summary join eps2.rx_tx
on vw_rx_summary.last_filled_rx_tx_id = rx_tx.id
-- where TRUNC(rx_tx.fill_date,'DD') = TRUNC(SYSDATE - 1,'DD')
Can anyone help me? Thanks in advance.
select COUNT(*) as TOTAL_RXS,
SUM(rx_tx.brand_acquisition_cost) as TOTAL_COST,
MAX(select
SUM(tx_tp.balance_due_from_tp) as total_receivables
from eps2.tx_tp
join eps2.rx_tx on rx_tx.id = tx_tp.rx_tx_id
where 1=1
AND (tx_tp.paid_status like '%PAID%'
OR tx_tp.paid_status like '%PART%') ) as total_receivables
Adding a MAX()
for total_receivables
to mask it as a aggregate function!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.