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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:37:16+00:00 2026-05-28T01:37:16+00:00

I’m using SQL Server 2008. I have data as in this table: Team Email

  • 0

I’m using SQL Server 2008. I have data as in this table:

Team    Email              Groups
------- ------------------ ------
|Team1|-|email0@email.com|-|A|
|Team1|-|email1@email.com|-|B|
|Team1|-|email2@email.com|-|C|
|Team2|-|email3@email.com|-|A|
|Team2|-|email4@email.com|-|B|
|Team2|-|email5@email.com|-|C|

I want to get the data in this format:

Team    A                  B                  C
------- ------------------ ------------------ ------------------
|Team1|-|email0@email.com|-|email1@email.com|-|email2@email.com|
|Team2|-|email3@email.com|-|email4@email.com|-|email5@email.com|

How can I achieve this?

  • 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-28T01:37:16+00:00Added an answer on May 28, 2026 at 1:37 am

    Using PIVOT You can do the following

    With SampleData AS 
    (
    SELECT 'Team1' as Team , 'email0@email.com' as email, 'A' as Groups
    UNION SELECT 'Team1' as Team , 'email1@email.com' as email, 'B' as Groups
    UNION SELECT 'Team1' as Team , 'email2@email.com' as email, 'C' as Groups
    UNION SELECT 'Team2' as Team , 'email3@email.com' as email, 'A' as Groups
    UNION SELECT 'Team2' as Team , 'email4@email.com' as email, 'B' as Groups
    UNION SELECT 'Team2' as Team , 'email5@email.com' as email, 'C' as Groups
    )
    SELECT Team, A, B,C FROM 
    (SELECT * FROM SampleData) source
    PIVOT
    (MAX(email) FOR Groups IN ([A], [B], [C]) )as pvt
    

    Produces

    Team  A                B                C
    ----- ---------------- ---------------- ----------------
    Team1 email0@email.com email1@email.com email2@email.com
    Team2 email3@email.com email4@email.com email5@email.com
    

    See a working Data.SE example

    In a DB that doesn’t support PIVOT you can instead do multiple joins to your table. Although you may want to anyway, since as GBN pointed out, since we’re not using an aggregate.

    With SampleData AS 
    (
    SELECT 'Team1' as Team , 'email0@email.com' as email, 'A' as Groups
    UNION SELECT 'Team1' as Team , 'email1@email.com' as email, 'B' as Groups
    UNION SELECT 'Team1' as Team , 'email2@email.com' as email, 'C' as Groups
    UNION SELECT 'Team2' as Team , 'email3@email.com' as email, 'A' as Groups
    UNION SELECT 'Team2' as Team , 'email4@email.com' as email, 'B' as Groups
    UNION SELECT 'Team2' as Team , 'email5@email.com' as email, 'C' as Groups
    )
    
    SELECT 
        source.Team,
        A.email,
        B.email,
        C.email
    FROM 
        (SELECT DISTINCT TEAM From SampleData) source
        LEFT JOIN SampleData A 
        ON source.Team = A.Team
         AND A.GROUPS = 'A'
        LEFT JOIN SampleData B 
        ON source.Team = B.Team
        AND B.GROUPS = 'B'
        LEFT JOIN SampleData C 
        ON source.Team = C.Team
        AND C.GROUPS = 'C'
    

    See a working Data.SE example

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

Sidebar

Related Questions

I'm using SQL Server 2008 and I have a table with three columns: Length
I am using SQL Server 2008 Enterprise. I have yearly customer data from 2000
I'm using sql server 2005/2008 and I have a table that it's PK (rec_index)
Using Microsoft SQL Server 2008. I have a table of interest rates. I've manually
I am using SQL Server 2008 R2. I have a table which has several
I'm using SQL Server 2008 Management studio viewing a 2005 server and have just
I am using SQL Server 2008 Enterprise. I have created a very simple test
Background: I'm using SQL Server Management Studio 2008 and I have a query window
I have a database that I have created using SQL Server Developer 2008. I
I have an asp.net web application written in C# using a SQL Server 2008

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.