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

  • SEARCH
  • Home
  • 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 to select a bunch of data from a table using a GROUP
I've got a question about reusing table data but a view won't work in
i'm trying to get all the images from a facebook group using facebook API
I'm trying to sell our group on using Mercurial as a source repository rather
Before delving into the issue, first I will explain the situation. I have two
I'm doing some refactoring work on PHP/MySQL code and stumbled across an interesting problem,
General Case How do you perform a left join across a many-to-many relationship when
The Query: SELECT max(image_url), Model_Name FROM ( SELECT max(image_url), Model_Name FROM dbo.proinfo_vodafone WHERE Brand_name='Nokia'
I need to run a report grouped by week. This could be done by
I am trying to retrieve a list of properties and to do this I

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.