您可以使用分析功能row_number()
,min()
和max()
:
select p_code, gl_code, text, type, tax_amt, invoice_amt, posting_key
from (
select row_number() over (partition by p_code, gl_code order by tax_amt desc) as rn,
max(tax_amt) over (partition by p_code, gl_code)
- min(tax_amt) over (partition by p_code, gl_code) as tax_amt,
p_code, gl_code, text, type, invoice_amt, posting_key
from sap_table)
where rn = 1
測試數據和輸出:
create table sap_table (p_code varchar2(2), gl_code number(6), text varchar2(25),
type varchar2(20), tax_amt number(8, 2), invoice_amt number(6), posting_key number(6));
insert into sap_table values ('LH', 160069, 'Prepaid Charge-Out 0916',
'Tax For Charge', .96, 0, 53);
insert into sap_table values ('LH', 160069, 'Prepaid Charge-Out 0916',
'Tax For Charge', .5, 0, 54);
P_CODE GL_CODE TEXT TYPE TAX_AMT INVOICE_AMT POSTING_KEY
------ ------- ------------------------- --------------- ---------- ----------- -----------
LH 160069 Prepaid Charge-Out 0916 Tax For Charge 0,46 0 53
簡單的查詢就可以了,並把它寫在SP是沒有必要的。 – user3664645