Results 1 to 5 of 5

Thread: [MS SQL 2005]Ways to turn rows into columns

  1. #1

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    [MS SQL 2005]Ways to turn rows into columns

    We are creating a web app for doing teacher gradebooks.

    Each student appears in the gradebook for a class once - and has many columns. And to make matters worse each gradebook has a variable number of columns.

    The data for each cell of the grid is stored in it's own row - like this:

    Code:
    StuId Column Score
      1      1     99
      1      2     98
      1      4     97
      2      1     88
      2      3     87
      3      1     50
      3      2     51
      4      4    100
    What I need to give the web page is a recordset that looks like this

    Code:
    StuId Col1 Col2 Col3 Col4 Col5 Col6 ...
      1    99   98        97
      2    88        87
      3    50   51
      4                  100
    Any suggestions on how to accomplish this would be appreciated!

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  2. #2
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: [MS SQL 2005]Ways to turn rows into columns

    I've never done this but I think PIVOT and UNPIVOT functions are there for similar purpose. Didi u have a look at them?
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  3. #3

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: [MS SQL 2005]Ways to turn rows into columns

    From what I can see PIVOT does an AGGREGATE function - which I don't need.

    And of course I have the issue of a "variable" number of columns for each gradebook.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  4. #4
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: [MS SQL 2005]Ways to turn rows into columns

    This would give you the results you are expecting:


    SELECT * FROM StudentScore
    PIVOT (MAX(Score) FOR [Column] IN ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10])) PT
    Last edited by Pradeep1210; Jul 14th, 2009 at 05:53 AM.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  5. #5

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: [MS SQL 2005]Ways to turn rows into columns

    I'm leaning towards using that method.

    But to be even less elegant...

    Having 5 sets of temporary tables to PIVOT into - one with 20 columns, one with 40, 60, 80 and also one with 100 columns.

    Pivoting into the correct temp tables takes just 5 IF/blocks using a value of the MAX(COLUMN) from the class we are working with.

    Then we select from whatever temp table we built - and our webpage is hardwired to look at 20, 40, 60, 80 and 100 column recordsets.

    It loads the data into the webpage with this anyway

    Code:
            'then the row data
            JSON = AddObjectItemWithArrayToJSON(JSON, "Values")
            For i = 0 To dtx.Rows.Count - 1
                For i2 = 1 To dtx.Columns.Count - 1
                    JSON = AddArrayItemToJSON(JSON, dtx.Rows(i).Item(i2).ToString)
                Next
            Next
            JSON = EndArrayInJSON(JSON)

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width