【问题标题】:Computing Balance Sheet计算资产负债表
【发布时间】:2020-06-16 03:22:07
【问题描述】:

使用以下查询:

select b1.Name DrBook, c1.Name DrControl, b2.Name CrBook, c2.Name CrControl, tn.Amount
from Transactions tn
left join Books b1 on b1.Id = tn.DrBook
left join Books b2 on b2.Id = tn.CrBook
left join ControlLedgers c1 on c1.Id = tn.DrControl
left join ControlLedgers c2 on c2.Id = tn.CrControl

我得到了资产负债表的这个结果集:

+---------------------+-----------+---------------------+-----------+--------+
|       DrBook        | DrControl |       CrBook        | CrControl | Amount |
+---------------------+-----------+---------------------+-----------+--------+
| Current Assets      | Cash      | Fund                | Initial   | 100000 |
| Current Assets      | Cash      | Fund                | Initial   | 100000 |
| Current Assets      | Cash      | Fund                | Initial   | 100000 |
| Current Assets      | Cash      | Fund                | Initial   | 100000 |
| Current Assets      | Cash      | Fund                | Initial   | 100000 |
| Expenses            | Foods     | Current Liabilities | Payables  |  10000 |
| Current Liabilities | Payables  | Current Assets      | Cash      |   5000 |
+---------------------+-----------+---------------------+-----------+--------+

在我的应用程序中显示资产负债表我现在正在做的是发出两个查询并获得两个这样的结果集:

查询1:

select b1.Name DrBook, c1.Name DrControl, SUM(tn.Amount) Amount
from Transactions tn
left join Books b1 on b1.Id = tn.DrBook
left join ControlLedgers c1 on c1.Id = tn.DrControl
group by DrBook, DrControl

结果集 1:

+---------------------+-----------+--------+
|       DrBook        | DrControl | Amount |
+---------------------+-----------+--------+
| Current Assets      | Cash      | 500000 |
| Expenses            | Foods     |  10000 |
| Current Liabilities | Payables  |   5000 |
+---------------------+-----------+--------+

查询 2:

select b1.Name CrBook, c1.Name CrControl, SUM(tn.Amount) Amount
from Transactions tn
left join Books b1 on b1.Id = tn.CrBook
left join ControlLedgers c1 on c1.Id = tn.CrControl
group by CrBook, CrControl

结果集 2:

+---------------------+-----------+--------+
|       CrBook        | CrControl | Amount |
+---------------------+-----------+--------+
| Current Assets      | Cash      |   5000 |
| Current Liabilities | Payables  |  10000 |
| Fund                | Initial   | 500000 |
+---------------------+-----------+--------+

如果是资产或费用(在本例中为流动资产和费用),则从结果集 1 中减去结果集 2,如果是负债、收入或基金(在本例中为流动负债和资金),则从结果集 2 中减去结果集 1。 Fund) 来获得这样的最终结果集:

+---------------------+---------------+---------+
|        Book         | ControlLedger | Balance |
+---------------------+---------------+---------+
| Current Assets      | Cash          |  495000 |
| Expenses            | Food          |   10000 |
| Current Liabilities | Payables      |    5000 |
| Fund                | Initial       |  500000 |
+---------------------+---------------+---------+

我尝试了一些case 语句来通过 sql 查询而不是在应用程序代码中手动计算来获得最终结果集,但是这些都不起作用!

编辑

这是表格的定义:

CREATE TABLE "Transactions"(
    "Id"            INTEGER NOT NULL,
    "Date"          TEXT NOT NULL,
    "DrBook"        INTEGER NOT NULL,
    "CrBook"        INTEGER NOT NULL,
    "DrControl"     INTEGER NOT NULL,
    "CrControl"     INTEGER NOT NULL,
    "DrLedger"      INTEGER,
    "CrLedger"      INTEGER,    
    "DrSubLedger"   INTEGER,
    "CrSubLedger"   INTEGER,
    "DrPartyGroup"  INTEGER,
    "CrPartyGroup"  INTEGER,
    "DrParty"       INTEGER,
    "CrParty"       INTEGER,
    "DrMember"      INTEGER,
    "CrMember"      INTEGER,
    "Amount"        INTEGER NOT NULL,
    "Narration"     TEXT,

    FOREIGN KEY("DrBook") REFERENCES "Books"("Id"),
    FOREIGN KEY("CrBook") REFERENCES "Books"("Id"),
    FOREIGN KEY("DrControl") REFERENCES "ControlLedgers"("Id"),
    FOREIGN KEY("CrControl") REFERENCES "ControlLedgers"("Id"),
    FOREIGN KEY("DrLedger") REFERENCES "Ledgers"("Id"),
    FOREIGN KEY("CrLedger") REFERENCES "Ledgers"("Id"),
    FOREIGN KEY("DrSubLedger") REFERENCES "SubLedgers"("Id"),
    FOREIGN KEY("CrSubLedger") REFERENCES "SubLedgers"("Id"),
    FOREIGN KEY("DrPartyGroup") REFERENCES PartyGroups("Id"),
    FOREIGN KEY("CrPartyGroup") REFERENCES PartyGroups("Id"),
    FOREIGN KEY("DrParty") REFERENCES "Parties"("Id"),
    FOREIGN KEY("CrParty") REFERENCES "Parties"("Id"),
    FOREIGN KEY("DrMember") REFERENCES "Members"("Id"),
    FOREIGN KEY("CrMember") REFERENCES "Members"("Id")  
);

