I'm trying to create a dynamic pivot table to get results in different columns rather than rows.
This is the table I'm using to test
CREATE TABLE [dbo].[Authors](
[Client_Id] [nvarchar](50) NOT NULL,
[Project_Id] [nvarchar](50) NOT NULL,
[Person_Id] [int] NOT NULL,
[Author_Number] [int] NOT NULL,
[Family_Name] [nvarchar](50) NULL,
[First_Name] [nvarchar](50) NULL,
)
INSERT INTO Authors (Client_Id, Project_Id, Person_Id, Author_Number, Family_Name, First_Name)
VALUES ('TEST','TEST',12345,1,'Giust','Fede')
INSERT INTO Authors (Client_Id, Project_Id, Person_Id, Author_Number, Family_Name, First_Name)
VALUES ('TEST','TEST',12345,2,'Giust','Fede')
INSERT INTO Authors (Client_Id, Project_Id, Person_Id, Author_Number, Family_Name, First_Name)
VALUES ('TEST','TEST',12346,1,'Giust','Fede')
INSERT INTO Authors (Client_Id, Project_Id, Person_Id, Author_Number, Family_Name, First_Name)
VALUES ('TEST','TEST',12346,2,'Giust','Fede')
INSERT INTO Authors (Client_Id, Project_Id, Person_Id, Author_Number, Family_Name, First_Name)
VALUES ('TEST','TEST',12346,3,'Giust','Fede')
So far I get the results like this
CLIENT_ID PROJECT_ID PERSON_ID AUTHOR_NUMBER FAMILY_NAME FIRST_NAME
TEST TEST 12345 1 Giust Fede
TEST TEST 12345 2 Ma Ke
TEST TEST 12346 1 Jones Peter
TEST TEST 12346 2 Davies Bob
TEST TEST 12346 3 Richards Craig
I need the results to come out like this, and to be dynamic because sometime I can have 2 authors, or 10 authors.
CLIENT_ID PROJECT_ID PERSON_ID FAMILY_NAME_1 FIRST_NAME_1 FAMILY_NAME_2 FIRST_NAME_2 FAMILY_NAME_3 FIRST_NAME_3
TEST TEST 12345 Giust Fede Ma Ke
TEST TEST 12346 Jones Peter Davies Bob Richards Craig
I've been trying to use this code, but keep getting errors