由于我正在使用的报告软件,我必须将所有内容放在一个sql语句中以用于报告。但是,Oracle SQL似乎不允许我使用单组结果创建内部选择,即使它在最终产品中只有1个结果。 (所有其他结果都是单组函数)。 这是我的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')
任何人都可以帮助我吗? 提前谢谢。
答案 0 :(得分:1)
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
为MAX()
添加total_receivables
以将其屏蔽为聚合函数!