Subject | RE: [ib-support] Offtopic basic SQL Question |
---|---|
Author | Dmitry Yemanov |
Post date | 2001-09-06T08:16:01Z |
C Fraser,
select
C.Id,
C.FirstName,
C.LastName,
etc,...
I.Id,
I.Date,
( select sum(Quantity * Price) from Invoice_Item where Invoice_Id = I.Id )
Sum_Of_Invoice
from
Customer C join Invoice I on C.Id = I.Customer_Id
;
It may or may not be better depending on your tables structure, indices and
number of records.
HTH,
Dmitry
> A simple question about SQL...I'd suggest something like this:
>
> Say you have a table of customers, invoices and invoice items, and you
> want to do a query on the customers returning their ID, first
> name, last
> name, phone, etc and a list of invoices and their totals
>
> To get the total for each invoice involves doing a sum and grouping by
> the invoice, but, for the query to work, you have to group by all the
> other customer fields... Is this the best way to do it, or
> does it slow
> things down heaps... is some type of sub query thing better???
>
> [snip]
select
C.Id,
C.FirstName,
C.LastName,
etc,...
I.Id,
I.Date,
( select sum(Quantity * Price) from Invoice_Item where Invoice_Id = I.Id )
Sum_Of_Invoice
from
Customer C join Invoice I on C.Id = I.Customer_Id
;
It may or may not be better depending on your tables structure, indices and
number of records.
HTH,
Dmitry