rows into columns

  • HI

    can somebody help me with this query

    i have table which has records like below

    A 1

    B 2

    C 1

    D 3

    . .

    . .

    . .

    . .

    AND WOULD LIKE TO SOME THEM AS

    A B C D . . . . .

    1 2 3 4 . . . . .

    TODAY IT IS ONLY A B C D COLUMNS

    BUT TOMMORROW IT MIGH TBE A B C D E ... COLUMNS

    IS THERE ANY DYNAMIC WAY OF DOING THIS

    can somebody please help me on this

  • Hi,

    i believe u need to use crosstab queries. Here is 2 link which will be useful for you.

    http://www.sqlmag.com/Articles/Index.cfm?ArticleID=15608&DisplayTab=Article

    http://www.simple-talk.com/sql/t-sql-programming/creating-cross-tab-queries-and-pivot-tables-in-sql/

    regards,

    vijay

  • As far as I understood your problem...

    CREATE TABLE [dbo].[tblABC](

    [C1] [varchar](50) NULL,

    [V1] [varchar](50) NULL

    )

    Declare @sSqlC varchar(1000)

    select @sSqlC = COALESCE(@sSqlC + ',','') + ' ''' + cast(tblABC.[V1] as varchar(20)) + ''' As ' + C1 from tblABC

    set @sSqlC = 'Select ' + @sSqlC

    Exec(@sSqlC)

    I hope it will help...

    Atif Sheikh

    ----------------------------------------------------------------------------------------------------------------------------------------------------------------------
    Sometimes, winning is not an issue but trying.
    You can check my BLOG
    [font="Arial Black"]here[/font][/url][/right]

  • If you'd like to avoid the unpleasantries of dynamic sql for crosstab queries check out the RAC utility. Coding dynamic crosstabs of complexity is like watching sausage made. It can take the appetite away from a normal human being. Save the prozac for something really important :). Check out RAC @

    www.rac4sql.net

    For developers who have smarts and common sense visit:

    www.beyondsql.blogspot.com

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply