I’d like to get a table which stores students data in long format and the marks they receive for all of their subjects in one query.
This is my table structure:
Table: markdetails
## studid ## ## subjectid ## ## marks ##
A1 3 50
A1 4 60
A1 5 70
B1 3 60
B1 4 80
C1 5 95
Table: student info
Actual Structure:
## studid ## ## name ##
A1 Raam
B1 Vivek
c1 Alex
I want the result set to have the following wide format structure as result of the pivotization:
Table: Student Info
## studid ## ## name## ## subjectid_3 ## ## subjectid_4 ## ## subjectid_5 ##
A1 Raam 50 60 70
B1 Vivek 60 80 null
c1 Alex null null 95
How can I accomplish this in SQLite?
First you need to change the current table to a temp table:
Then, you’ll want to recreate
student_info:Then, populate
student_info:Now, just drop your temp table:
And that’s how you can quickly update your table.
SQLite lacks a
pivotfunction, so the best you can do is hard-code some left joins. Aleft joinwill bring match any rows in its join conditions and returnnullfor any rows from the first, or left, table that don’t meet the join conditions for the second table.