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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:51:31+00:00 2026-06-01T20:51:31+00:00

Please help me with this. I am totally stuck. I have coders block or

  • 0

Please help me with this. I am totally stuck. I have coders block or something.

I have the following table

ID     Name        Cost     Included
----   ----------  -------  ----------
1      Package1    10.00    Yes
2      Package2    20.00    No
3      Package3    20.00    Yes

I would like to crosstab this information, to display like the following example,there will be more columns in the table.

Type        Package1     Package2       Package3
-----       ------------ -----------    ----------
Name        Package1     Package2       Package3
Cost        10.00        20.00          30.00
Included    Yes          No             Yes
  • 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-01T20:51:33+00:00Added an answer on June 1, 2026 at 8:51 pm

    It seems to me that you are trying to build a product comparison list. If this is true, you might unpivot the table first and then join individual records together.

    The ‘transponded’ part unpivots the columns. All columns must be of compatible types or converted to one. I choose varchar(100). transponded returns table with three columns, ID from ProductInfo, Type as column name and Value as value of corresponding column.

    Select part joins together info on as many product as demanded by adding another left join transponded tn on t1.Type = tnType and tn.ID = @parametern. This part seems as a hassle, but when I tried to do this part with pivot I failed to get column in proper order – pivot sorted names in Type. It would however demand dynamic sql generation. This solution is fixed providing that you add enough joins for maximum products you wish to compare at once. I belive it would not be over 5.

    =1, =2 and =3 should be replaced by parameters. The query should be hosted in stored procedure.

    ; with transponded as 
    (
      select ID, Type, Value
        from 
        (
          select ID, 
                 Name, 
                 cast (Cost as varchar(100)) Cost, 
                 cast (case when Included = 1 then 'Yes' else 'No' end as varchar(100)) Included
            from ProductInfo
        ) p
          unpivot (Value for Type in (Name, Cost, Included) ) a
    )
    select t1.Type, 
           t1.Value Product1, 
           t2.Value Product2, 
           t3.Value Product3
      from transponded t1
      left join transponded t2
             on t1.Type = t2.Type
            and t2.id = 2
       left join transponded t3
         on t1.Type = t3.Type
        and t3.id = 3
      where t1.id = 1
    

    In short, transpond one record at time and join to another transponded record by Type column.

    Oh, and here is a Sql Fiddle playground.

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

Sidebar

Related Questions

Please help this beginner here... I have a SQL Server 2008 R2 running on
Please help me about this issue... In my application i have calender where user
can you please help me with this issue the String class does not have
Can anybody please help me with this as I have no idea why public
Could anyone please help me convert this code to vb.net, I have tried it
Please help this would be my last problem in dealing with access database with
Please help me with this issue that I am facing on production server. I
Please help me understand this recursive function... var stack = Array; function power(base, exponent){
Can anyone please help me with this...I'm not very good with regular expressions and
Can any one please help me solve this. I am resizing some flash object/embed

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.