Please Help me for combining 2 select queries

  • i have 2 select queries i need to build

    my first query is

    select name from emp

    Kings

    wings

    sings

    things

    ...etc

    my second query is

    select age from emp

    16

    18

    20

    22

    my need is i have to combine both

    my result should be like this

    Kings

    16

    wings

    18

    sings

    20

    things

    22

    could you pls help me

  • Try this

    select name, age from emp

  • i have to get in seperate row one by one

  • try the following way:

    selecttop 10

    name + char (13) + char (10) + convert (varchar (10), age)

    fromemp with (nolock)

  • Kingsleen Solomon Doss (6/2/2009)


    i have to get in seperate row one by one

    Hi,

    Also try this to store the record one by one

    declare @RESULT varchar(1000)

    select @RESULT = ''

    select @RESULT = @RESULT +name+','+age+',' from emp

    select @RESULT = 'select ''' + replace (@RESULT,',',''' union all select ''')+''''

    -- create table #ABC

    -- (

    -- RESULT varchar(20)

    -- )

    insert into #ABC

    exec (@RESULT)

    select * from #ABC

    ARUN SAS

  • try this:

    --YOUR TABLE

    DECLARE @emp TABLE

    (ID INT IDENTITY(1,1),

    Name VARCHAR(10),

    AGE INT)

    --TEST DATA

    INSERT INTO @emp

    SELECT 'Kings',16 UNION ALL

    SELECT 'Wings',18 UNION ALL

    SELECT 'Sings',20 UNION ALL

    SELECT 'Things',22

    --SOLUTION

    SELECT id,Name as Field FROM @Emp UNION

    SELECT id,CAST(age as VARCHAR) as Field FROM @Emp

    ORDER BY id,Field DESC

    ----------------------------------------------
    Try to learn something about everything and everything about something. - Thomas Henry Huxley

    :w00t:
    Posting Best Practices[/url]
    Numbers / Tally Tables[/url]

    SQL-4-Life
  • SELECT E.EmpRow FROM (

    SELECT Name, Name AS EmpRow FROM EMP

    UNION ALL

    SELECT Name, CONVERT(varchar(10), AGE) AS EmpRow FROM EMP ) AS E

    ORDER BY E.NAME, E.EmpRow DESC

  • Using SQL 2005's UNPIVOT method

    SELECT*

    FROM(

    SELECT[Name], CONVERT( VARCHAR(100), AGE ) AS Age

    FROMEmp

    ) E

    UNPIVOT

    (

    RowValue FOR RowType IN( [Name], [Age] )

    ) UP

    --Ramesh


Viewing 8 posts - 1 through 7 (of 7 total)

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