【问题标题】:LAG and LEAD alternative in SQL syntaxSQL 语法中的 LAG 和 LEAD 替代方案
【发布时间】:2023-03-05 15:34:02
【问题描述】:

你好,

我正在编写一个 SQL 查询来导出期末余额,其值将用于计算下一行的期初余额,依此类推。我的表结构粘贴在上图中的代码如下:

SELECT
         t1."Document Date" as txn_dt,
         t1."GL Account Code" as gl_code,
         t1."GL Account Description" as cst_name,
         t1."Debit Amount" as debit,
         t1."Credit Amount" as credit,
         t2."Opening Balance" as op_bal,
         t1."Debit Amount" + t1."Credit Amount" + t2."Opening Balance" as closing_bal
FROM  "Trial Balance 1617" t1,
     "GL Master Account" t2 
WHERE    t1."GL Account Code"  = t2."GL Account Code"
 AND    t1."GL Account Code"  = 'A3010101B058'

请注意,ZOHO 报告中不支持 LAG 和 LEAD 有人可以指导我吗?

【问题讨论】:

  • 是否支持 row_number() ?
  • 样本数据和期望的结果会有很大帮助。
  • 而且,除了ROW_NUMBER(),您还可以使用 CTE 吗? (顺便说一句,您可能想激活您的 Windows ;)).

标签: sql sql-server database select zoho


【解决方案1】:

假设“试算表 1617”是一个汇总表,在 GL 帐户代码和文档日期上具有唯一索引,您可以手动计算导致特定条目的借方和贷方总和,如下所示:

create table "Trial Balance 1617" ("Document Date" date, "GL Account Code" varchar(25), 
    "Debit Amount" numeric(12,2), "Credit Amount" numeric(12,2))
create table "GL Master Account" ("GL Account Code" varchar(25), "Opening Balance" numeric(12,2))

insert into "GL Master Account" values ('A3010101B058', '8110339.14')
insert into "Trial Balance 1617" values ('4/1/2016', 'A3010101B058', 5332269.28, 0)
insert into "Trial Balance 1617" values ('4/2/2016', 'A3010101B058', 741674.9, 0)
insert into "Trial Balance 1617" values ('4/4/2016', 'A3010101B058', 570253.96, 0)
insert into "Trial Balance 1617" values ('4/5/2016', 'A3010101B058', 0, -148839.52)

SELECT
    t1."Document Date" as txn_dt,
    t1."GL Account Code" as gl_code,
    t1."Debit Amount" as debit,
    t1."Credit Amount" as credit,
    case 
    when not exists 
        (select * from "Trial Balance 1617" where "GL Account Code" = t1."GL Account Code" and "Document Date" < t1."Document Date")
            then t2."Opening Balance" 
    else
        t2."Opening Balance" + (select sum("Debit Amount") from "Trial Balance 1617" where "GL Account Code" = t1."GL Account Code" and "Document Date" < t1."Document Date")
            - (select sum("Credit Amount") from "Trial Balance 1617" where "GL Account Code" = t2."GL Account Code" and "Document Date" < t1."Document Date") 
            - (select sum("Credit Amount") from "Trial Balance 1617" where "GL Account Code" = t2."GL Account Code" and "Document Date" < t1."Document Date")

    end as op_bal,
    case 
    when not exists 
        (select * from "Trial Balance 1617" where "GL Account Code" = t1."GL Account Code" and "Document Date" < t1."Document Date")
            then t2."Opening Balance" 
    else
        t2."Opening Balance" + (select sum("Debit Amount") from "Trial Balance 1617" where "GL Account Code" = t1."GL Account Code" and "Document Date" < t1."Document Date")
            - (select sum("Credit Amount") from "Trial Balance 1617" where "GL Account Code" = t2."GL Account Code" and "Document Date" < t1."Document Date") 
            - (select sum("Credit Amount") from "Trial Balance 1617" where "GL Account Code" = t2."GL Account Code" and "Document Date" < t1."Document Date")

    end + t1."Debit Amount" + t1."Credit Amount" as closing_bal
FROM "Trial Balance 1617" t1
INNER JOIN "GL Master Account" t2 ON t1.[GL Account Code] = t2.[GL Account Code]
WHERE t1."GL Account Code"  = 'A3010101B058'

返回:

txn_dt      gl_code         debit       credit      op_bal      closing_bal
2016-04-01  A3010101B058    5332269.28  0.00        8110339.14  13442608.42
2016-04-02  A3010101B058    741674.90   0.00        13442608.42 14184283.32
2016-04-04  A3010101B058    570253.96   0.00        14184283.32 14754537.28
2016-04-05  A3010101B058    0.00        -148839.52  14754537.28 14605697.76

如果你可以使用 CTE,你可以通过在单独的查询中计算期初余额来清理它:

;with cte_op_balance as
(
    SELECT
         t1."Document Date" as txn_dt,
         t1."GL Account Code" as gl_code,
         case 
            when not exists 
                (select * from "Trial Balance 1617" where "GL Account Code" = t1."GL Account Code" and "Document Date" < t1."Document Date")
                    then t2."Opening Balance" 
            else
                t2."Opening Balance" + (select sum("Debit Amount") from "Trial Balance 1617" where "GL Account Code" = t1."GL Account Code" and "Document Date" < t1."Document Date")
                    - (select sum("Credit Amount") from "Trial Balance 1617" where "GL Account Code" = t2."GL Account Code" and "Document Date" < t1."Document Date") 
                    - (select sum("Credit Amount") from "Trial Balance 1617" where "GL Account Code" = t2."GL Account Code" and "Document Date" < t1."Document Date")

         end as op_bal
        FROM "Trial Balance 1617" t1
        INNER JOIN "GL Master Account" t2 ON t1.[GL Account Code] = t2.[GL Account Code]
        WHERE t1."GL Account Code"  = 'A3010101B058'
)
SELECT
         t1."Document Date" as txn_dt,
         t1."GL Account Code" as gl_code,
         t1."Debit Amount" as debit,
         t1."Credit Amount" as credit,
         c.op_bal,
         c.op_bal + t1."Debit Amount" + t1."Credit Amount" as closing_bal
FROM "Trial Balance 1617" t1
INNER JOIN "GL Master Account" t2 ON t1.[GL Account Code] = t2.[GL Account Code]
INNER JOIN cte_op_balance c ON t1.[GL Account Code] = c.gl_code and t1.[Document Date] = c.txn_dt
WHERE t1."GL Account Code"  = 'A3010101B058'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-14
    • 2018-12-12
    • 2016-09-19
    • 1970-01-01
    • 1970-01-01
    • 2021-01-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多