Subject | Re: [firebird-support] Doubt Regarding Sum() Function |
---|---|
Author | Kjell Rilbe |
Post date | 2010-09-15T09:42:39Z |
Vishal Tiwari skriver:
would either first join the tables, then group by, or the other way
around, depending on your requirements.
For example (not using your exact sample schema):
select MT.MasterID, MT.MasterName, MT.BookPrice, Sub.SubTableName,
sum(Sub.CountSold) "Sold"
from MasterTable MT
inner join (
select 'MainOffice' SubTableName, CountSold
from MainOfficeSubtable
union
select 'BranschOffice1' SubTableName, CountSold
from BranchOffice1Subtable
) Sub on Sub.MasterID = MT.MasterID
group by MT.MasterID, MT.MasterName, MT.BookPrice
Kjell
--
--------------------------------------
Kjell Rilbe
DataDIA AB
E-post: kjell@...
Telefon: 08-761 06 55
Mobil: 0733-44 24 64
>If you want to calculate sums over records from multiple tables you
> In general I would like to know that, if I need to do summation of two
> different columns from two different tables (provided database is
> designed well) independentely in the same sql, that time how I should
> achieve that, as you said I should use UNION as per the sql requirement.
>
> I just want to get the accurate sum, if I come accross with some
> situation then how should I do this?
would either first join the tables, then group by, or the other way
around, depending on your requirements.
For example (not using your exact sample schema):
select MT.MasterID, MT.MasterName, MT.BookPrice, Sub.SubTableName,
sum(Sub.CountSold) "Sold"
from MasterTable MT
inner join (
select 'MainOffice' SubTableName, CountSold
from MainOfficeSubtable
union
select 'BranschOffice1' SubTableName, CountSold
from BranchOffice1Subtable
) Sub on Sub.MasterID = MT.MasterID
group by MT.MasterID, MT.MasterName, MT.BookPrice
Kjell
--
--------------------------------------
Kjell Rilbe
DataDIA AB
E-post: kjell@...
Telefon: 08-761 06 55
Mobil: 0733-44 24 64