Skip to content
Advertisement

MySQL sum() from more than 1 table

Ok, so every sum() returns the desired value, I already checked it but I’m having problems when trying to sum everything together and then get the total value.

Basically I have to do this:

  • initial Fee(which is a set value defined on the”customer” table) + sum of activity1 cost + sum of activity2 cost == value

But I don’t know how I can make it work so I get a sum of everything, I tried for a while but I always get the “SELECT is not valid at this position”

Advertisement

Answer

You can get the sums of hours more readily by just JOINing the tables to each other and using conditional aggregation to get the activity sums:

And then using this result as a derived table for the final addition:

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement