SQL, UNION and correct ORDER

Just had to combine two tables month column and sort the whole thing. I had two tables like

Name Month
Something 201201
Else 201112

and

Name Month
Anything 201201
Other 201111

and I wanted (put the two tables‘ month in one column and remove duplicates):

Month
201201
201112
201111

The solution was not too complicated, but I thought I share it.

SELECT MONTH FROM table1
UNION
SELECT MONTH FROM table2 ORDER BY MONTH DESC

No need to add a GROUP BY because of the UNION, and the seconds query ORDER BY sorts the complete result.

Permanentlink zu diesem Beitrag: https://techblog.steffmeister.at/sql-union-and-correct-order/

Schreibe einen Kommentar

Deine Email-Adresse wird nicht veröffentlicht.

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.