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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:12:49+00:00 2026-06-11T11:12:49+00:00

I am trying to pivot a table into a view which would be more

  • 0

I am trying to pivot a table into a view which would be more easily manipulable with Excel. I am creating views in Sql Server that can be read by the accounting department in order to create stats and other cross-table pivot in Excel.

The table in its current format

OccupationId | ProductId | IsGroup | IsAdditionalNight | Price
-------------+-----------+---------+-------------------+------
1            | 47        | 0       | 0                 | 50
2            | 47        | 0       | 1                 | 45
3            | 47        | 1       | 0                 | 30
4            | 47        | 1       | 1                 | 28

The View format that I want to expose

ProductId | Price | AdditionalNight | GroupPrice | GroupAdditionalNight
----------+-------+-----------------+------------+---------------------
47        | 50    | 45              | 30         | 28

How can I achieve that in Sql Server 2008 R2 ? I need the query to be fast since it will be part of a bigger Query (using CTE).

Currently the only way that I am thinking is to include 4 sub queries to get all 4 prices individually, but I don’t like the approach and it scan the table 4 times.

  • 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-11T11:12:50+00:00Added an answer on June 11, 2026 at 11:12 am
    SELECT ProductId,
    MAX(CASE 
     WHEN IsGroup=0 AND IsAdditionalNight =0 then Price
     END) as [Price],
    MAX(CASE 
     WHEN IsGroup=0 AND IsAdditionalNight =1 then Price
     END) as [AdditionalNight],
    MAX(CASE 
     WHEN IsGroup=1 AND IsAdditionalNight =0 then Price
     END) as [GroupPrice],
    MAX(CASE 
     WHEN IsGroup=1 AND IsAdditionalNight =1 then Price
     END) as [GroupAdditionalNight] 
    
     FROM table1     
    GROUP BY ProductId
    

    Or you can do the same using PIVOT (Personally I found the syntax of PIVOT more complex than approach above, but there is no performance difference, and some people prefer PIVOT).

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

Sidebar

Related Questions

I am trying to create a pivot table type view in postgresql and am
I'm trying to pivot some data in SQL Server 2000, but the user that
I'm trying out the pivot function in SQL Server 2005 for the first time
I am using excel 2010. I am trying to create pivot table between two
I am creating a pivot table excel sheet in my ASP .NET web application
Possible Duplicate: T-SQL Pivot? Possibility of creating table columns from row values I've been
Trying to find some simple SQL Server PIVOT examples. Most of the examples that
Whilst trying to pivot a sql table I came across this post Here .
I am trying to get a handle on manipulating table data into more visually
I am trying to make a pivot table with rollup and a case statement

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.