views:

38

answers:

1

Hi Everyone,

I'm using SQLServer2008 Express
I have a table and a script to convert the rows into columns with expected result as

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 there's something wrong with the code, it's just that I can't figure out. Hope somebody can help.

CREATE TABLE #temp (
    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 #temp values (1,1,'1990-10-01','ABC',null,'1989-01-01','9999-12-31')  
insert into #temp values (1,2,'1991-06-02','DEF',null,'1989-01-01','9999-12-31')  
insert into #temp values (1,3,'1992-06-02','ABC2',null,'1989-01-01','9999-12-31')  
insert into #temp values (1,4,'1993-06-02','DEF2',null,'1989-01-01','9999-12-31')  
insert into #temp values (2,5,'2000-06-02','DEF3',null,'1989-01-01','9999-12-31')  

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 #temp  
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

+2  A: 

Something wrong with the data rather than the code I think. You are inserting 5 as the dependent_id but expecting it to appear in the 1 column?

You could maybe use dependent_id % 4 but I'm not really sure what the intention is here?

Edit Following your comment I think you need this?

SELECT PARENT_ID  
    ,[1] as name01  
    ,[2] as name02  
    ,[3] as name03  
    ,[4] as name04  
FROM 
(
  SELECT  
    PARENT_ID,
    ROW_NUMBER() OVER (PARTITION BY PARENT_ID ORDER BY date_of_birth) AS RN
   , (isnull(first_name,'')+last_name) as fullname  
FROM #temp  
where GETDATE() between effective_start_date and effective_end_Date  
) AS piv  
PIVOT ( max(fullname)  
  FOR RN IN ([1], [2], [3], [4])  
  ) AS chld  
Martin Smith
Hi Martin,The table consists of a maximum of four rows for a given parent_id.And that all 4 rows should become the 4 columns.The parent_id of the last insert statement differs from that of the 1st 4 insert statement.Thanks, Elmer
Elmer
Hi again Martin,What is that %4 stands for? That %4 worked.Regards,Elmer
Elmer
@Elmer Following your clarification I don't think you need `%4` (it is the modulo operator). I think you need to use `ROW_NUMBER` see edit.
Martin Smith
Thanks again Martin for supporting tsql beginners like me. This code worked. I hope I can also contribute to the community. Regards
Elmer