multiple columns in one row

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nagarjunamukara
    New Member
    • Aug 2012
    • 1

    multiple columns in one row

    hey hi i have table like this
    a b c
    1 4 7
    2 5 8
    3 6 9

    i need that all data under a,b,c in once column of another name like this
    d
    1
    2
    3
    4
    5
    6
    7
    8
    9


    is there any possibility to get solution means please please reply to my mail...
  • ariful alam
    New Member
    • Jan 2011
    • 185

    #2
    try self union to show the columns in one column

    Comment

    • ck9663
      Recognized Expert Specialist
      • Jun 2007
      • 2878

      #3
      Option 1:
      Unpivot your data

      Option 2
      Use UNION ALL

      Option 3
      1. Concatenate your columns
      2. Try doing this...

      Happy Coding!!!


      ~~ CK

      Comment

      • nbiswas
        New Member
        • May 2009
        • 149

        #4
        Declare @t table(a int,b int,c int)
        Code:
        Insert Into @t select 1,4,7 union all select 2,5,8 union all select 3,6,9
        
        select d = a from @t union 
        select b from @t union 
        select c from @t
        ~~CK: How will you fit your first (Unpivot) and last Option (Concatenate your columns) in this case? Curious to know about this with working code
        Last edited by nbiswas; Aug 24 '12, 06:51 AM. Reason: A message for ~~CK

        Comment

        • ck9663
          Recognized Expert Specialist
          • Jun 2007
          • 2878

          #5
          It might actually the longest way to do it but it will still work...

          Here, try running this:

          Code:
          with x 
          as
          (
             select 1 as a, 4 as b, 7 as c
             union all 
             select 2, 5, 8
             union all
             select 3, 6, 9
          ) 
          select 
             d
          from   
           (
             select a, b, c
             from x
           ) p
           UNPIVOT
             (
               d for columnname in (a,b,c)
             ) un
          The last option will work in similar manner. I can post a sample code if you want, but it could be later :)

          Happy Coding!!!


          ~~ CK

          Comment

          • vickysatya
            New Member
            • Aug 2012
            • 1

            #6
            select c1 from #ta union select c2 from #ta union select c3 from #ta

            Comment

            Working...