【问题标题】:Opening and Closing Balance is wrt Debit and Credit amount期初和期末余额是借方和贷方金额
【发布时间】:2018-07-09 16:57:43
【问题描述】:

我在这里的某个人的帮助下编写了这个表结构和查询,它运行良好。我想获得日期之间的期初和期末余额。我已经评论了我想要获取日期的日期,如果我运行该日期,请检查我想要的预期输出如下所示。

这是结构和示例数据:

DROP TABLE [TransactionMaster];
DROP TABLE [VoucherType];

CREATE TABLE [VoucherType](
    [VoucherTypeCode] [tinyint] NOT NULL PRIMARY KEY,
    [FullName] [nvarchar](255) NOT NULL
);


INSERT INTO [VoucherType] VALUES (1, 'Cash Payment Voucher');
INSERT INTO [VoucherType] VALUES (2, 'Cash Receipt Voucher');
INSERT INTO [VoucherType] VALUES (3, 'Bank Payment Voucher');
INSERT INTO [VoucherType] VALUES (4, 'Bank Receipt Voucher');


CREATE TABLE [TransactionMaster](
    [ID] [bigint] NOT NULL PRIMARY KEY,
    [VoucherTypeCode] [tinyint] NOT NULL,
    [PayeeName] [varchar](255) NOT NULL,
    [TransactionDate] datetime,
    [RefNo] [nvarchar](50) NULL
    CONSTRAINT [FK_tbl_TransactionMaster_tbl_VoucherType] FOREIGN KEY([VoucherTypeCode])
    REFERENCES [VoucherType] ([VoucherTypeCode])
)



INSERT INTO [TransactionMaster] VALUES (1, 2, 'Asim', '2018-03-21', 'CRV-0001-LHR');
INSERT INTO [TransactionMaster] VALUES (2, 4, 'Ali', '2018-03-21', 'BRV-2421-KHI');
INSERT INTO [TransactionMaster] VALUES (3, 1, 'Erick', '2018-03-23', 'CPV-5435-ISL');
INSERT INTO [TransactionMaster] VALUES (4, 3, 'Asim', '2018-03-24', 'BPV-2345-CAN');
INSERT INTO [TransactionMaster] VALUES (5, 2, 'Mehboob', '2018-03-25', 'CRV-2976-PSH');
INSERT INTO [TransactionMaster] VALUES (6, 1, 'Erick', '2018-03-25', 'CPV-2323-KOH');
INSERT INTO [TransactionMaster] VALUES (7, 1, 'Feroze', '2018-03-21', 'CRV-0531-SRG');
INSERT INTO [TransactionMaster] VALUES (8, 3, 'Ali', '2018-03-21', 'BRV-2001-RWP');



CREATE TABLE TransactionDetail
(
ID NUMERIC NOT NULL PRIMARY KEY,
TransactionCode bigint,
DrAmount NUMERIC,
CrAmount NUMERIC
);

INSERT INTO TransactionDetail VALUES (1, 1, '2500', NULL);
INSERT INTO TransactionDetail VALUES (2, 1, NULL, '1500');
INSERT INTO TransactionDetail VALUES (3, 1, NULL, '1000');
INSERT INTO TransactionDetail VALUES (4, 2, '1150', NULL);
INSERT INTO TransactionDetail VALUES (5, 2, NULL, '1150');
INSERT INTO TransactionDetail VALUES (6, 3, '600', NULL);
INSERT INTO TransactionDetail VALUES (7, 3, '400', NULL);
INSERT INTO TransactionDetail VALUES (8, 3, '200', NULL);
INSERT INTO TransactionDetail VALUES (9, 3, NULL, '1200');
INSERT INTO TransactionDetail VALUES (10, 4, '1000', NULL);
INSERT INTO TransactionDetail VALUES (11, 4, NULL, '1000');
INSERT INTO TransactionDetail VALUES (12, 5, '2400', NULL);
INSERT INTO TransactionDetail VALUES (13, 5, NULL, '1200');
INSERT INTO TransactionDetail VALUES (14, 5, NULL, '1000');
INSERT INTO TransactionDetail VALUES (15, 5, NULL, '200');
INSERT INTO TransactionDetail VALUES (16, 6, '2900', NULL);
INSERT INTO TransactionDetail VALUES (17, 6, NULL, '2900');
INSERT INTO TransactionDetail VALUES (18, 7, '700', NULL);
INSERT INTO TransactionDetail VALUES (19, 7, '300', NULL);
INSERT INTO TransactionDetail VALUES (20, 7, '2100', NULL);
INSERT INTO TransactionDetail VALUES (21, 7, NULL, '3100');
INSERT INTO TransactionDetail VALUES (22, 8, '500', NULL);
INSERT INTO TransactionDetail VALUES (23, 8, NULL, '500');

