循环 - SQL Developer

时间:2015-09-15 09:01:05

标签: mysql sql oracle-sqldeveloper

我在SQL数据库中有以下表,

Acct_Num           Date                Amount   Debit_Credit

001234          10/Jan/2014      56879     Credit

001234          15/Jan/2014      56879     Debit

001234          10/Feb/2014      10000    Credit

001234          09/Feb/2014       9000     Credit

001234          25/Mar/2014       19000     Credit

预期输出

Acct_no      Month        Consolidate Credit       Consolidate Debit   Balance 

001234       JAN                      56879          56879             0

001234       FEB                      19000           0                                19000

问题:

系统应该在一个月内单独合并借记/贷记。用户将输入日期(例如15.01.2014),系统应从开始月份开始收取借方和贷方的总和,即2014年1月至2014年,并且循环应以当前系统月结束,即9月 - 15日

2 个答案:

答案 0 :(得分:0)

您应该尝试避免在SQL中循环。 SQL基于离散数学,因此您应该总是尝试设置操作。

要解决您的问题,您应该按照您的帐号,日期和借方或信用类型列进行分组。在主要选择中,您应该将金额列汇总并将日期转换为月份值。在重叠超过一年时要小心,您可能希望在输出中添加年份列。

答案 1 :(得分:0)

这是一个纯SQL解决方案,包括基于您的数据的create table语句。

以下是设置:

CREATE TABLE Trans
    (Acct_Num varchar2(6), Tran_date date, Amount number, Debit_Credit varchar2(6))
;

INSERT ALL 
    INTO Trans (Acct_Num, Tran_date, Amount, Debit_Credit)
         VALUES ('001234', to_Date('10-Jan-2014','dd-mon-yyyy'), 56879, 'Credit')
    INTO Trans (Acct_Num, Tran_date, Amount, Debit_Credit)
         VALUES ('001234', to_Date('15-Jan-2014','dd-mon-yyyy'), 56879, 'Debit')
    INTO Trans (Acct_Num, Tran_date, Amount, Debit_Credit)
         VALUES ('001234', to_Date('10-Feb-2014','dd-mon-yyyy'), 10000, 'Credit')
    INTO Trans (Acct_Num, Tran_date, Amount, Debit_Credit)
         VALUES ('001234', to_Date('09-Feb-2014','dd-mon-yyyy'), 9000, 'Credit')
    INTO Trans (Acct_Num, Tran_date, Amount, Debit_Credit)
         VALUES ('001234', to_Date('25-Mar-2014','dd-mon-yyyy'), 19000, 'Credit')
SELECT * FROM dual;

和解决方案的三种变体。

选项1枢轴然后聚合:

with pvt as (
select *
  from trans
  pivot (sum(amount)
    FOR Debit_Credit IN ( 'Credit' AS Credit
                        , 'Debit'  AS Debit))
) 
select acct_num
     , trunc(tran_date, 'month') period_Start
     , sum(credit) consolidate_credit
     , sum(debit) consolidate_debit
     , nvl(sum(credit),0)
     - nvl(sum(debit),0) total
  from pvt
 group by acct_num
     , trunc(tran_date, 'month');

选项2预处理日期,然后让枢轴进行聚合:

with dta as (
  select acct_num, trunc(tran_date, 'month') tran_date
       , amount, Debit_Credit
    from trans
)
select acct_num
     , tran_date
     , credit consolidate_credit
     , debit consolidate_debit
     , nvl(credit,0)
     - nvl(debit,0) total
  from dta
 pivot (sum(amount)
   FOR Debit_Credit IN ( 'Credit' AS Credit
                        , 'Debit'  AS Debit));

选项3透视并汇总为一体:

select acct_num
     , trunc(tran_date, 'month')
     , sum(credit) consolidate_credit
     , sum(debit) consolidate_debit
     , nvl(sum(credit),0)
     - nvl(sum(debit),0) total
  from trans
 pivot (sum(amount)
   FOR Debit_Credit IN ( 'Credit' AS Credit
                        , 'Debit'  AS Debit))
 group by acct_num
     , trunc(tran_date, 'month');