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 772285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T18:47:01+00:00 2026-05-14T18:47:01+00:00

Sorry for the lame title, my descriptive skills are poor today. In a nutshell,

  • 0

Sorry for the lame title, my descriptive skills are poor today.

In a nutshell, I have a query similar to the following:

SELECT P.LAST_NAME, P.FIRST_NAME, D.DEMO_GROUP
FROM PERSON P
JOIN PERSON_DEMOGRAPHIC PD ON PD.PERSON_ID = P.PERSON_ID
JOIN DEMOGRAPHIC D ON D.DEMOGRAPHIC_ID = PD.DEMOGRAPHIC_ID

This returns output like this:

LAST_NAME      FIRST_NAME     DEMO_GROUP
---------------------------------------------
Johnson        Bob            Male
Smith          Jane           Female
Smith          Jane           Teacher
Beeblebrox     Zaphod         Male
Beeblebrox     Zaphod         Alien
Beeblebrox     Zaphid         Politician

I would prefer the output be similar to the following:

LAST_NAME      FIRST_NAME     Male           Female         Teacher        Alien          Politician
---------------------------------------------------------------------------------------------------------
Johnson        Bob            1              0              0              0              0
Smith          Jane           0              1              1              0              0
Beeblebrox     Zaphod         1              0              0              1              1

The number of rows in the DEMOGRAPHIC table varies, so I can’t say with certainty how many columns I need. The query needs to be flexible.

Yes, it would be trivial to do this in code. But this query is one piece of a complicated set of stored procedures, views, and reporting services, many of which are outside my sphere of influence. I need to produce this output inside the database to avoid breaking the system. Any ideas?

This is MS SQL Server 2005, by the way.

Thanks.

  • 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-14T18:47:02+00:00Added an answer on May 14, 2026 at 6:47 pm

    You can use the PIVOT function. Here´s a piece of code. This function needs the columns in advance, but if you do not know the number of columns, you should make a dynamic sql query. Take a look at this answer.

    SELECT LAST_NAME, FIRST_NAME, [Male], [Female], [Alien], [Politician], [Teacher]
    FROM 
    (SELECT LAST_NAME, FIRST_NAME, DEMO_GROUP
    FROM Person) p
    PIVOT
    (
    COUNT (DEMO_GROUP)
    FOR DEMO_GROUP IN
    ( [Male], [Female], [Alien], [Politician], [Teacher] )
    ) AS pvt
    ORDER BY LAST_NAME
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry about the lame title. I have a project I'm working on and I
First off, sorry for the lame title, but I couldn't think of a better
Sorry if the title is not clear enough, I have <a> elements with the
Sorry for the lame title: I'm not sure how to sum this one up...
Sorry for such a lame title, but I just had no idea what to
(Sorry about the title, couldn't think of how to explain it) So I have
Sorry the title isn't more help. I have a database of media-file URLs that
Sorry if the title is poorly descriptive, but I can't do better right now
I am new to php, so sorry if this is lame. I have a
sorry for the lame question but with HTTP handler it is easy to do

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.