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

  • Home
  • SEARCH
  • 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 3397204
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:30:01+00:00 2026-05-18T04:30:01+00:00

this will be the first question I’ve posted here so pardon any unintended lapses

  • 0

this will be the first question I’ve posted here so pardon any unintended lapses in board etiquette.

In my current project, I’ve taken a large, non-normalized table and broken it into four separate, normalized tables. My ultimate goal that I’m reaching out to this board for is to create a view which mimics the non-normalized table for backwards compatibility.

To provide a simplified snapshot of my scenario, the crux of what I’m trying to do lies in two tables:

ASSOC_ROLE          ASSOCIATE
----------          ----------
assoc_id (fk)       assoc_id (pk)
role_id  (fk)       last_name
org_nbr  (fk)

So if I issue the following query…

SELECT Assoc_Role.org_nbr, Assoc_Role.assoc_id, Associate.last_name, Assoc_Role.role_id
FROM Assoc_Role INNER JOIN
     Associate ON Assoc_Role.assoc_id = Associate.assoc_id
WHERE Assoc_Role.org_nbr = '1AA'

…I get the following result set

org_nbr     assoc_id     last_name     role_id
-------     --------     ---------     -------
1AA         1447         Cooper        1
1AA         1448         Collins       3
1AA         1448         Collins       4
1AA         1448         Collins       5
1AA         1449         Lynch         6

Ultimately, the view I would like to construct would look something like this:

org_nbr   role1_ID   role1_name   role2_ID   role2_name   role3_ID   role3_name   role4_ID   role4_name   role5_ID   role5_name   role6_ID   role6_name
-------   --------   ----------   --------   ----------   --------   ----------   --------   ----------   --------   ----------   --------   ----------
1AA       1447       Cooper       NULL       NULL         1448       Collins      1448       Collins      1448       Collins      1449       Lynch

My initial thought was to try to use the PIVOT command, but my understanding is that PIVOT requires some kind of aggregation, and that doesn’t fit my scenario. I’ve also played around with the CASE command in the SELECT clause, but it doesn’t flatten my result set down to one record.

Hopefully someone can shed some light on how I can accomplish this. Let me know if anyone needs more info. Thanks!

Scot

  • 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-18T04:30:02+00:00Added an answer on May 18, 2026 at 4:30 am

    To get the basic numbered-role data, we might start with

    SELECT
        org_nbr
        , r1.assoc_id   role1_ID
        , r1.last_name  role1_name
        , r2.assoc_id   role2_ID
        , r2.last_name  role2_name
        , r3.assoc_id   role3_ID
        , r3.last_name  role3_name
        , r4.assoc_id   role4_ID
        , r4.last_name  role4_name
        , r5.assoc_id   role5_ID
        , r5.last_name  role5_name
        , r6.assoc_id   role6_ID
        , r6.last_name  role6_name
    FROM
        ASSOC_ROLE ar
        LEFT JOIN ASSOCIATE r1 ON ar.role_id = 1 AND ar.assoc_id = r1.assoc_id
        LEFT JOIN ASSOCIATE r2 ON ar.role_id = 2 AND ar.assoc_id = r2.assoc_id
        LEFT JOIN ASSOCIATE r3 ON ar.role_id = 3 AND ar.assoc_id = r3.assoc_id
        LEFT JOIN ASSOCIATE r4 ON ar.role_id = 4 AND ar.assoc_id = r4.assoc_id
        LEFT JOIN ASSOCIATE r5 ON ar.role_id = 5 AND ar.assoc_id = r5.assoc_id
        LEFT JOIN ASSOCIATE r6 ON ar.role_id = 6 AND ar.assoc_id = r6.assoc_id
    

    BUT this will give us, for each org_nbr, a separate row for each role_id that has data! Which is not what we want – so we need to GROUP BY org_nbr. But then we need to either GROUP BY or aggregate over every column in the SELECT list! The trick then is to come up with an aggregate function that will placate SQL Server and give us the results we want. In this case, MIN will do the job:

    SELECT
        org_nbr
        , MIN(r1.assoc_id)   role1_ID
        , MIN(r1.last_name)  role1_name
        , MIN(r2.assoc_id)   role2_ID
        , MIN(r2.last_name)  role2_name
        , MIN(r3.assoc_id)   role3_ID
        , MIN(r3.last_name)  role3_name
        , MIN(r4.assoc_id)   role4_ID
        , MIN(r4.last_name)  role4_name
        , MIN(r5.assoc_id)   role5_ID
        , MIN(r5.last_name)  role5_name
        , MIN(r6.assoc_id)   role6_ID
        , MIN(r6.last_name)  role6_name
    FROM
        ASSOC_ROLE ar
        LEFT JOIN ASSOCIATE r1 ON ar.role_id = 1 AND ar.assoc_id = r1.assoc_id
        LEFT JOIN ASSOCIATE r2 ON ar.role_id = 2 AND ar.assoc_id = r2.assoc_id
        LEFT JOIN ASSOCIATE r3 ON ar.role_id = 3 AND ar.assoc_id = r3.assoc_id
        LEFT JOIN ASSOCIATE r4 ON ar.role_id = 4 AND ar.assoc_id = r4.assoc_id
        LEFT JOIN ASSOCIATE r5 ON ar.role_id = 5 AND ar.assoc_id = r5.assoc_id
        LEFT JOIN ASSOCIATE r6 ON ar.role_id = 6 AND ar.assoc_id = r6.assoc_id
    GROUP BY
        org_nbr
    

    Output:

    org_nbr    role1_ID    role1_name role2_ID    role2_name role3_ID    role3_name role4_ID    role4_name role5_ID    role5_name role6_ID    role6_name
    ---------- ----------- ---------- ----------- ---------- ----------- ---------- ----------- ---------- ----------- ---------- ----------- ----------
    1AA        1447        Cooper     NULL        NULL       1448        Collins    1448        Collins    1448        Collins    1449        Lynch
    Warning: Null value is eliminated by an aggregate or other SET operation.
    

    Of course this will fall short should the maximum role_id increase…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This will be my first iOS app with any bit of complexity. I'd like
This is my first question so I will do my best to conform to
My first question here. The question is similar to this one: PHP: Retrying a
This is my first question in stackoverflow, so I will be quick and precise.
this is my first question here; hope I'll be clear enough... I've got this
This is my first question here, if I did something wrong, tell me... I'm
I know one awkward solution for this taks will be : first use ct
I'm fairly new to writing test cases and this will be my first major
This is the first time I have attempted to create a .dll, that will
This is my first time on this site. I will ask you to help

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.