SELECT COUNT(DISTINCT [name]) from several tables

前端 未结 4 1374
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-06 08:28

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 [MyT         


        
4条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-06 09:16

    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.

    SELECT [NAME], COUNT(*) as TheCount
    FROM
        (
         SELECT [Name] FROM [MyTable1]
         UNION ALL
         SELECT [Name] FROM [MyTable2]
         UNION ALL
         SELECT [Name] FROM [MyTable3]
         ) AS [TheNames]
    GROUP BY [NAME]
    

提交回复
热议问题