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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:59:33+00:00 2026-06-18T04:59:33+00:00

I am using MS Sql2008, Table1: Plan.Plan Table2: Plan.PlanFeature Table3:Plan.PlanDetails Plan.Plan PlanID_PK PlanName AnnualPrice

  • 0

I am using MS Sql2008,

     Table1: Plan.Plan
     Table2: Plan.PlanFeature
     Table3:Plan.PlanDetails

    Plan.Plan

    PlanID_PK    PlanName  AnnualPrice  MonthlyPrice

  1                   Plan1            Free              Free
  2                   Plan2            $50.00          $4.99
  3                   Plan3            $100.00        $9.99

Plan.PlanFeature

PlanFeatureID_PK    FeatureName
- - - - - - - - - - - - - - - - - - - - - - - - - - - 
        1                           Feature1
        2                           Feature2
        3                           Feature3
        4                           Feature4


Plan.PlanDetails

PlanDetailsID_PK    PlanID_FK    PlanFeatureID_FK    Quantity     Quantity_Type
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - - - - - - - - - -
             1                        1                   1                                0               Included
             2                        1                   2                                0               Unlimited
             3                        1                   3                                2               None
             4                        1                   4                                0               Unlimited
             5                        2                   1                                0               Included
             6                        2                   2                                0               Unlimited
             7                        2                   3                                20               None
             8                        2                   4                                0               Unlimited
             9                        3                   1                                0               Included
             10                      3                   2                                0               Unlimited
             11                      3                   3                                >20               None
             12                      3                   4                                0               Unlimited




Output :

FeatureName     Plan1          Plan2         Plan3  
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
Feature1          Included      Included      Included
Feature2          Unlimited     Unlimited    Unlimited
Feature3              2                    20                >20
Feature4          Unlimited     Unlimited     Unlimited
AnnualPrice     Free             $50.00           $100.00
MonthlyPrice   Free             $4.99              $9.99

So here we go, For converting rows into columns I used COALESCE and Pivot

Here the stored procedure I am trying it,

DECLARE @cols NVARCHAR(2000)
DECLARE @SubjectQuery NVARCHAR(MAX)
SELECT  @cols = COALESCE(@cols + ',[' + PlanNames + ']',
                         '[' + PlanNames + ']')
FROM    Plans.Plans
ORDER BY PlanNames
SET @SubjectQuery = 'SELECT FeatureName, ' + @cols + '
FROM 
(select p.PlanId_PK, p.PlanNames,pf.FeatureName,pd.Quantity from Plans.PlanDetails pd 
join Plans.Plans p on pd.PlanId_FK=p.PlanId_PK 
join Plans.PlanFeatures pf on pd.PlanFeatureId_FK=pf.PlanFeatureId_PK  ) S
PIVOT
(
Count(Quantity) --Dont know which aggregate functions i have to use here according to my output
FOR PlanNames IN
(' + @cols + ')) AS pvt'
exec @SubjectQuery

For quantity I have to check condition like this MAX(CASE WHEN pd.Quantity = '0' THEN pd.Quantity_Type)as Quantity, if the quantity value is 0 then i have to display Quantity_type values

