我下面有两个子查询来获取同一列的总数。现在,我要获取的是我使用的两个子查询的总数。我该怎么办?
Subquery1
(select sum(aila.total_rec_tax_amount)
from ap_invoice_lines_all aila
where aila.invoice_id = aia.invoice_id
and aila.line_type_lookup_code = 'ITEM'
and aila.tax_classification_code = 'VAT12 SERVICES'
group by aila.invoice_id) as total_tax_charged1
Subquery2
(select sum(aila.total_rec_tax_amount)
from ap_invoice_lines_all aila
where aila.invoice_id = aia.invoice_id
and aila.line_type_lookup_code = 'ITEM'
and aila.tax_classification_code = 'VAT12 GOODS'
group by aila.invoice_id) as total_tax_charged2
答案 0 :(得分:0)
您可以使用“全部并集”,然后计算总和:
select sum( as total_tax_charged)
from(
select sum(aila.total_rec_tax_amount) as total_tax_charged
from ap_invoice_lines_all aila
where aila.invoice_id = aia.invoice_id
and aila.line_type_lookup_code = 'ITEM'
and aila.tax_classification_code = 'VAT12 SERVICES'
group by aila.invoice_id
Union all
select sum(aila.total_rec_tax_amount)
from ap_invoice_lines_all aila
where aila.invoice_id = aia.invoice_id
and aila.line_type_lookup_code = 'ITEM'
and aila.tax_classification_code = 'VAT12 GOODS'
group by aila.invoice_id
)
您也可以使用一个选择语句来完成
select sum(aila.total_rec_tax_amount)
from ap_invoice_lines_all aila
where aila.invoice_id = aia.invoice_id
and aila.line_type_lookup_code = 'ITEM'
and aila.tax_classification_code in( 'VAT12 SERVICES', 'VAT12 GOODS')
group by aila.invoice_id