Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 975451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:35:13+00:00 2026-05-16T03:35:13+00:00

I’m using SQLServer2008 Express I have a table and a script to convert the

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T03:35:13+00:00Added an answer on May 16, 2026 at 3:35 am

    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  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I have thousands of HTML files to process using Groovy/Java and I need to
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.