Sum

  • Hello

    I have a database schema,which has several tables which are 5 in all, these tables all have a particular column which icalled total [sum (qtyordered*item_price) as Total] that is been feed from different sources.

    Will i need to create a join query to sum up all of the total columns which will also be grouped by

    table name/ID?

  • Since you need the totals from each of the five tables, you have to join the tables and group by id.

  • It also depends on how you want your results. You might care to UNION the summary statements together and do some more aggregation on it....

    ----------------------------------------------------------------------------------
    Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?

  • This is what i have done so far, can anyone tell me what am doing wrong?

    select sum(total)

    from table_a, table_b,table_c,table_d

    where table_a.total = table_b.total

    and table_c.total = table_d.total

  • I have also tried to use

    select sum(total)as Total_of_All_Tables

    from table_a

    union

    select sum(total)as Total_of_All_Tables

    from table_b

    union

    select sum(total)as Total_of_All_Tables

    from table_c

    union

    select sum(total)as Total_of_All_Tables

    from table_d

    but need to the table names to be displayed

  • Try something like:

    select 'Table_a' as tablename, sum(total)as Total_of_All_Tables

    from table_a

    union

    select 'Table_b' as tablename,sum(total)as Total_of_All_Tables

    from table_b

    union

    select 'Table_c' as tablename,sum(total)as Total_of_All_Tables

    from table_c

    union

    select 'Table_d' as tablename,sum(total)as Total_of_All_Tables

    from table_d

    ----------------------------------------------------------------------------------
    Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply