我有一个查询,我打算消除两个学期的重复。不幸的是,我必须结合使用chpid,secno,lname,fname和email来使记录唯一以便比较。
查询为我要完成的工作提供了一个起点。
SELECT DISTINCT
'1' as Counter
chpid,
SECNO,
LNAME,
FNAME,
EmailAddress
FROM
Eligmember
WHERE
((FORMYEAR = 2018 AND FORMSEMESTER = 4) OR
(FORMYEAR = 2019 AND FORMSEMESTER = 2))
AND presentmem <> 'Y'
返回58000条记录
Counter chpid SECNO LNAME FNAME EmailAddress
---------------------------------------------------------------
1 1 2 Banes Moore MMB@test.test
1 1 2 Baron Lana LRB@test.org
这是很好的开始,但是我想做的是计算每个chpid中有多少个。我只需要每个chpid中每个人的计数,但是我必须选择chpid,secno,lname,fname和email以使其唯一。
有什么建议吗?
答案 0 :(得分:0)
不幸的是,SQL Server不支持带有多个表达式的count(distinct)
。一种方法是子查询:
SELECT chpid, COUNT(*)
FROM (SELECT DISTINCT chpid, SECNO, LNAME, FNAME, EmailAddress
FROM Eligmember
WHERE ((FORMYEAR = 2018 and FORMSEMESTER = 4) OR
(FORMYEAR = 2019 and FORMSEMESTER = 2)
) and
presentmem <> 'Y'
) c
GROUP BY chpid;
答案 1 :(得分:0)
这是一个简单的GROUP BY方案:
SELECT
Count(*) as Counter
,chpid
,max(SECNO) as SECNO
,LNAME
,FNAME
,max(EmailAddress) as EmailAddress
FROM
Eligmember
WHERE
((FORMYEAR = 2018 AND FORMSEMESTER = 4) OR
(FORMYEAR = 2019 AND FORMSEMESTER = 2))
AND presentmem <> 'Y'
GROUP BY
chpid
,LNAME -- for each person within the chpid
,FNAME -- ^
;
此“组合chpid,secno,lname,fname和email以使记录唯一以进行比较”。它还会生成所需的结果集。假设fname,lname和电子邮件地址之间存在1:1的关系;如果不是,则需要删除电子邮件地址的最大值和分组。