Select return dynamic columns

Posted by Ascalonian on Stack Overflow See other posts from Stack Overflow or by Ascalonian
Published on 2010-04-28T13:08:19Z Indexed on 2010/04/28 13:13 UTC
Read the original article Hit count: 244

Filed under:
|
|

I have two tables: Standards and Service Offerings. A Standard can have multiple Service Offerings. Each Standard can have a different number of Service Offerings associated to it.

What I need to be able to do is write a view that will return some common data and then list the service offerings on one line. For example:

Standard Id | Description | SO #1 | SO #2 | SO #3 | ... | SO #21 | SO Count
1           | One         | A     | B     | C     | ... | G      |  21
2           | Two         | A     |       |       | ... |        |  1
3           | Three       | B     | D     | E     | ... |        |  3

I have no idea how to write this. The number of SO columns is set to a specific number (21 in this case), so we cannot exceed past that.

Any ideas on how to approach this?

A place I started is below. It just returned multiple rows for each Service Offering, when they need to be on one row.

SELECT *
  FROM SERVICE_OFFERINGS
 WHERE STANDARD_KEY IN (SELECT STANDARD_KEY
                          FROM STANDARDS)

© Stack Overflow or respective owner

Related posts about sql-server

Related posts about views