Subject | Re: [firebird-support] Re: help on query |
---|---|
Author | Arno Brinkman |
Post date | 2004-11-18T08:34:36Z |
Hi,
result, because on duplicate "purchase_det.product_id" entries the sum for
"sale_det.qty_sold" will include the same "sale_det" record x-times.
I guess this is what he wants, but if he wants to see all products (even
those that doesn't have sales) then he should join with products and left
join the other tables.
SELECT
sd.product_id,
sum(sd.qty_sold) as sold_qty,
pd.qty_purchased as purchased_qty,
pd.value as purchase_value
FROM
sale_det sd
JOIN purchase_det pd ON (pd.product_id = sd.product_id)
GROUP BY
sd.product_id,
pd.qty_purchased,
pd.value
Regards,
Arno Brinkman
ABVisie
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Firebird open source database (based on IB-OE) with many SQL-99 features :
http://www.firebirdsql.org
http://www.firebirdsql.info
http://www.fingerbird.de/
http://www.comunidade-firebird.org/
Support list for Interbase and Firebird users :
firebird-support@yahoogroups.com
Nederlandse firebird nieuwsgroep :
news://newsgroups.firebirdsql.info
> The simple answer is "Learn basic SQL", e.g. at www.w3schools.com/sql/Certainly ;)
> The answer you're looking for isIf that is the case then your above solution will be not given the expected
>
> select pd.product_id,
> sum(sd.qty_sold) as sold_qty,
> sum(pd.qty_purchased) as purchased_qty,
> sum(pd.value) as purchase_value
> from sale_det sd
> join purchase_det pd on pd.product_id = sd.product_id
> group by pd.product_id
>
> I assume that you want to sum the product_det as well, even though
> your example doesn't require it.
result, because on duplicate "purchase_det.product_id" entries the sum for
"sale_det.qty_sold" will include the same "sale_det" record x-times.
I guess this is what he wants, but if he wants to see all products (even
those that doesn't have sales) then he should join with products and left
join the other tables.
SELECT
sd.product_id,
sum(sd.qty_sold) as sold_qty,
pd.qty_purchased as purchased_qty,
pd.value as purchase_value
FROM
sale_det sd
JOIN purchase_det pd ON (pd.product_id = sd.product_id)
GROUP BY
sd.product_id,
pd.qty_purchased,
pd.value
Regards,
Arno Brinkman
ABVisie
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Firebird open source database (based on IB-OE) with many SQL-99 features :
http://www.firebirdsql.org
http://www.firebirdsql.info
http://www.fingerbird.de/
http://www.comunidade-firebird.org/
Support list for Interbase and Firebird users :
firebird-support@yahoogroups.com
Nederlandse firebird nieuwsgroep :
news://newsgroups.firebirdsql.info