I am using SQLServer2008 Express
. I have a table and a script to convert rows to columns with the expected result in the form
PARENT_ID name01 name02 name03 name04
1 ABC DEF ABC2 DEF2
2 DEF3 null null null
However, I get the result as
PARENT_ID name01 name02 name03 name04
1 ABC DEF ABC2 DEF2
2 null null null null
I know that something is wrong with the code, I just can’t understand. Hope someone can help.
CREATE TABLE
parent_id bigint NOT NULL
,dependent_id bigint not null
,date_of_birth date not null
,last_name varchar(100)
,first_name varchar(100)
,effective_start_date date
,effective_end_Date date
)
insert into
insert into
insert into
insert into
insert into
SELECT PARENT_ID
,[1] as name01
,[2] as name02
,[3] as name03
,[4] as name04
FROM ( SELECT top(100) percent
PARENT_ID
, dependent_id
, (isnull(first_name,'')+last_name) as fullname
FROM
where GETDATE() between effective_start_date and effective_end_Date
order by date_of_birth
) AS piv
PIVOT ( max(fullname)
FOR dependent_id IN ([1], [2], [3], [4])
) AS chld
Thanks Elmer
source
share