← back to the blog

SQL horizontal vs vertical data query with CASE WHEN

Posted on June 27th, 2020 in MySQL, NodeJS by George


The first image shows the database entries we are dealing with.

Please right-click the image and from the context menu select "Open image in a new tab". This way you can have a clear view of the data in detail.

The main column used in this example is the "year" column which is not a date as you may assume at first glance but instead is a category.



Then the second image shows a query with the data ordered horizontally based on the "year" type column. Using the COUNT function we separate data in 4 columns for the type category. The other two columns, the first column, "state", and the last column "total players" are used to be able to group and order by.




The last image will show data displayed vertically on the same "year" type column.

This time around the players' data is grouped into two classes for the second column. 




Thank you.