SQL Query to sum values across 4 tables

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • imtiazdba
    New Member
    • Aug 2007
    • 1

    SQL Query to sum values across 4 tables

    hi, it is very nice to interact with u all i have small problem in sql query
    that is i want to join the four tables with column each and sum them
    and it should display four column total.

    regards imtiaz
  • azimmer
    Recognized Expert New Member
    • Jul 2007
    • 200

    #2
    Originally posted by imtiazdba
    hi, it is very nice to interact with u all i have small problem in sql query
    that is i want to join the four tables with column each and sum them
    and it should display four column total.

    regards imtiaz
    I'm afraid at this point it's not really clear what you mean. I see you have four tables that you want to join (if you have the relations it's no problem). Is it that you have a column with the same name in each and you want to sum these columns (four totals)? If so, here's the skeleton for the solution:
    Code:
    SELECT a.idfield, sum(a.sumfield), sum(b.sumfield), sum(c.sumfield), sum(d.sumfield)
    FROM mytable as a, mytable as b, mytable as c, mytable as d
    WHERE a.idfield=b.idfield and a.idfield=c.idfield and a.idfield=d.idfield
    GROUP BY a.idfield
    If it's not what you meant, pls. give us a more specific description (e.g. with examples)

    Comment

    Working...