2016-04-01 42 views
0

我有一個這樣的表。ORACLE - 自定義ORDER BY來訂購數據行對

|DATE  |VOUCHER_NO|CURRENCY|AMOUNT|DESCRIPTION|JOURNAL_TYPE|COA_NO | 

|03/30/2016|0000000001|USD  |2000 |ABCD  |CREDIT  |150001 | 
|03/30/2016|0000000001|USD  |2000 |ABCD  |DEBIT  |150001 | 
|03/30/2016|0000000002|USD  |1500 |ABCD  |CREDIT  |150002 | 
|03/30/2016|0000000002|USD  |1000 |ABCD  |DEBIT  |150002 | 
|03/30/2016|0000000002|USD  |500 |ABCD  |DEBIT  |150002 | 
|03/30/2016|0000000003|USD  |4000 |ABCD  |CREDIT  |150003 | 
|03/30/2016|0000000003|USD  |2000 |ABCD  |DEBIT  |150003 | 
|03/30/2016|0000000003|USD  |1000 |ABCD  |DEBIT  |150003 | 
|03/30/2016|0000000003|USD  |500 |ABCD  |DEBIT  |150003 | 
|03/30/2016|0000000003|USD  |500 |ABCD  |DEBIT  |150003 | 

我必須按最大金額排序表,首先使用日記帳類型信用。並且它必須跟隨以Voucher_No爲基礎的借記(不涉及借方的金額),然後是第二大金額,如此。

|DATE  |VOUCHER_NO|CURRENCY|AMOUNT|DESCRIPTION|JOURNAL_TYPE|COA_NO | 

|03/30/2016|0000000003|USD  |4000 |ABCD  |CREDIT  |150003 | 
|03/30/2016|0000000003|USD  |2000 |ABCD  |DEBIT  |150003 | 
|03/30/2016|0000000003|USD  |1000 |ABCD  |DEBIT  |150003 | 
|03/30/2016|0000000003|USD  |500 |ABCD  |DEBIT  |150003 | 
|03/30/2016|0000000003|USD  |500 |ABCD  |DEBIT  |150003 | 
|03/30/2016|0000000001|USD  |2000 |ABCD  |CREDIT  |150001 | 
|03/30/2016|0000000001|USD  |2000 |ABCD  |DEBIT  |150001 | 
|03/30/2016|0000000002|USD  |1500 |ABCD  |CREDIT  |150002 | 
|03/30/2016|0000000002|USD  |1000 |ABCD  |DEBIT  |150002 | 
|03/30/2016|0000000002|USD  |500 |ABCD  |DEBIT  |150002 | 

如何做到這一點?如果我先按金額訂購,則借記卡將分散至下方,如果我先按voucher_no訂購,則不會按金額排序。

回答

4

可以ORDER BY一個組和第一,這樣

ORDER BY 
    MAX(AMOUNT) OVER (PARTITION BY VOUCHER_NO) DESC, -- voucher with highest amount first 
    VOUCHER_NO, -- all rows of that voucher 
    CASE WHEN JOURNAL_TYPE = 'CREDIT' THEN 0 ELSE 1 END, -- credit first 
    AMOUNT DESC 
+0

這是否會在'debit'之前爲特定優惠券排序'credit'? –

+0

@sql_dummy:如果您首先需要「credit」,則必須添加另一列,然後編輯我的答案。 – dnoeth

+0

他用最大的金額要求代金券。他詢問信用量最大的代金券。 –

0

我不知道,但一旦檢查此

 SELECT * from ... ORDER BY JOURNAL_TYPE asc, AMOUNT asc; 

EDIT1:-Misunderstood你的問題,讓我重新編輯我的答案。
EDIT2: - 正確的答案是已經張貼由用戶dnoeth
注: - 這是不正確的答案

2

這應該做的伎倆:

