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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:51:28+00:00 2026-05-29T09:51:28+00:00

First, I am a SQL noob, second if there is a better solution that

  • 0

First, I am a SQL noob, second if there is a better solution that can be implement in code later, I’d love to hear it. Also, the data is currently stored in MySQL but will eventually have to be ported to MSSQL so a cross DB solution would be best (if one exists).

Now, the problem, my simplified data looks like this:

[STYLES]
ID   NAME
1    A Style
2    B Style
...
N    N Style

[EQUIPMENT]
ID   NAME
1    A Equipment
2    B Equipment
...
N    N Equipment

[AVAILABILITY]
STYLE  EQUIPMENT  TYPE
1      1          Standard
1      2          Optional
2      1          Optional
... #items will be missing and represent not available
2      2          Standard

Now I need a table that looks like this:

[DESIRED_VIEW]
EQUIPMENT_NAME   A_STYLE_TYPE  B_STYLE_TYPE ... N_STYLE_TYPE
A Equipment      Standard      Optional     ... NULL
B Equipment      Optional      NULL         ... Standard

I have seen a number of simple pivot examples and they all rely on having a set number of columns. Is there a way to set up the view with a variable number of columns based on how many rows are in the STYLES table?

As a note, I am using Visual Studio to create the Data xsd and letting it auto-generate the table fill methods then displaying the info in WPF DataGrids so being able to bind directly to a view with the correct data would be ideal.

  • 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-05-29T09:51:30+00:00Added an answer on May 29, 2026 at 9:51 am

    No – there is no way to write a query that has a dynamic number of columns.

    Your choices are (in order of preference):

    1. write a query that has “a lot” of columns, some of which may be blank (easy to write and use, but there would be a hard maximum on the pivot width)
    2. write app code that writes the dynamic query for you (not too bad: portable)
    3. write a stored procedure to create the SQL (terrible choice: painful to write logic code in PL/SQL and totally non-portable)

    If you are comfortable with a maximum number of styles (it can be arbitrarily large, but finite), code your pivot would look like this:

    select
      e.name as equipment_name,
      a1.type as a_style_type,
      a2.type as b_style_type,
      a3.type as c_style_type,
      ...
      an.type as n_style_type
    from equipment e
    left join availability a1 on a1.equipment = e.id and a1.style = 1
    left join availability a2 on a2.equipment = e.id and a2.style = 2
    left join availability a3 on a3.equipment = e.id and a3.style = 3
    ...
    left join availability an on an.equipment = e.id and an.style = 999;
    

    When there is no availability type for a given style, you’ll get a NULL.

    Also, you need to know the style ids and they need to be the same on all environemtns. If this is not the case (eg different in test/prod environments) you can change a1.style = 1 to a1.style = (select id from style where name = 'A Style') etc – it will still perform OK.

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

Sidebar

Related Questions

Do you refactor your SQL first? Your architecture? or your code base? Do you
First some background: VB.NET 2005 Application that accesses a MS-SQL back-end, using multiple Web
in an sql table there's an id, first name and last name field. i'd
I've noticed that the first sql statement with LIKE on a large table runs
I'm using EF CTP5 code-first with Sql CE 4.0. I am trying to perform
Is this possible? I have a web application the uses EF code-first and SQL
There are only 3 files that can be created : File_1, File_2 and File_3.
Note that I'm a complete SQL noob and in the process of learning. Based
I'm starting my first Linq to SQL project in VB.NET (which I am also
It's my first LINQ TO SQL Project , So definitely my question could be

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.