Results 1 to 3 of 3

Thread: 2 columns as 1

  1. #1
    Join Date
    Jan 2013
    Posts
    2

    Unanswered: 2 columns as 1

    hey everyone!
    how is it possible to print 2 columns as 1?
    eg.
    name & lastname
    ali wurst
    peter lol
    horst schwengel

    select name as onlycolumn, lastname as onlycolumn from db
    didnt work.
    the output should look like this:

    onlycolumn:
    ali
    peter
    horst
    wurst
    lol
    schwengel

    thank you guys

  2. #2
    Join Date
    Feb 2008
    Location
    Japan
    Posts
    3,483
    See UNPIVOT on SQL on Fire! Part 1
    Or, seach UNPIVOT on this forum.

    An example:

    Code:
    ------------------------------ Commands Entered ------------------------------
    WITH
     db( seq_id , name , lastname ) AS (
    VALUES
      ( 1 , 'ali'   , 'wurst'     )
    , ( 2 , 'peter' , 'lol'       )
    , ( 3 , 'horst' , 'schwengel' )
    )
    SELECT onlycolumn
     FROM  db
     CROSS JOIN
           LATERAL
           (VALUES ( 1 , name     )
                 , ( 2 , lastname )
           ) AS f( k , onlycolumn )
     ORDER BY
           k
         , seq_id
    ;
    ------------------------------------------------------------------------------
    
    ONLYCOLUMN
    ----------
    ali       
    peter     
    horst     
    wurst     
    lol       
    schwengel 
    
      6 record(s) selected.
    Last edited by tonkuma; 01-09-13 at 14:27. Reason: Adjust blanks in a sample code.

  3. #3
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    Quote Originally Posted by randomtask View Post
    select name as onlycolumn, lastname as onlycolumn from db
    didnt work.
    Code:
    SELECT name AS onlycolumn
      FROM daTable
    UNION ALL
    SELECT lastname 
      FROM daTable
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

Posting Permissions

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