with your_table as (select to_date('30/03/2016', 'dd/mm/yyyy') dt, 1 voucher_no, 'USD' currency, 2000 amount, 'ABCD' description, 'Credit' journal_type, 150001 coa_no from dual union all 
        select to_date('30/03/2016', 'dd/mm/yyyy') dt, 1 voucher_no, 'USD' currency, 2000 amount, 'ABCD' description, 'Debit' journal_type, 150001 coa_no from dual union all 
        select to_date('30/03/2016', 'dd/mm/yyyy') dt, 2 voucher_no, 'USD' currency, 1500 amount, 'ABCD' description, 'Credit' journal_type, 150002 coa_no from dual union all 
        select to_date('30/03/2016', 'dd/mm/yyyy') dt, 2 voucher_no, 'USD' currency, 1000 amount, 'ABCD' description, 'Debit' journal_type, 150002 coa_no from dual union all 
        select to_date('30/03/2016', 'dd/mm/yyyy') dt, 2 voucher_no, 'USD' currency, 500 amount, 'ABCD' description, 'Debit' journal_type, 150002 coa_no from dual union all 
        select to_date('30/03/2016', 'dd/mm/yyyy') dt, 3 voucher_no, 'USD' currency, 4000 amount, 'ABCD' description, 'Credit' journal_type, 150003 coa_no from dual union all 
        select to_date('30/03/2016', 'dd/mm/yyyy') dt, 3 voucher_no, 'USD' currency, 2000 amount, 'ABCD' description, 'Debit' journal_type, 150003 coa_no from dual union all 
        select to_date('30/03/2016', 'dd/mm/yyyy') dt, 3 voucher_no, 'USD' currency, 1000 amount, 'ABCD' description, 'Debit' journal_type, 150003 coa_no from dual union all 
        select to_date('30/03/2016', 'dd/mm/yyyy') dt, 3 voucher_no, 'USD' currency, 500 amount, 'ABCD' description, 'Debit' journal_type, 150003 coa_no from dual union all 
        select to_date('30/03/2016', 'dd/mm/yyyy') dt, 3 voucher_no, 'USD' currency, 500 amount, 'ABCD' description, 'Debit' journal_type, 150003 coa_no from dual) 
-- End of mimicking your table with data in. See SQL below: 
select * 
from your_table 
order by max(amount) over (partition by voucher_no) desc, 
     journal_type, 
     amount desc; 

DT   VOUCHER_NO CURRENCY  AMOUNT DESCRIPTION JOURNAL_TYPE  COA_NO 
---------- ---------- -------- ---------- ----------- ------------ ---------- 
30/03/2016   3 USD   4000 ABCD  Credit   150003 
30/03/2016   3 USD   2000 ABCD  Debit   150003 
30/03/2016   3 USD   1000 ABCD  Debit   150003 
30/03/2016   3 USD    500 ABCD  Debit   150003 
30/03/2016   3 USD    500 ABCD  Debit   150003 
30/03/2016   1 USD   2000 ABCD  Credit   150001 
30/03/2016   1 USD   2000 ABCD  Debit   150001 
30/03/2016   2 USD   1500 ABCD  Credit   150002 
30/03/2016   2 USD   1000 ABCD  Debit   150002 
30/03/2016   2 USD    500 ABCD  Debit   150002 

NB,如果你要在子查詢中使用此查詢,顯然,您必須將max(amount) over (partition by voucher_no)添加到選擇列列表中,以便將該列公開給外部查詢。


ETA。如果你可以的情況下借方金額均高於貸款金額越大,你行的最高授信額度的優惠券代碼之後是首先要顯示,然後改變:

max(amount) over (partition by voucher_no) 

max(case when journal_type = 'Credit' then amount end) over (partition by voucher_no) 
+0

他用最大的金額要求代金券。他詢問信用量最大的代金券。 –

+0

只假設案件有'voucher1借記4000'也'voucher2貸記3000'根據他'voucher2'必須上面'voucher1' –

+0

我工作的假設a)每個憑證代碼只有一個信用和b)借方金額永遠不能超過貸方金額。如果需要,我已經更新了我的答案以增加額外的限制。 – Boneist