我有一个使用 count 的 select 语句,因为我正在计算行而不是返回它们,我如何确保我没有在列上获得重复值?
例如
_table_
fName someField
Eric data
Kyle mdata
Eric emdata
Andrew todata
我希望计数为 3,因为 Eric 是重复的,有没有办法做到这一点?我的选择是:
Select Count(*) From _table_ INTO :var
谢谢,
我有一个使用 count 的 select 语句,因为我正在计算行而不是返回它们,我如何确保我没有在列上获得重复值?
例如
_table_
fName someField
Eric data
Kyle mdata
Eric emdata
Andrew todata
我希望计数为 3,因为 Eric 是重复的,有没有办法做到这一点?我的选择是:
Select Count(*) From _table_ INTO :var
谢谢,
SELECT Count(DISTINCT fName) From _table_ INTO :var
它将计算fName列中不同元素的数量。
这将完成工作Select Count(distinct fnmae) From _table_ INTO :var
尝试
SELECT Count(DISTINCT fName) From _table_ INTO :var
您可以选择 DISTINCT 名字的计数:
declare @table table (fname varchar(20), someField varchar(20))
insert into @table (fname, someField)
select 'Eric', 'data'
union select 'Kyle', 'mdata'
union select 'Eric', 'emdata'
union select 'Andrew', 'todata'
-- returns 4, because there are 4 rows in the table
select count(*) from @table
-- returns 3, because there are 3 rows with distinct first names
select count(*) from (select distinct fname from @table) firstNames