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 6133001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:10:17+00:00 2026-05-23T17:10:17+00:00

I can perform the following SQL Server selection of distinct (or non-repeating names) from

  • 0

I can perform the following SQL Server selection of distinct (or non-repeating names) from a column in one table like so:

SELECT COUNT(DISTINCT [Name]) FROM [MyTable]

But what if I have more than one table (all these tables contain the name field called [Name]) and I need to know the count of non-repeating names in two or more tables.

If I run something like this:

SELECT COUNT(DISTINCT [Name]) FROM [MyTable1], [MyTable2], [MyTable3]

I get an error, “Ambiguous column name ‘Name'”.

PS. All three tables [MyTable1], [MyTable2], [MyTable3] are a product of a previous selection.

  • 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-23T17:10:18+00:00Added an answer on May 23, 2026 at 5:10 pm

    After the clarification, use:

      SELECT x.name, COUNT(x.[name])
        FROM (SELECT [name]
                FROM [MyTable]
              UNION ALL
              SELECT [name]
                FROM [MyTable2]
              UNION ALL
              SELECT [name]
                FROM [MyTable3]) x
    GROUP BY x.name
    

    If I understand correctly, use:

      SELECT x.name, COUNT(DISTINCT x.[name])
        FROM (SELECT [name]
                FROM [MyTable]
              UNION ALL
              SELECT [name]
                FROM [MyTable2]
              UNION ALL
              SELECT [name]
                FROM [MyTable3]) x
    GROUP BY x.name
    

    UNION will remove duplicates; UNION ALL will not, and is faster for it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can COMMON Table expressions be used to avoid having SQL Server perform the following
Smarty Pants sounds like a cool idea to me: SmartyPants can perform the following
Using the JavaScript Native Interface of GWT I can perform the following: public native
In this live SQL Server 2008 (build 10.0.1600) database, there's an Events table, which
While the following work fine in SQL Server Management Studio, it just won't work
I'm pulling some internationalized text from a MS SQL Server 2005 database. As per
When I am performing deletes from an Sql table, how would I do the
I have an application that stores xml documents inside a column on SQL Server.
I would like to write the following SQl in NHibernate - Detached Criteria if
I'm going to perform a search in my SQL server DB (ASP.NET, VS2010,C#), user

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.