对三个表进行 SQL JOIN、GROUP BY 以获得总计
我继承了以下数据库设计。 表如下:
customers
---------
customerid
customernumber
invoices
--------
invoiceid
amount
invoicepayments
---------------
invoicepaymentid
invoiceid
paymentid
payments
--------
paymentid
customerid
amount
我的查询需要返回给定客户编号的发票 ID、发票金额(在发票表中)和应付金额(发票金额减去已向发票支付的所有付款)。 一个客户可能有多张发票。
当对一张发票进行多次付款时,以下查询会给出重复的记录:
SELECT i.invoiceid, i.amount, i.amount - p.amount AS amountdue
FROM invoices i
LEFT JOIN invoicepayments ip ON i.invoiceid = ip.invoiceid
LEFT JOIN payments p ON ip.paymentid = p.paymentid
LEFT JOIN customers c ON p.customerid = c.customerid
WHERE c.customernumber = '100'
我该如何解决这个问题?
I've inherited the following DB design. Tables are:
customers
---------
customerid
customernumber
invoices
--------
invoiceid
amount
invoicepayments
---------------
invoicepaymentid
invoiceid
paymentid
payments
--------
paymentid
customerid
amount
My query needs to return invoiceid, the invoice amount (in the invoices table), and the amount due (invoice amount minus any payments that have been made towards the invoice) for a given customernumber. A customer may have multiple invoices.
The following query gives me duplicate records when multiple payments are made to an invoice:
SELECT i.invoiceid, i.amount, i.amount - p.amount AS amountdue
FROM invoices i
LEFT JOIN invoicepayments ip ON i.invoiceid = ip.invoiceid
LEFT JOIN payments p ON ip.paymentid = p.paymentid
LEFT JOIN customers c ON p.customerid = c.customerid
WHERE c.customernumber = '100'
How can I solve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我不确定我是否明白你的意思,但这可能就是你正在寻找的:
如果每张发票有多个付款行,这将为你提供金额
I am not sure I got you but this might be what you are looking for:
This would get you the amounts sums in case there are multiple payment rows for each invoice
非常感谢您的回复!
Saggi Malachi,不幸的是,在有不止一笔付款的情况下,该查询会汇总发票金额。 假设一张 39 美元的发票有两笔付款:18 美元和 12 美元。 因此,与其最终得到如下结果:
你最终会得到:
Charles Bretana,在将查询精简为最简单的查询的过程中,我(愚蠢地)省略了一个附加表 customerinvoices,它提供了一个链接客户和发票之间。 这可用于查看尚未付款的发票。
经过一番努力,我认为以下查询会返回我需要的内容:
你们同意吗?
Thank you very much for the replies!
Saggi Malachi, that query unfortunately sums the invoice amount in cases where there is more than one payment. Say there are two payments to a $39 invoice of $18 and $12. So rather than ending up with a result that looks like:
You'll end up with:
Charles Bretana, in the course of trimming my query down to the simplest possible query I (stupidly) omitted an additional table, customerinvoices, which provides a link between customers and invoices. This can be used to see invoices for which payments haven't made.
After much struggling, I think that the following query returns what I need it to:
Would you guys concur?
对于那些想要从同一个表中获取各种聚合值的人,我有一个提示。
假设我有用户表和用户获得的积分表。 所以它们之间的联系是1:N(一个用户,很多点记录)。
现在,在“积分”表中,我还存储有关用户获得积分的信息(登录、单击横幅等)。 我想列出按
SUM(points)
排序的所有用户,然后按SUM(points WHERE type = x)
排序。 也就是说,按用户拥有的所有积分排序,然后按用户针对特定操作(例如登录)获得的积分排序。SQL 如下:
其美妙之处在于
SUM(points.points * (points.type = 7))
,其中内括号的计算结果为 0 或 1,从而将给定的点值乘以0 或 1,取决于它是否等于我们想要的点类型。I have a tip for those, who want to get various aggregated values from the same table.
Lets say I have table with users and table with points the users acquire. So the connection between them is 1:N (one user, many points records).
Now in the table 'points' I also store the information about for what did the user get the points (login, clicking a banner etc.). And I want to list all users ordered by
SUM(points)
AND then bySUM(points WHERE type = x)
. That is to say ordered by all the points user has and then by points the user got for a specific action (eg. login).The SQL would be:
The beauty of this is in the
SUM(points.points * (points.type = 7))
where the inner parenthesis evaluates to either 0 or 1 thus multiplying the given points value by 0 or 1, depending on wheteher it equals to the the type of points we want.首先,Invoices表中不应该有一个CustomerId吗? 事实上,您无法对尚未付款的发票执行此查询。 如果发票上没有付款,该发票甚至不会显示在查询的输出中,即使它是外部联接...
另外,当客户付款时,您如何知道要附加什么发票到 ? 如果唯一的方法是通过随付款一起到达的存根上的 InvoiceId,那么您(可能不恰当)将发票与付款的客户关联,而不是与订购发票的客户关联。 (有时发票可以由订购服务的客户以外的其他人支付)
First of all, shouldn't there be a CustomerId in the Invoices table? As it is, You can't perform this query for Invoices that have no payments on them as yet. If there are no payments on an invoice, that invoice will not even show up in the ouput of the query, even though it's an outer join...
Also, When a customer makes a payment, how do you know what Invoice to attach it to ? If the only way is by the InvoiceId on the stub that arrives with the payment, then you are (perhaps inappropriately) associating Invoices with the customer that paid them, rather than with the customer that ordered them... . (Sometimes an invoice can be paid by someone other than the customer who ordered the services)
我知道这已经晚了,但它确实回答了你原来的问题。
I know this is late, but it does answer your original question.