这里是查询

    with data1 as (
select a.id inid,a.VoucherTypeCode,PayeeName,MAX(c.DrAmount) InAmount,TransactionDate,RefNo,FullName from TransactionMaster a 
inner join  [VoucherType] b on a.VoucherTypeCode = b.VoucherTypeCode
inner join  TransactionDetail c on a.ID = c.TransactionCode
where a.VoucherTypeCode in (1,3)
GROUP BY a.id,a.VoucherTypeCode,PayeeName,TransactionDate,RefNo,FullName
),

data2 as (
select a.id outid,a.VoucherTypeCode,PayeeName,MAX(c.CrAmount) OutAmount,TransactionDate,RefNo,FullName from TransactionMaster a 
inner join  [VoucherType] b on a.VoucherTypeCode = b.VoucherTypeCode
inner join  TransactionDetail c on a.ID = c.TransactionCode
where a.VoucherTypeCode in (2,4)
GROUP BY a.id,a.VoucherTypeCode,PayeeName,TransactionDate,RefNo,FullName
)
select *,COALESCE(a.TransactionDate,b.TransactionDate) as FullDate from data1 a full join data2 b on inid = outid and a.TransactionDate = b.TransactionDate

--WHERE COALESCE(a.TransactionDate,b.TransactionDate) BETWEEN '2018-03-23 00:00:00.000' AND '2018-03-24 00:00:00.000'

order by FullDate

当您从日期检查中删除评论时,下面会提供预期的输出:

    inid                 VoucherTypeCode PayeeName                                                                                                                                                                                                                                                       InAmount                                TransactionDate         RefNo                                              FullName                                                                                                                                                                                                                                                        outid                VoucherTypeCode PayeeName                                                                                                                                                                                                                                                       OutAmount                               TransactionDate         RefNo                                              FullName                                                                                                                                                                                                                                                        FullDate                            Opening
-------------------- --------------- --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- --------------------------------------- ----------------------- -------------------------------------------------- --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------- --------------- --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- --------------------------------------- ----------------------- -------------------------------------------------- --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -----------------------
3                    1               Erick                                                                                                                                                                                                                                                           1200                                    2018-03-23 00:00:00.000 CPV-5435-ISL                                       Cash Payment Voucher                                                                                                                                                                                                                                            NULL                 NULL            NULL                                                                                                                                                                                                                                                            NULL                                    NULL                    NULL                                               NULL                                                                                                                                                                                                                                                            2018-03-23 00:00:00.000                 -50
4                    3               Asim                                                                                                                                                                                                                                                            1000                                    2018-03-24 00:00:00.000 BPV-2345-CAN                                       Bank Payment Voucher                                                                                                                                                                                                                                            NULL                 NULL            NULL                                                                                                                                                                                                                                                            NULL                                    NULL                    NULL                                               NULL                                                                                                                                                                                                                                                            2018-03-24 00:00:00.000                 1150

公式是(Opening+InAmount) - Out Amount 将是Closing BalanceOpening 将是前面的行Closing Balance。 因此,对于第一个记录,开始为 0,结束为 3100,第二个记录为 3100,结束为 3600,依此类推。

如果 VoucherType 是 1 或 3,我必须获得 Credit Amount CrAmount,如果 VoucherType 是 2 或 4,那么我需要获得 Debit Amount DrAmount。

我之前问过这个问题,也得到了预期的输出,但现在数据库结构发生了一些变化,我无法在此处使用该逻辑。

