Subject Re: [firebird-support] Doubt Regarding Sum() Function
Author Mark Rotteveel
> Hi Mark,
>  
> Pl do not go on database design (I know it is not good one, I have taken
> this example to just know that how to achieve the two columns summation
> independantely), it is just an example to understand my doubt.

It is not a doubt, it is a question. I am of the opinion that good database design makes things a lot easier.

> 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.

What requirement?

> I just want to get the accurate sum, if I come accross with some situation
> then how should I do this?

There is no single answer, because it depends on if the SUM result depends on the content of both tables or not, what should be contained in the sum and what not, etc.

Given your example in your previous mail

Totals per book over all branches:
SELECT t.Book_Name, SUM(t.Copy_Sold) FROM (
SELECT Book_Name, Copy_Sold FROM "Main_Branch_Book_Details"
UNION ALL
SELECT Book_Name, Copy_Sold FROM "Sub_Branch_Book_Details"
)t
GROUP BY t.Book_Name

Totals per book per branch:
SELECT t.Branch, t.Book_Name, SUM(t.Copy_Sold) FROM (
SELECT 'MAIN' AS Branch, Book_Name, Copy_Sold FROM "Main_Branch_Book_Details"
UNION ALL
SELECT 'SUB' AS Branch, Book_Name, Copy_Sold FROM "Sub_Branch_Book_Details"
)t
GROUP BY t.Branch, t.Book_Name

Mark

--
GRATIS: Spider-Man 1-3 sowie 300 weitere Videos!
Jetzt freischalten! http://portal.gmx.net/de/go/maxdome