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.
After the clarification, use:
If I understand correctly, use:
UNION
will remove duplicates;UNION ALL
will not, and is faster for it.EDIT: Had to change after seeing recent comment.
Does this give you what you want? This gives a count for each person after combining the rows from all tables.
Here's another way:
In case you have different amounts of columns per table, like:
And you want to count the amount of distinct values of different column names, what it was useful to me in AthenaSQL was to use
CROSS JOIN
since your output would be only one row, it would be just 1 combination:Would return a table with one row and their counts: