count() 用于不同的值
count() for distinct values
举个例子
CREATE TABLE #repeated ( iValue int NOT NULL)
INSERT INTO #repeated
VALUES(1),(1),(2),(3),(4),(5),(5),(5),(6),(7)
SELECT * FROM #repeated
SELECT
count(*) as countAsterisco
,count(iValue) as countValue
FROM #repeated
countAsterisco 和 countValue 的结果都是 10,因为这两个计数都考虑了重复值。我只需要计算不同的值,所以结果必须是 7。
有这个功能吗?我以为 count(iValue)
会做。
select count(distinct iValue) from #repeated
举个例子
CREATE TABLE #repeated ( iValue int NOT NULL)
INSERT INTO #repeated
VALUES(1),(1),(2),(3),(4),(5),(5),(5),(6),(7)
SELECT * FROM #repeated
SELECT
count(*) as countAsterisco
,count(iValue) as countValue
FROM #repeated
countAsterisco 和 countValue 的结果都是 10,因为这两个计数都考虑了重复值。我只需要计算不同的值,所以结果必须是 7。
有这个功能吗?我以为 count(iValue)
会做。
select count(distinct iValue) from #repeated