【问题讨论】:

  • 你的样本数据和预期的结果匹配吗?看起来像TransactionDetail contra 中的数据。总博士 = Cr
  • @Squirrel 他们应该匹配,让我验证一下。是的,一个主记录的 Total Dr = Cr。
  • 如果匹配,那么所有的Opening都是0
  • @Squirrel 打开行应该是关闭前一行,关闭前一行应该是 Receipt - Payment Amount and Receipt = Opening+Receipt Amount。

标签: sql-server


【解决方案1】:

基本上它与我在您的另一个线程中发布的上一个查询相同。 由于另一个表 TransactionDetail 中的详细信息,您只需要从 TransactionMaster 到它的 INNER JOIN 并执行 SUM()。我没有验证 Dr - Cr 或 Cr - Dr 逻辑。请自行验证。

SELECT  t.*, v.FullName, o.Opening
FROM    [TransactionMaster] t
        INNER JOIN [VoucherType] v  on  t.VoucherTypeCode   = v.VoucherTypeCode
        OUTER APPLY
        (   
        SELECT  Opening = sum(case when m.[VoucherTypeCode] in (1, 3) 
                                   then - ISNULL(d.CrAmount, 0)
                                   else + ISNULL(d.CrAmount, 0)
                                   end)
        FROM    [TransactionMaster] m
                INNER JOIN [TransactionDetail] d    ON  m.ID    = d.TransactionCode
        WHERE   m.TransactionDate < t.TransactionDate
        ) o
WHERE   t.TransactionDate   BETWEEN '2018-03-23' AND '2018-03-24'
order by t.TransactionDate

编辑: 您不需要使用 FULL JOIN 来识别 IN 和 OUT。只需使用 CASE 语句如下

SELECT  inID     = case when t.VoucherTypeCode in (1,3) then t.ID end,
        inAmount = case when t.VoucherTypeCode in (1,3) then a.Amount end,
        outID    = case when t.VoucherTypeCode in (2,4) then t.ID end,
        outAmount = case when t.VoucherTypeCode in (2,4) then a.Amount end,
        t.PayeeName, t.TransactionDate, t.RefNo, 
        v.FullName, Opening = isnull(o.Opening, 0)
FROM    [TransactionMaster] t
        INNER JOIN [VoucherType] v  on  t.VoucherTypeCode   = v.VoucherTypeCode
        CROSS APPLY
        (
        SELECT  Amount  = sum(case when m.[VoucherTypeCode] in (1, 3) 
                                           then -CrAmount
                                           else DrAmount
                                           end)
        FROM    [TransactionMaster] m
            INNER JOIN [TransactionDetail] d    ON  m.ID    = d.TransactionCode
        WHERE   m.ID    = t.ID
        ) a
        OUTER APPLY
        (   
        SELECT  Opening = sum(case when m.[VoucherTypeCode] in (1, 3) 
                                           then -CrAmount
                                           else DrAmount
                                           end)
        FROM    [TransactionMaster] m
            INNER JOIN [TransactionDetail] d    ON  m.ID    = d.TransactionCode
        WHERE   m.TransactionDate   < t.TransactionDate
        ) o
order by t.TransactionDate

【讨论】:

  • 但是Opening 不应该是0。我没有提供正确的数据吗?
  • 查看您的数据,对于 TransactionMater 中的每一行,TransactionDetail 中的 Dr 和 Cr 总数相互矛盾。所以基本上每个 TransactionMaster 的开头都是 0
  • 可能您的数据不正确。例如,对于Asim,事务是Cash Receipt Voucher。为什么在TransactionDetail,你有Dr 2500Cr 1500Cr 1000
  • 这就是重点。当凭证为Receipt 时,它应该得到Debit AmountDr,它始终是一个特定记录,并且始终等于Cr 金额SUMPayment Voucher 也是如此,但在这种情况下,Cr 将仅位于一行中,并且始终等于 Dr 金额 SUM。例如对于 Erick,其中 Master ID3
  • 抱歉,这里有点迷路了。你能展示如何让-50 成为Erick 的开场白吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多