SQL JOIN, GROUP BY on three tables to get totals

前端 未结 5 1082
攒了一身酷
攒了一身酷 2020-12-14 21:17

I\'ve inherited the following DB design. Tables are:

customers
---------
customerid  
customernumber

invoices
--------
invoiceid  
amount

invoicepayments
         


        
5条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-14 21:20

    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)

提交回复
热议问题