我为每本日记帐插入一行,其中包含借方、贷方和金额信息。我没有 Dr/CrProduct 或 Dr/CrServices,因为这是为个人和家庭的簿记和会计设计的。

例如,从 A 先生那里购买食物,我通过 (1):

Expenses -> Food -> Rice -> Fine Rice A/c          Dr. 10000
    Current Liabilities -> Payables A/C            Cr. 10000

如果是赊购并且购买金额以现金支付时,我通过 (2):

Current Liabilities -> Payables A/C                 Dr. 10000
    Current Assets -> Cash -> In Hand -> Emon A/c   Cr. 10000

在表格中变成:

+----+------------+--------+--------+-----------+-----------+----------+----------+-------------+-------------+--------------+--------------+---------+---------+----------+----------+--------+----------------+
| Id |    Date    | DrBook | CrBook | DrControl | CrControl | DrLedger | CrLedger | DrSubLedger | CrSubLedger | DrPartyGroup | CrPartyGroup | DrParty | CrParty | DrMember | CrMember | Amount |   Narration    |
+----+------------+--------+--------+-----------+-----------+----------+----------+-------------+-------------+--------------+--------------+---------+---------+----------+----------+--------+----------------+
|  3 | 2020-06-15 |      3 |      5 |         9 |        18 |        2 |          |           2 |             |              |            4 |         |       1 |          |          |  10000 | Some Narration |
|  3 | 2020-06-15 |      5 |      2 |        18 |         7 |          |        1 |             |           1 |            4 |              |       1 |         |          |          |  10000 |                |
+----+------------+--------+--------+-----------+-----------+----------+----------+-------------+-------------+--------------+--------------+---------+---------+----------+----------+--------+----------------+

这里是第一行的快速剖析:

+--------------+----------------+---------------------+
|   Columns    |     Values     |      Mappings       |
+--------------+----------------+---------------------+
| DrBook       | 3              | Expenses            |
| CrBook       | 5              | Current Liabilities |
| DrControl    | 9              | Food                |
| CrControl    | 18             | Payables            |
| DrLedger     | 2              | Rice                |
| DrSubLedger  | 2              | Fine Rice           |
| CrPartyGroup | 4              | Groceries           |
| CrParty      | 1              | Mr. A               |
| Amount       | 10000          |                     |
| Narration    | Some Narration |                     |
+--------------+----------------+---------------------+

【问题讨论】:

  • Transactions 的行包含 DrBook、DrControl、CrBook、CrControl 的所有列的值?或者每一行对于其中的 2 个都有空值?最好发布示例数据。
  • @forpas,我添加了详细说明如何插入这些行以及它们的含义。
  • 没有什么可以做的(在我看来)。也许不需要 t4 cte,因为聚合和减法可以在 1 步中完成。
  • @forpas,是的,删除了 t4 并更新了 Answer 中的代码
  • 我会写成:sum(case when Id <=3 then 1 else -1 end * Amount)

标签: sqlite


【解决方案1】:

不确定以下是否是唯一的方法:

with t1(Id, Book, Control, Amount) as (
    select tn.DrBook, b1.Name, c1.Name, sum(tn.Amount)
    from Transactions tn
    left join Books b1 on b1.Id = tn.DrBook
    left join ControlLedgers c1 on c1.Id = tn.DrControl
    group by DrBook, DrControl
), 
t2 as (
    select tn.CrBook, b1.Name, c1.Name, -1*sum(tn.Amount)
    from Transactions tn
    left join Books b1 on b1.Id = tn.CrBook
    left join ControlLedgers c1 on c1.Id = tn.CrControl
    group by CrBook, CrControl
), 
t3 as (
    select * from t1 union all select * from t2
)
select Book, Control,
    case when Id <=3 then sum(Amount)
    else -1*sum(Amount) end Balance
from t3 group by Book, Control order by Id

它产生的正是我所期望的:

+---------------------+----------+---------+
|        Book         | Control  | Balance |
+---------------------+----------+---------+
| Current Assets      | Cash     |  495000 |
| Current Liabilities | Payables |    5000 |
| Expenses            | Foods    |   10000 |
| Fund                | Initial  |  500000 |
+---------------------+----------+---------+

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多