Results 1 to 4 of 4
  1. #1
    Join Date
    May 2011
    Posts
    2

    Unanswered: Display Data of a Table vertically in sql server

    Hi,

    I am a beginner to sqlserver.here is my problem.
    i have a table data as below
    Id Period1 Period2 Period3 Period4 Period5
    5 09.30-10.20 10.20-11.10 11.10-12.00 12:00-12:50 12.50-01.40

    i want it to be displayed as
    09.30-10.20
    10.20-11.10
    11.10-12.00
    12:00-12:50
    12.50-01.40

    i have tried using row_number but i didnt get as i required.

    any suggestions??

    thanks in advance

    Anupama

  2. #2
    Join Date
    May 2011
    Location
    Boston, MA area
    Posts
    4

    Using unpivot should work

    Hi,

    The following SQL should work to flip the data. If you are displaying the values for multiple ids, you probably want to add id to the select list as well.
    Code:
    SELECT PeriodValues
    FROM 
       (SELECT id, period1, period2, period3, period4, period5
       FROM Periods) p
    UNPIVOT
       (PeriodValues FOR IDVal IN 
          (period1, period2, period3, period4, period5)
    )AS unpvt
    where id = 5
    I hope this helps.

    Visit http://www.helpwithsql.com
    Last edited by Andrew Zwicker; 05-14-11 at 10:33. Reason: Small edit.

  3. #3
    Join Date
    May 2011
    Posts
    2

    Display Data of a Table vertically in sql server

    Thanks for the quick reply...

    It was really helpful

  4. #4
    Join Date
    Aug 2004
    Location
    Dallas, Texas
    Posts
    831
    Wouldn't it be easier to build the table with id,period and populate?

    Code:
    create table time_periods (id int,time varchar(15))
    insert into time_periods values (1,'09.30-10.20')
    insert into time_periods values (2,'10.20-11.10')
    insert into time_periods values (3,'11.10-12.00')
    insert into time_periods values (4,'12:00-12:50')
    insert into time_periods values (5,'12.50-01.40')
    
    select * from time_periods
    
    drop table time_periods

Tags for this Thread

Posting Permissions

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