2

How can i turn a table from this form:

S_ID          SUBJECT          MARK
1             English          90
1             Math             40
1             Computer         30
2             English          85
2             Math             10
2             Computer         06
3             English          10
3             Math             20
3             Computer         40

To this form

S_ID       English        Math         Computer
1          90             40           30
2          85             10           06
3          10             20           40

using SQL code,

I'm using MS Access 2010,

Thank you

3 Answers 3

4
SELECT S_ID ,
       MAX(CASE WHEN subject = 'English' THEN mark ELSE null END) AS English,
       MAX(CASE WHEN subject = 'Math' THEN mark ELSE null END) AS Math,
       MAX(CASE WHEN subject = 'Computer' THEN mark ELSE null END) AS Computer
FROM myTable
GROUP BY S_ID 
6
  • +1 You can omit the else null, an unmatched case returns null by default
    – Andomar
    Commented Jul 4, 2011 at 12:35
  • i looking forward to test this, thank you :) @HansUp :Access supports SWITCH statement, is it the same as CASE WHEN?
    – Ali
    Commented Jul 5, 2011 at 8:20
  • Scorpi0: if you are going to add an ELSE null clause then shouldn't you also cast the null to the same type as mark?
    – onedaywhen
    Commented Jul 5, 2011 at 8:44
  • @onedaywhen : Seriously, cast a null ? Are you serious ? Commented Jul 5, 2011 at 8:46
  • ...noting that neither CASE nor type casting NULL is supported by the Access database engine (ACE, Jet, whatever).
    – onedaywhen
    Commented Jul 5, 2011 at 8:46
4

You want a cross-tab query which will yield a column per subject;

TRANSFORM Sum(MARK) AS TotalMark
SELECT 
   S_ID
FROM marks
   GROUP BY S_ID
PIVOT SUBJECT;
4
  • -although it works, but i'm looking for something that would work with mysql also, a more general SQL approach thanks you very much for your reply, and of course up voted :)
    – Ali
    Commented Jul 5, 2011 at 8:17
  • -the other thing is i don't want the total(cross-tab),I want a "list" of students as shown in my question. :)
    – Ali
    Commented Jul 5, 2011 at 8:24
  • 1
    So long as there is one-mark-per-student-per-subject then SUM() will be ok as you will only ever be summing one value, another aggregate like MIN()/MAX() would likewise give the same result. For mySQL you can use Scorpi0's answer replacing CASE with sum( if(itemname = 'english', mark, 0)) - access also has a similar iif() - I doubt your going to find a cross-database way of doing this elegantly, particularly if one of them is Access.
    – Alex K.
    Commented Jul 5, 2011 at 9:47
  • "I doubt your going to find a cross-database way of doing this elegantly, particularly if one of them is Access." couldn't agree more
    – Ali
    Commented Jul 5, 2011 at 21:35
0

if this is a one time exercise, you could use many statements, sequentially: something like this:

insert into new_table
select distinct s_id, 0,0,0
from old_table

then a series of updates

update new_table n
set english = (select english from old_table where s_id = n.s_id )
where s_id = n.s_id

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.