Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 431301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T19:57:40+00:00 2026-05-12T19:57:40+00:00

How can I using group by with union in T-SQL? I want to group

  • 0

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.

SELECT  *
FROM    ( SELECT    a.id ,
                    a.time
          FROM      dbo.a
          UNION
          SELECT    b.id ,
                    b.time
          FROM      dbo.b
        )
GROUP BY 1
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T19:57:41+00:00Added an answer on May 12, 2026 at 7:57 pm

    GROUP BY 1

    I’ve never known GROUP BY to support using ordinals, only ORDER BY. Either way, only MySQL supports GROUP BY’s not including all columns without aggregate functions performed on them. Ordinals aren’t recommended practice either because if they’re based on the order of the SELECT – if that changes, so does your ORDER BY (or GROUP BY if supported).

    There’s no need to run GROUP BY on the contents when you’re using UNION – UNION ensures that duplicates are removed; UNION ALL is faster because it doesn’t – and in that case you would need the GROUP BY…

    Your query only needs to be:

    SELECT a.id,
           a.time
      FROM dbo.TABLE_A a
    UNION
    SELECT b.id,
           b.time
      FROM dbo.TABLE_B b
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want DBSession.query(Article).group_by(Article.created.month).all() But this query can't using How do I do this using
Using Cloudformation can you set the Authenticated Users group to have put/delete Access Control
Using my c++ program how can I find out what group the current user
I have a problem - i can't compile SqlCipher. I'm using this http://groups.google.com/group/sqlcipher/browse_thread/thread/55c6296b56bf4533/c792bbec6df7d4f4?tvc=2#c792bbec6df7d4f4 instructions
I'm using MS SQL Server but welcome comparitive solutions from other databases. This is
I'm using group_by in a DBIx::Class resultset search. The result returned for each group
how can i do if my select dont come with result using SYS_REFCURSOR ?
How can I limit the number of results when using group() in MongoDB? Basically
I can submit using Jquery like this: $('.SomeLink').bind('click', function() { $(form).submit(); }); The actual
I can browse using the desktop browsers via ..localhost:54647/... and from windows mobile emulators

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.