2016-08-22 6 views
2

より大きな金額の転記キーを表示したい(私はそれを得るためにMAXコマンドを使用した)。より大きい金額の表示方法

私が添付した画像では、同時に同じ値を持つレコードをグループ化する必要があります。これは、より高い金額をより少ない金額に引きますが、ここではより高い金額のposting_keyを表示する必要があります。

POSTING_KEY

SELECT PUBLICATION_CODE, 
    RS_GL_ACCT_NO, 
    ASSIGNMENT, 
    TEXT, 
    RRAC_TYPE, 
    MAX(INV_TAX_AMT)-MIN(INV_TAX_AMT) AS INV_TAX_AMT, 
    RS_AMOUNT, 
    POSTING_KEY 
FROM SAP_TABLE 
GROUP BY PUBLICATION_CODE, 
    RS_GL_ACCT_NO, 
    ASSIGNMENT, 
    TEXT, 
    RRAC_TYPE, 
    RS_AMOUNT, 
    POSTING_KEY; 
+0

単純なクエリは大丈夫だと思うし、必要でないSPでそれを書きます。 – user3664645

答えて

1

これを試してみてください:

select 
    a.PUBLICATION_CODE, 
    a.RS_GL_ACCT_NO, 
    a.TEXT, 
    a.RRAC_TYPE, 
    a.INV_TAX_AMT-b.INV_TAX_AMT AS INV_TAX_AMT, 
    a.RS_AMOUNT, 
    c.POSTING_KEY 
from sap_Table a 
join sap_Table b on (a.RS_GL_ACCT_NO = b.RS_GL_ACCT_NO and b.INV_TAX_AMT < a.INV_TAX_AMT) 
join (select RS_GL_ACCT_NO, max(posting_key) as posting_key from sap_Table) c on (a.RS_GL_ACCT_NO = b.RS_GL_ACCT_NO) 

は何が失敗した場合、私に教えてください:)

1

あなたは分析関数に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 
関連する問題