Subject | Re: help on query |
---|---|
Author | Svein Erling Tysvær |
Post date | 2004-11-18T08:14:59Z |
Hi Sudheer!
The simple answer is "Learn basic SQL", e.g. at www.w3schools.com/sql/
The answer you're looking for is
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.
When you start working with larger tables (millions of records), you
will want to add a 'where' clause before the 'group by' in most cases,
but for tiny tables this kind of SQL is OK.
HTH,
Set
The simple answer is "Learn basic SQL", e.g. at www.w3schools.com/sql/
The answer you're looking for is
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.
When you start working with larger tables (millions of records), you
will want to add a 'where' clause before the 'group by' in most cases,
but for tiny tables this kind of SQL is OK.
HTH,
Set
> sale_det
> ========
> id product_id qty_sold
> -----------------------------------------
> 1 1 10
> 1 2 20
> 2 2 50
> 2 3 30
> 2 1 20
> 3 2 40
> 3 3 20
>
> purchase_det
> ============
> id product_id qty_purchased value
> ------------------------------------------------------
> 1 2 500 5000.00
> 2 3 300 3000.00
> 3 1 200 2000.00
>
>
> How do I get a result set like the following ?
>
> product_id sold_qty purchased_qty purchase_value
> -------------------------------------------------------------------
> 1 30 200 2000.00
> 2 110 500 5000.00
> 3 50 300 3000.00