Search code examples
htmlsqlsql-serverasp.net-mvc-5pivot

MVC5 HTML make column as table header?


Look at this table in my database:

==============================
|^student |^Subject |^Result |
==============================
| name1   | math    | pass   |
==============================
| name1   | physics | pass   |
==============================
| name1   | Biology | faille |
==============================
| name2   | math    | pass   |
==============================
| name2   | physics | faille |
==============================
| name2   | Biology | pass   |
==============================

By using MVC 5, HTML and SQL Server 2017 can I make it like this in index page:

======================================
|^student | Math | Physics | Biology |
======================================
| name1   | pass | pass    | faille  |
======================================
| name2   | pass | faille  | pass    |
======================================

Thank you in advance.


Solution

  • You can do conditional aggregation:

    select student,
        max(case when subject = 'math'    then result end) math,
        max(case when subject = 'physics' then result end) physics,
        max(case when subject = 'biology' then result end) biology
    from mytable
    group by student