帮我!我的 SQL 坏了:(
我写了一个小应用程序来帮助我和我的室友管理谁欠谁的钱。人们将债务(例如每周购物的付款)输入网络界面,然后记录到数据库中。
该数据库有 3 个表:
User { Name, Id }
Expenditure { Pennies, Id, Payer (User.Id) }
Debt { Expenditure (Expenditure.Id), User (User.Id) }
因此,一个人对另一个人的债务将在“支出”表中输入一行,其中包含贷方的 ID,并在每个“债务人”的债务表中输入一行。每周商店在支出中输入一行,然后将所有四位室友作为债务人分在 4 个单独的行中(在这种情况下,有 1 个人同时被列为贷方和债务人)。任何人对某一特定支出所欠的金额是总支出除以支付该支出的债务人数量(即债务表中引用的次数)
希望这是有道理的!
我的问题是编写一些 SQL 来计算谁欠什么。我想编写一个查询来计算人与人之间的总债务,这需要获取引用贷方的所有支出和引用债务人的所有债务,并且(重要的是)它需要将所有支出的便士相加按引用它的债务数量。
然后,我可以简单地通过以下方式计算未偿债务总额:
Debt(A, B) - Debt(B, A)
目前我有这个 SQL:
SELECT
SUM("Pennies") / (SELECT COUNT(*) FROM public."Debt", public."Expenditure" WHERE public."Expenditure"."Id" = public."Debt"."Expenditure") As "Refs"
FROM
public."Debt",
public."Expenditure"
WHERE
public."Debt"."User" = $debtorId
AND
public."Expenditure"."Payer" = $lenderId
AND
public."Expenditure"."Id" = public."Debt"."Expenditure"
看起来是正确的,但是当我手动将这些值相加时,我得到了不同的数字。
编辑:: 回应以下答案之一。我显示了共享数量,但它似乎总是显示 1 :(
SELECT
shares
FROM
public."Debt",
public."Expenditure",
(SELECT COUNT(*) as "shares", public."Expenditure"."Id" as "Id" FROM public."Expenditure" GROUP BY public."Expenditure"."Id") as "debtors"
WHERE
public."Debt"."User" = 4
AND
public."Expenditure"."Payer" = 1
AND
public."Expenditure"."Id" = public."Debt"."Expenditure"
AND
"debtors"."Id" = public."Debt"."Expenditure"
I've written a little application to help me and my housemates manage who owes whom money. People enter their debts (such as payments for the weekly shopping) into a web interface and it gets logged to a database.
The database has 3 tables:
User { Name, Id }
Expenditure { Pennies, Id, Payer (User.Id) }
Debt { Expenditure (Expenditure.Id), User (User.Id) }
So, a debt from a person to a person enters a row into the "expenditure" tablewith the Id of the lender, and a row into the Debt table for each "Debtor". A weekly shop enters a single row into the expenditure, and then all four housemates as debtors in 4 separate rows (in that case, 1 person is listed as both the lender and a debtor). The amount any one person owes for a particular expenditure is the total expenditure, divided by the number of debtors who are paying it (ie. the number of times it is reference in the Debts table)
Hopefully that makes sense!
My problem is writing a bit of SQL to calculate who owes what. I want to write a query which calculates the total debt from person to person, this needs to get all the expenditures which reference the lender and all the debts which reference the debtor and (vitally) it needs to sum the pennies of all the expenditures divided by the number of debts which reference it.
I can then calculate the total outstanding debt simply through:
Debt(A, B) - Debt(B, A)
At the moment I have this SQL:
SELECT
SUM("Pennies") / (SELECT COUNT(*) FROM public."Debt", public."Expenditure" WHERE public."Expenditure"."Id" = public."Debt"."Expenditure") As "Refs"
FROM
public."Debt",
public."Expenditure"
WHERE
public."Debt"."User" = $debtorId
AND
public."Expenditure"."Payer" = $lenderId
AND
public."Expenditure"."Id" = public."Debt"."Expenditure"
which looks right, but when I add the values up by hand I get different numbers.
EDIT:: In response to one of the answers below. I have the number of shares being displayed, but it always seems to display 1 :(
SELECT
shares
FROM
public."Debt",
public."Expenditure",
(SELECT COUNT(*) as "shares", public."Expenditure"."Id" as "Id" FROM public."Expenditure" GROUP BY public."Expenditure"."Id") as "debtors"
WHERE
public."Debt"."User" = 4
AND
public."Expenditure"."Payer" = 1
AND
public."Expenditure"."Id" = public."Debt"."Expenditure"
AND
"debtors"."Id" = public."Debt"."Expenditure"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
仔细检查您用于计算欠债人数的 SQL - 您当前的代码计算系统中所有债务的欠款总数。
该子选择可能应该移至
FROM
部分并添加一个 where 子句 -
然后您可以
Double check the SQL you have for calculating the number of people owing a debt - your current code calculates the total number of portions owing on all debts in the system.
That sub-select should probably be moved into the
FROM
sectionand add a where clause -
Then you can
我不明白“除以引用它的债务数量”的部分。您实际上要做的就是除以数据库中的部门总数(前提是它们都有支出)。
I don't understand the part "divided by the number of debts which reference it." What you actually do is dividing by the total number of Depts in your database (provided that they all have an Expenditure).