Home Forums SQL Server 2008 SQL Server Newbies How to merge two sql select statement results I tried it but it not get please see it in detailed explination with my query RE: How to merge two sql select statement results I tried it but it not get please see it in detailed explination with my query

  • This is because the UNION keyword combines the results of two or more queries into a single result set that includes all the rows that belong to all queries in the union.

    Due to the brevity of the sample data and results, I assume that you are wanting the total hours across both queries for each ID. If that is the case you will need to write an aggregate with GROUP BY, something like this:

    select t.ID, t.NAME, sum(t.HOURS)

    from (

    select ID, NAME, HOURS

    from first_query_results

    union all

    select ID, NAME, HOURS

    from second_query_results

    ) t

    group by t.ID, t.NAME