Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 建立会计跟踪余额报告绩效问题_Sql_Database_Oracle_Date Arithmetic_Accounting - Fatal编程技术网

Sql 建立会计跟踪余额报告绩效问题

Sql 建立会计跟踪余额报告绩效问题,sql,database,oracle,date-arithmetic,accounting,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中为试算平衡构建最佳性能报告?报告列为 帐号:是帐号 月内借方:指从给定月1日起至该月底(或如果给定日期在当月,则截至当日)与账号相

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

create table jour_entries 
 (jseq number, 
  j_date date, 
  Eseq number, 
  account_no varchar2(32), 
  debit number, 
  credit number,
  note varchar2(256) );
如何在SQL中为试算平衡构建最佳性能报告?报告列为

  • 帐号:是帐号

  • 月内借方:指从给定月1日起至该月底(或如果给定日期在当月,则截至当日)与账号相关的所有借方的总和

  • 月内贷记:是从给定月1日开始到该月底(或如果给定日期为本月,则截至当日)与账号相关的所有贷记的总和

  • 借方至本日:指给定日期年份内(从给定日期的1月1日起至当日)与账号相关的所有借方的累计总和

  • 信用证到期日:指给定日期年份内(从给定日期的1月1日起至当日)与账号相关的所有信用证的累计金额


我尝试了以下选择:

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

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

如果我理解您的问题,听起来这样的解决方案应该适合您:

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*Plus替换变量语法来表示给定的月份。您需要为实际使用的任何客户端更改此选项

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')
    给出了月份的第一个,而'yyyy'掩码给出了一年的第一个
  • last_day()
    给出给定月份的最后一天

您已经尝试了什么?除了性能之外,当给定日期不在当前月份时,您的解决方案不会给出正确的月度合计结果。您的TIL当前总计逻辑也有问题。好的,如果我们是在2012年6月,并且用户输入的日期是“2012年5月31日”,那么报告应该给出每个账号的所有交易(借记、贷记)都发生在5月,以及从2012年初到5月底的所有交易。这就是方法。对于试算表,您需要分类账表中的每一行,因为资产负债表项目将是“截止日期”余额。需要每一行意味着完全扫描将比索引查找更有效,并且您唯一的选择是按照这里的建议尝试只扫描一次表。