How to create horizontal columns from rows in MYSQL?


Problem :

Sorry if I asked this dumb question as it puzzled me how to do such for several days.

I have a table in MySQL named Grades. enter image description here

Now, what I want to do is to make an output like this one. enter image description here

Is there an SQL to do such? Thank you so much in advance.



Solution :

You can apply a case/when and group by the student and the class. If you want only a single person, just apply a where clause... or even if you want all students for a class, or a given school year... etc.

SELECT
      g.studNumber,
      g.subjCode,
      MAX( case when g.period = 1 then g.grade else 0 end ) as 1st_period,
      MAX( case when g.period = 2 then g.grade else 0 end ) as 2nd_period,
      MAX( case when g.period = 3 then g.grade else 0 end ) as 3rd_period,
      MAX( case when g.period = 4 then g.grade else 0 end ) as 4th_period,
      AVG( g.grade ) as Ave
   from
      Grades g
   group by 
      g.studNumber,
      g.subjCode

Mysql Tutorials

Mysql Howto..

VB.net mySQL How to use DateAdd in sql query

MySQL how to join tables on two fields

How to select a value with MySQL based on another table's value?

How to retrieve objects from a MySQL Database with a many-to-many relationship?

How to find a parent with a child excluding another in mysql?

How to query avg for every past 7 days in sql, MySQL?

How do I get rid of the duplicates in this double mysql join?

How to connect MySQL Cartridge to Django in OpenShift

How to serialize C# data to wp_postmeta (MySQL longtext column)

How to delete data from sql with pdo?

Data Modeling - how to handle two, dependent “status” columns?

How to Find Validaity Periods using mysql

How to change MySQL Connector installation directory?

How to Mysql escape in PYTHON

How to link mySQl and mongoDB

How 'Set Default value' to 'NOW()' with Sequel PRO?

How to call a php function that queries mysql from within map area using onclick

How to create a root user with no password in mysql

how to add ' like special characters in mysql varchar data type column?

PDO : How different is mysql from postgresql [closed]

how to fire a query in mysql prompt using java code

How to reuse auto_increment values? [duplicate]

How to construct MySQL query to get Products with all selected Features?

Complicated idea - how to create car racing for my RPG game's players

MySQL table with 2000 million records how to optimize [closed]

How to reset the auto increment number/column in a MySql table

How to get data from mysql php to android [closed]

How to show barchart value by year from mysql Database using php

how to convert datetime from any zone to a specific zone in mysql

MySQL: How to count from columns separately?