Skip to content
Advertisement

How to get the totals of each column MySQL

I have an SQL query that get the number of jobs, gross weight etc for different companies.

The result in MySQL Workbench looks like this: enter image description here

I would like to add another row at the bottom that calculates the total of each column i.e the total of the jobs column would be 267. How do I add an extra row for the totals?

SQL query:

Advertisement

Answer

You are looking for the GROUP BY modifier WITH ROLLUP:

Note: I also changed the GROUP BY key for two reasons. First, it really should match the column used in the SELECT. Second, you were using a value from a LEFT JOINed table, so the value could be NULL. That is usually undesirable.

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