ORACLE - 如何根据特定字段

时间:2016-03-31 10:44:46

标签: sql oracle

我有一个ORACLE视图,必须根据此视图生成报告。这是View示例。

SELECT DATE, VOUCHER_NO, CURRENCY, AMOUNT, DESCRIPTION, JOURNAL_TYPE, COA_NO 
FROM VIEW_EXAMPLE

|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       |(NULL)|
|03/30/2016|0000000002|USD     |1500  |ABCD       |CREDIT      |150002|
|03/30/2016|0000000002|USD     |1000  |ABCD       |DEBIT       |530001|
|03/30/2016|0000000002|USD     |500   |ABCD       |DEBIT       |540002|
|03/31/2016|0000000003|USD     |4000  |ABCD       |CREDIT      |150003|
|03/31/2016|0000000003|USD     |2000  |ABCD       |DEBIT       |520002|
|03/31/2016|0000000003|USD     |1000  |ABCD       |DEBIT       |520004|
|03/31/2016|0000000003|USD     |500   |ABCD       |DEBIT       |520005|
|03/31/2016|0000000003|USD     |500   |ABCD       |DEBIT       |520010|

我必须通过DATECOA_NO作为参数获取记录,但正如您所看到的,COA_NO JOURNAL_TYPE DEBIT所在的COA_NO是不同。与参数匹配的CREDIT位于VOUCHER_NO。数据是基于SELECT DATE, VOUCHER_NO, CURRENCY, AMOUNT, DESCRIPTION, JOURNAL_TYPE, COA_NO FROM VIEW_EXAMPLE WHERE DATE = @date AND VOUCHER_NO IN (SELECT VOUCHER_NO FROM VIEW_EXAMPLE WHERE COA_NO = @coa_no) 的信用卡和借记卡对,一个信用卡可以有多个借记卡。

首先,我使用此查询:

COA_NO

如果我只想获得一个COA_NO,它就有效。但是,问题是我还需要获取特定DATE中的所有COA_NO并使用它来对数据进行分组(在C#中)。

我还尝试创建两个查询,首先在特定日期获取所有COA_NO,然后在该日期使用foreach COA_NO循环查询数据。但是,它非常慢,因为它必须在该日期每隔COA_NO查询一次数据库。

所以我认为我必须创建一个临时字段,其中包含|DATE |VOUCHER_NO|CURRENCY|AMOUNT|DESCRIPTION|JOURNAL_TYPE|COA_NO|TEMP_FIELD| |03/30/2016|0000000001|USD |2000 |ABCD |CREDIT |150001|150001 | |03/30/2016|0000000001|USD |2000 |ABCD |DEBIT |(NULL)|150001 | |03/30/2016|0000000002|USD |1500 |ABCD |CREDIT |150002|150002 | |03/30/2016|0000000002|USD |1000 |ABCD |DEBIT |530001|150002 | |03/30/2016|0000000002|USD |500 |ABCD |DEBIT |540002|150002 | |03/31/2016|0000000003|USD |4000 |ABCD |CREDIT |150003|150003 | |03/31/2016|0000000003|USD |2000 |ABCD |DEBIT |520002|150003 | |03/31/2016|0000000003|USD |1000 |ABCD |DEBIT |520004|150003 | |03/31/2016|0000000003|USD |500 |ABCD |DEBIT |520005|150003 | |03/31/2016|0000000003|USD |500 |ABCD |DEBIT |520010|150003 | 所有记录的信用,如下所示。

COA_NO

怎么做?或者是通过ORDER

对此数据进行分组的另一种方法

此外,我必须AMOUNT VOUCHER_NO这些数据sys.stdout.write('\x1b[<char>')而不会破坏[<row>;<column>H对。 (可选)

1 个答案:

答案 0 :(得分:2)

您只需要一个分析函数来确定计算列,例如:

with view_example 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, null 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, 530001 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, 540002 coa_no from dual union all
                      select to_date('31/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('31/03/2016', 'dd/mm/yyyy') dt, 3 voucher_no, 'USD' currency, 2000 amount, 'ABCD' description, 'DEBIT' journal_type, 520002 coa_no from dual union all
                      select to_date('31/03/2016', 'dd/mm/yyyy') dt, 3 voucher_no, 'USD' currency, 1000 amount, 'ABCD' description, 'DEBIT' journal_type, 520004 coa_no from dual union all
                      select to_date('31/03/2016', 'dd/mm/yyyy') dt, 3 voucher_no, 'USD' currency, 500 amount, 'ABCD' description, 'DEBIT' journal_type, 520005 coa_no from dual union all
                      select to_date('31/03/2016', 'dd/mm/yyyy') dt, 3 voucher_no, 'USD' currency, 500 amount, 'ABCD' description, 'DEBIT' journal_type, 520010 coa_no from dual)
-- the above subquery is used to mimic the data in your VIEW_EXAMPLE.
-- You wouldn't need it - just use the SQL below:
select dt,
       voucher_no,
       currency,
       amount,
       description,
       journal_type,
       coa_no,
       max(case when journal_type = 'CREDIT' then coa_no end) over (partition by voucher_no) related_credit_coa_no
from   view_example
order by voucher_no, journal_type, amount;

DT         VOUCHER_NO CURRENCY     AMOUNT DESCRIPTION JOURNAL_TYPE     COA_NO RELATED_CREDIT_COA_NO
---------- ---------- -------- ---------- ----------- ------------ ---------- ---------------------
30/03/2016          1 USD            2000 ABCD        CREDIT           150001                150001
30/03/2016          1 USD            2000 ABCD        DEBIT                                  150001
30/03/2016          2 USD            1500 ABCD        CREDIT           150002                150002
30/03/2016          2 USD             500 ABCD        DEBIT            540002                150002
30/03/2016          2 USD            1000 ABCD        DEBIT            530001                150002
31/03/2016          3 USD            4000 ABCD        CREDIT           150003                150003
31/03/2016          3 USD             500 ABCD        DEBIT            520010                150003
31/03/2016          3 USD             500 ABCD        DEBIT            520005                150003
31/03/2016          3 USD            1000 ABCD        DEBIT            520004                150003

N.B。如果借记可能与其信用的日期不同,那么如果您直接在上述查询中添加过滤日期,则最终可能会得到错误的结果 - 相反,您必须使用子查询,例如。

select *
from   (select dt,
               voucher_no,
               currency,
               amount,
               description,
               journal_type,
               coa_no,
               max(case when journal_type = 'CREDIT' then coa_no end) over (partition by voucher_no) related_credit_coa_no
        from   view_example)
where dt = :p_date
-- and related_credit_coa_no = :p_coa_no  -- if required
order by voucher_no, journal_type, amount;