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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:09:51+00:00 2026-06-17T21:09:51+00:00

Hi I have the following table of data Class | Member | Value ———————-

  • 0

Hi I have the following table of data

Class | Member | Value
----------------------
c1    | m1     | 10
c1    | m2     | 20
c1    | list   | 30
c1    | list   | 40
c1    | list   | 50
c2    | m1     | 60
c2    | m2     | 70
c2    | list   | 80

and would like to pivot the data into this form

Class | Member 1 | Member 2 | List
----------------------------------
c1    | 10       | 20       | 30
c1    | 10       | 20       | 40
c1    | 10       | 20       | 50
c2    | 60       | 70       | 80

a normal pivot using max as the aggregate function would have given me

Class | Member 1 | Member 2 | List
----------------------------------
c1    | 10       | 20       | 50
c2    | 60       | 70       | 80

but I want each of the List values of any Class to be listed out.

Instead of finding alternatives by writing SQL queries full of CASEes e.g CASE Member WHEN ‘m1’ then Value, CASE Member WHEN ‘m2’ then Value, … to achieve what I want, I want to know if there is any chance to use pivot with some tweaks to make it work for my task?

The database is SQL 2008 R2

Thank you

  • 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-17T21:09:53+00:00Added an answer on June 17, 2026 at 9:09 pm

    No. PIVOT is effectively syntactic sugar for doing aggregates around CASE expressions. If the sugar doesn’t work, you need to go back to the longer form.

    The syntax for PIVOT is fully described in the FROM clause:

    <pivoted_table> ::=
        table_source PIVOT <pivot_clause> [ AS ] table_alias
    
    <pivot_clause> ::=
            ( aggregate_function ( value_column [ [ , ]...n ]) 
            FOR pivot_column 
            IN ( <column_list> ) 
        ) 
    

    And note that aggregate_function must be provided. All aggregate functions in SQL Server operate on any number of input values and produce a single output value. There’s no aggregate which can produce multiple output values, as you would require here.


    This gives the result you’ve asked for, but does rely on each Class value only having one row for each of m1 and m2:

    declare @t table (Class char(2) not null,Member varchar(4) not null,Value int not null)
    insert into @t(Class,Member,Value) values
    ('c1','m1',10),
    ('c1','m2',20),
    ('c1','list',30),
    ('c1','list',40),
    ('c1','list',50),
    ('c2','m1',60),
    ('c2','m2',70),
    ('c2','list',80)
    
    select l.Class,m1.Value as m1,m2.Value as m2,l.Value as list
    from
        @t l
            inner join
        @t m1
            on
                l.Class = m1.Class and
                m1.Member = 'm1'
            inner join
        @t m2
            on
                l.Class = m2.Class and
                m2.Member = 'm2'where
    l.Member='list'
    

    Result:

    Class m1          m2          list
    ----- ----------- ----------- -----------
    c1    10          20          30
    c1    10          20          40
    c1    10          20          50
    c2    60          70          80
    

    If there are multiple rows for m1 and m2, and you just want, say, the MAX values for them, then you’d make m1 and m2 in my above query subqueries:

    ...
        inner join
    (select Class,MAX(Value) from @t where Member='m1' group by Class) m1
        on
            l.Class = m1.Class
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following code to insert data into GiftCouponPayment table and Payment table. This
Have you got another idea to get this list of data? I would like
I have the following table template that is rendered through knockout: <table class=gv data-bind=visible:
I have some data in a table that looks roughly like the following: table
In my application I have some table data that looks like this: <table> <tr>
Suppose we have the following table data: ID parent stage submitted 1 1 1
I have the following table and data docRefID docExternalContent 1 'a' 1 'b' 1
I have the following table: CREATE TABLE [dbo].[Data] ( [Id] UNIQUEIDENTIFIER NOT NULL, [Data]
I have the following table: ID | JobID | Data | ResultType --------------------------------- 1
I currently have the following row in my table: course_data: user_id days <-- This

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.