title: How to use group by with union in T-SQL
body: <p>How can I using group by with union in T-SQL? I want to group by the first column of a result of union, I wrote the following SQL but it doesn't work. I just don't know how to reference the specified column (in this case is 1) of the union result.</p>
  <pre><code>SELECT  *
  FROM    ( SELECT    a.id ,
                      a.time
            FROM      dbo.a
            UNION
            SELECT    b.id ,
                      b.time
            FROM      dbo.b
          )
  GROUP BY 1
  </code></pre>