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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:31:52+00:00 2026-06-18T03:31:52+00:00

Is it possible to define a fallback or an else case for a PIVOT

  • 0

Is it possible to define a “fallback” or an “else” case for a PIVOT operation? Let me play with the sample from MSDN:

-- Pivot table with one row and five columns
SELECT
    'AverageCost' AS Cost_Sorted_By_Production_Days, 
    [0], [1], [2], [3], [4],
    [??magicalELSEkeyword??] as 'too many days'

FROM
(
    SELECT DaysToManufacture, StandardCost 
    FROM Production.Product
) AS SourceTable

PIVOT
(
    AVG(StandardCost)
    FOR DaysToManufacture IN
    (
        [0], [1], [2], [3], [4],
        [??magicalELSEkeyword??]
    )
) AS PivotTable;

Pivot behavior is quite obvious – it selects/case/groups over the occurences of specified list of values. What I’d like to see is a “fallback” case. In the above query, I’d like to fetch:

  • average cost for ‘instantaneous’ 0-day process
  • average cost for 1-day process
  • …
  • average cost for 4-day process
  • average cost for all the others

Please note that the list of cases “wanted” is constant and known. I do not want to produce pivot of an unknown-number-of-columns. I just want to have all nonmatching values grouped as simple result named “others”

I’ve not seen any such possibility in the T-SQL syntax reference on MSDN, I just find it hard to believe this is not available.. It seems to be such useful feature and trivial to implement, that it just hurts to ommit it..

Yep, I know that it would probably introduce a discussion whether and how to handle NULL values that appear in the ‘other’, but that’s just two special cases.. they could be ignored like in any aggregating function — that would be trivial to implement, and 100% fine and consistent with rest of SQL — or could be handled by two different fallback keywords like ELSE-NOT-NULL vs. ELSE-OR-NULL..

Is this feature really missing?

  • 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-18T03:31:53+00:00Added an answer on June 18, 2026 at 3:31 am

    Unfortunately, it is not possible to place the other in an ELSE statement so I would put the CASE in your subquery. Then you will PIVOT on those new values:

    select *
    from
    (
      select StandardCost,
        case DaysToManufacture
          when 0 then '0'
          when 1 then '1'
          when 2 then '2'
          when 3 then '3'
          when 4 then '4'
          else 'Others' end as DaysToManufacture
      from Product
    ) src
    pivot
    (
      avg(StandardCost)
      for DaysToManufacture in ([0], [1], [2], [3], [4], [Others])
    ) piv
    

    See SQL Fiddle with Demo

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

Sidebar

Related Questions

Is it possible to define an instance method in ruby from a string (the
Is it possible to define an interface where one of the objects is a
Is it possible to define more than one destination folder in Inno Setup script?
Is it possible to define more than one conditional in one {$IFDEF} directive ?
is it possible to define in a spring context file, and one or more
Is it possible to define two operation contracts with the same paramiters? I want
Possible Duplicate: Is it possible to define more than one function per file in
Is it (in a performant way) possible to define fallback methods in JavaScript? For
Is it possible to define more then one function in AJAX Enabled WCF Service
Is it possible to define a timestamp column in a MySQL table that will

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.