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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T01:22:30+00:00 2026-06-09T01:22:30+00:00

I am working on an sql server 2008 database. I have a table like

  • 0

I am working on an sql server 2008 database.

I have a table like this”

Id     Year    Series  Value
----+------+--------+------
1     1990    a       1.5
1     1990    b       1.6
1     1990    c       1.7
1     1991    a       1.8
1     1991    b       1.9
1     1991    c       2.5

Is there a query that can select the values and return them like this?

Year    a     b        c
------+------+--------+------
1990    1.5    1.6      1.7
1991    1.8    1.9      2.5

Thanks a lot for any help.

  • 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-06-09T01:22:31+00:00Added an answer on June 9, 2026 at 1:22 am

    If series is fixed to a,b,c you can do this:

    CREATE TABLE #t (Id    INT,  Year  INT,  
        Series VARCHAR(5), Value DECIMAL(10,1))
    INSERT #t
    VALUES
    (1,     1990,    'a',       1.5),
    (1,     1990,    'b',       1.6),
    (1,     1990,    'c',       1.7),
    (1,     1991,    'a',       1.8),
    (1,     1991,    'b',       1.9),
    (1,     1991,    'c',       2.5)
    
    SELECT  pvt.Year,
            pvt.a,
            pvt.b,
            pvt.c
    FROM    #t
    PIVOT (
        MIN(Value) FOR Series IN ([a], [b], [c])
    ) pvt
    

    If there would be other values you can use dynamic pivot:

    DECLARE @series VARCHAR(100) = 
        STUFF(( SELECT  DISTINCT ',[' + Series + ']'
                FROM    #t
                FOR     XML PATH(''))
                ,1, 1, '')
    
    DECLARE @query VARCHAR(2000) = '
    SELECT  pvt.Year, ' + @series +'
    FROM    #t
    PIVOT (
        MIN(Value) FOR Series IN (' + @series + ')
    ) pvt
    ';
    
    EXEC(@query)
    

    In a scenario with fixed series, there is also possibility with CROSS JOIN:

    SELECT  a.Year, 
            MAX(CASE WHEN a.Series = 'a' THEN a.Value END) a,
            MAX(CASE WHEN a.Series = 'b' THEN a.Value END) b,
            MAX(CASE WHEN a.Series = 'c' THEN a.Value END) c
    FROM    #t a
    CROSS   JOIN #t b
    GROUP   BY a.Id, a.Year
    ORDER   BY a.Year
    
    • 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. I have a database table that looks like this
I have a table in SQL Server 2008 that looks kind of like this:
I'm working on a SQL Server 2008 database. This database has two tables. Book
I have an audit table in my SQL Server 2008 database which contains the
Working with SQL Server 2008 R2. I have a table of hierarchical data, with
I have a live SQL Server 2008 database and I need to start working
Ii have a database (sql server 2008) and I am working with c# in
Hi friends i am working on export excel rows to Sql Server 2008 Table
I'm working on a project involving C# and a SQL Server 2008 database. In
I have two tables in a SQL Server 2008 database in my company. The

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.