0

In SQL Server 2017, I have two SQL Tables like below:

Table1:

ProjectId ProjectScore
1         90
2         50
3         30

Table2:

ProjectId  PersonName  GivenScore
1          Jack         50
1          John         30
1          Mark         10
2          Jack         20
2          John         20
2          Mark         10
3          Jack         20
3          John         10

What I need is to come with a query giving me the output below:

ProjectId  ProjectScore Jack John Mark
1             90         50   30   10
2             50         20   20   10
3             30         20   10   

I know I need to use "PIVOT" at some point but couldn't end up with a solution. Any help would be appreciated.

  • `PIVOT` requires that you list the pivoted column names (i. e. "Jack", "John", "Mark") in the text of the SELECT statement. It won't give you dynamic data driven pivot column names. Is that okay? – Seva Alekseyev Jan 24 '23 at 18:09
  • No, the names should be dynamic. – Jesus Hamdi Jan 24 '23 at 18:15
  • Then `PIVOT` is not your answer. MSSQL requires that column names are known from the SQL text alone (without execution). Consider dynamic SQL generation - an example is [here](https://stackoverflow.com/questions/14797691/dynamic-pivot-columns-in-sql-server). – Seva Alekseyev Jan 24 '23 at 19:15

0 Answers0