我需要根据特定列获取重复记录数。如果字段具有 NULL 值,则它不被视为重复。
SELECT Leadid,COUNT(Lead)
FROM #CheckNULLCOUNT
GROUP BY LeadId,Lead
但对于以下查询,它认为是重复的
SELECT Leadid,COUNT(ISNULL(Lead,'-'))
FROM #CheckNULLCOUNT
GROUP BY LeadId,Lead
我的问题是有没有更好的方法来获取 NULL 列的重复计数。
/* Create Temporary Table*/
CREATE table #CheckNULLCOUNT(
Id Int PRIMARY key
,name varchar(100)
,LeadId Int
,Lead varchar(100))
/* Insert data into temporary Table*/
INSERT INTO #CheckNULLCOUNT VALUES(1,'Skip',101,'lead1')
INSERT INTO #CheckNULLCOUNT VALUES(2,'Turner',102,'lead2')
INSERT INTO #CheckNULLCOUNT VALUES(3,'Molisa',103,'lead3')
INSERT INTO #CheckNULLCOUNT VALUES(4,'Mario',104,'lead4')
INSERT INTO #CheckNULLCOUNT VALUES(5,'Jack',105,'lead5')
INSERT INTO #CheckNULLCOUNT VALUES(6,'Kris',106,'lead6')
INSERT INTO #CheckNULLCOUNT VALUES(7,'John',107,'lead7')
INSERT INTO #CheckNULLCOUNT VALUES(8,'Owen',109,NULL)
INSERT INTO #CheckNULLCOUNT VALUES(9,'Bill',109,NULL)
INSERT INTO #CheckNULLCOUNT VALUES(10,'Martin',109,NULL)
INSERT INTO #CheckNULLCOUNT VALUES(11,NULL,111,'lead9')
INSERT INTO #CheckNULLCOUNT VALUES(12,NULL,111,'lead9')
/* **NOT** treating as duplicate */
SELECT Leadid,COUNT(ISNULL(Lead,'-'))
FROM #CheckNULLCOUNT
GROUP BY LeadId,Lead
/* treating as duplicate */
SELECT Leadid,COUNT(ISNULL(Lead,'-'))
FROM #CheckNULLCOUNT
GROUP BY LeadId,Lead
DROP TABLE #CheckNULLCOUNT