help with SQL Query

Status
Not open for further replies.
Hi Guys,

I have been struggling with this for a few hours. It seems like this should be a simple query but it's not. I have three tables:

Invoices Table
id cusotmerid
30 1
40 2


Customers Table
id customername
1 Customer #1
2 Customer #2

and InvoiceCharges Table
id invoiceid unitprice quantity linetotal
1 30 30 1 30
2 30 21 3 63
3 40 3.5 1 3.5


I need to return a result set with each invoice id, the customers name, and the invoice total ie
30, Customer #1, 93
40, Customer #2, 3.5


Any help would be appreciated.
 

LordHunter317

Ars Legatus Legionis
22,394
Subscriptor
It would be nice to know what database this is for, but it seems as simple as:
Code:
SELECT a.invoiceid, c.customername, a.tot 
FROM invoice i INNER JOIN customer c ON i.customerid = c.id 
     INNER JOIN 
     (SELECT invoiceid, SUM(linetotal) AS tot 
      FROM invoicecharges
      GROUP BY invoiceid) a ON i.id = a.invoiceid
ORDER BY invoiceid;
The inner select generates a subtable containing the total for each invoice. You join this table against the invoice table to get the relationship with each customer, then against the customer table to get the name.

Depending on your database, it may need to be tweaked to work right.
 

wallinbl

Ars Legatus Legionis
14,319
Subscriptor
C0rinthian":3ctix4oa said:
The subselect isn't really necessary. The following will get you the same result.
It's not, but it serves a useful purpose and it's not a bad habit to form. It takes a table with different granularity and it makes it the same, such that the join is safe. Were you to have multiple one to many relationships, or require some aggregation on the invoice or customers table, this pattern can easily be extended to those situations.
 

LordHunter317

Ars Legatus Legionis
22,394
Subscriptor
I would also suspect the subselect version to be faster, because query optimization around 'GROUP BY' tends to be rather poor. Not that I expect it to really matter unless the tables are truly massive...

But yes, wallinbl hit the rationale perfectly. It's generally a good idea to mentally structure and code your queries in this fashion.
 

GohanIYIan

Ars Legatus Legionis
10,196
LordHunter317":3qhv0zfh said:
I would also suspect the subselect version to be faster, because query optimization around 'GROUP BY' tends to be rather poor. Not that I expect it to really matter unless the tables are truly massive...

It does sort of depend on the database. MySQL's performance on joins against subqueries is absurdly awful, IME.
 

topham

Ars Legatus Legionis
13,756
Subscriptor
Mux":1w6k6te2 said:
zeotherm":1w6k6te2 said:
Moderation:
Mux":1w6k6te2 said:
Amateur hour
Do you have anything constructive to add?
Other than it really *is* a simple query (in the OPs words), and any answer thereof amounts to doing somebody's homework?

No. :D
Pretty much this.

If all they want is a solution Google is good enough (or stack exchange). But real help? They have to show some work first.
 
Status
Not open for further replies.