Skip to content
Advertisement

cte FULL OUTER JOIN MYSQL

I have this table(called trial):

‘year1’ refers to values in Column ‘val1’, ‘year2’ to values in Column ‘val2’.

I want to get totals of all values grouped by year.

So I would like to see a result like:

I set up a common table expression(cte) using a self join to do this as follows:

(The first four output columns are added to get clarity concerning what is going on)

The output is:

The output is obviously just not correct.

I have tried modifications to the SELECT statement above such as changing the JOIN type, changing the GROUP BY clause or leaving it out, and adding a WHERE clause, without success. I have Googled and read websites with no success as I can’t get a similar example although there must be many identical questions.

Many thanks for any help.

Advertisement

Answer

If I understand, you want to unpivot and then aggregate. One method is:

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