建立会计试算平衡报告绩效问题

时间:2013-01-18 20:36:46

标签: sql database oracle date-arithmetic accounting

假设我们有日记帐分录表如下

create table jour_entries 
 (jseq number, 
  j_date date, 
  Eseq number, 
  account_no varchar2(32), 
  debit number, 
  credit number,
  note varchar2(256) );

如何在SQL中为试算平衡建立最佳性能报告?报告列是

  • account_number:是帐号。

  • debit_within_month:与account_number相关的所有借方的总和,从给定月份的第1个月到该月末(如果给定日期是当月,则直到当天)

  • credit_within_month:与account_number相关的所有信用额的总和,从给定月份的第1个月到该月末(或者当前日期,如果给定日期是当前月份)

  • debit_till_this_day:是指给定日期年份(从给定日期的1月1日到当天)与account_number相关的所有借方的累计金额。

  • credit_till_this_day:是指给定日期年份(从给定日期的1月1日到当天)与account_number相关的所有信用的累计金额。


我尝试了这个选择:

select account_number
      , debit_within_month
      , credit_within_month
      , debit_till_this_day
      , credit_till_this_day
from jour_entries j, 
    (select account_number, sum(debit) debit_within_month, 
                         sum(credit) credit_within_month
              from jour_entries 
              where j_date between trunc(given_date, 'month') and given_date
              group by account_number
    ) j1,
    (select account_number, sum(debit) debit_till_this_day,
                         sum(credit) credit_till_this_day  
              from jour_entries 
              where j_date between trunc(given_date, 'year') and given_date
              group by account_number
    ) j2
wherer j.account_number = j1.account_number
and j.account_number = j2.account_number

但我正在寻找其他解决方案(可能通过使用分析函数)以获得最佳性能。

2 个答案:

答案 0 :(得分:2)

我将使用SQL * Plus替换变量语法来指示given_month。对于您实际使用的客户,您需要更改此内容。

select account_number
       , sum ( case when J_date between trunc(&given_date, 'mm') 
                                    and least(sysdate, last_day(&given_date)) 
          then debit else 0 end ) as debit_within_month
       , sum ( case when J_date between trunc(&given_date, 'mm')
                                   and least(sysdate, last_day(&given_date)) 
            then credit else 0 end ) as credit_within_month
       , sum ( case when J_date between trunc(&given_date, 'yyyy') and sysdate)  
               then debit else 0 end ) as debit_til_this_day
      , sum ( case when J_date between trunc(&given_date, 'yyyy') and sysdate)  
              then credit else 0 end ) as credit_til_this_day
from jour_entries
group by account_number

<强>解释

    应用于日期的
  • trunc()会将其截断为给定的格式掩码。所以trunc(sysdate, 'mm')给出了本月的第一天,并且&#39; yyyy&#39;面具给了一年的第一个。
  • last_day()给出了呃,给定月份的最后一天。

答案 1 :(得分:0)

如果我理解你的问题,听起来像这样的事情对你有用:

SELECT JE.Account_no as Account__Number,
    SUM(JE2.debit) as Debit_Within_Month,
    SUM(JE2.credit) as Credit_Within_Month,
    SUM(JE.debit) as Debit_Till_This_Day,
    SUM(JE.credit) as Credit_Till_This_Day
FROM Jour_Entries JE
    LEFT JOIN (
        SELECT jseq, Account_No, Debit, Credit
        FROM Jour_Entries
        WHERE to_char(j_date, 'YYYY') = 2013 AND to_char(j_date, 'mm') = 2
    ) JE2 ON JE.jseq  = JE2.jseq 
WHERE to_char(j_date, 'YYYY') = 2013 
GROUP BY JE.Account_no

这是sql fiddle

祝你好运。