Inside the pivot block don’t know which aggregate function i have to use according to my output, can you please figure this out?

  • 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-18T04:59:34+00:00Added an answer on June 18, 2026 at 4:59 am

    Based on your sample data and the desire output, I think you will want to use the case inside of the subquery, before the pivot:

    DECLARE @cols NVARCHAR(2000)
    DECLARE @SubjectQuery NVARCHAR(MAX)
    SELECT  @cols = COALESCE(@cols + ',[' + PlanName + ']',
                             '[' + PlanName + ']')
    FROM    [Plans]
    ORDER BY PlanName
    
    
    SET @SubjectQuery 
       = 'SELECT FeatureName, ' + @cols + '
          FROM 
          (
            select p.PlanName,
              pf.FeatureName,
                  MAX(CASE WHEN pd.Quantity = ''0'' THEN pd.Quantity_Type else quantity end)as Quantity
            from PlanDetails pd 
            join [Plans] p 
              on pd.PlanId_FK=p.PlanId_PK 
            join PlanFeature pf 
              on pd.PlanFeatureId_FK=pf.PlanFeatureId_PK  
            group by p.PlanName, pf.FeatureName
          ) S
          PIVOT
          (
            max(Quantity)
            FOR PlanName IN (' + @cols + ')
          ) AS pvt'
    
    exec(@SubjectQuery);
    

    See SQL Fiddle with Demo.

    The result is:

    | FEATURENAME |     PLAN1 |     PLAN2 |     PLAN3 |
    ---------------------------------------------------
    |    Feature1 |  Included |  Included |  Included |
    |    Feature2 | Unlimited | Unlimited | Unlimited |
    |    Feature3 |         2 |        20 |       >20 |
    |    Feature4 | Unlimited | Unlimited | Unlimited |
    

    Edit, If you need the annual price and monthly price, then you will need to incorporate an unpivot and the pivot function:

    DECLARE @cols NVARCHAR(2000)
    DECLARE @SubjectQuery NVARCHAR(MAX)
    SELECT  @cols = COALESCE(@cols + ',[' + PlanName + ']',
                             '[' + PlanName + ']')
    FROM    [Plans]
    ORDER BY PlanName
    
    
    SET @SubjectQuery 
       = 'SELECT FeatureName,'+@cols+'
          FROM 
          (
              select PlanName, FeatureName, 
                  MAX(CASE WHEN Quantity = ''0'' THEN Quantity_Type else quantity end)as Quantity,
                  SortOrder
              from
              (
                  select p.PlanName, pf.FeatureName, pd.Quantity, pd.Quantity_Type, 1 Sortorder
                  from PlanDetails pd 
                  join [Plans] p 
                    on pd.PlanId_FK=p.PlanId_PK 
                  join PlanFeature pf 
                    on pd.PlanFeatureId_FK=pf.PlanFeatureId_PK  
                  union all
                  select PlanName, col, ''0'', value, 2 SortOrder
                  from
                  (
                      select PlanID_PK, PlanName,
                          AnnualPrice, 
                          cast(MonthlyPrice as varchar(6)) MonthlyPrice
                      from plans
                  ) src
                  unpivot
                  (
                      value
                      for col in (annualprice, monthlyprice)
                  ) unpiv
              ) pl
              group by PlanName, FeatureName, SortOrder
          ) d
          PIVOT
          (
              max(Quantity)
              FOR PlanName IN ('+@cols+')
          ) AS pvt
          order by SortOrder'
    
    exec(@SubjectQuery);
    

    See SQL Fiddle with Demo

    Result:

    |  FEATURENAME |     PLAN1 |     PLAN2 |     PLAN3 |
    ----------------------------------------------------
    |     Feature1 |  Included |  Included |  Included |
    |     Feature2 | Unlimited | Unlimited | Unlimited |
    |     Feature3 |         2 |        20 |       >20 |
    |     Feature4 | Unlimited | Unlimited | Unlimited |
    |  AnnualPrice |      Free |     50.00 |    100.00 |
    | MonthlyPrice |      Free |      4.99 |      9.99 |
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

HI all, Iam using VS2008 and SQL2008 i want to access my stored procedures
i´m using subsonic 3 trying convert a SQL2008 project to MySQL. when the projects
Using SQL2008, I'm trying to figure out an efficient query to find a row
I'm attempting to populate a DB on my local SQL2008 Server using a Data
I'm using SQL2008 to create SPs. In my procedure I want to compare three
I'm storing localized strings in a single datatable using MS Sql (2008 or whatever).
I'm using CF8 and SQL2000. I'm storing a bunch of HTML in a Text
I have an Execute SQL task (SQL 2008) where I'm using two SSIS variables
How can I perform an idempotent insert row using subsonic with a SQL 2008
Using a CSS image sprite, I'm creating an 'interactive' image where hovering over certain

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.