我需要确定没有唯一person_id的学生(student_id),并在不唯一时返回另一列0,在唯一时返回1。
以下是我所拥有的:
Select
student_id, 0
FROM
PERSON
WHERE
PERSON.person_id in (
SELECT PERSON.person_id
FROM PERSON
WHERE PERSON.person_id<>''
GROUP BY PERSON.person_id
HAVING COUNT(*)>1
)
UNION
Select
student_id, 1
FROM
PERSON
WHERE
PERSON.person_id not in (
SELECT PERSON.person_id
FROM PERSON
WHERE PERSON.person_id<>''
GROUP BY PERSON.person_id
HAVING COUNT(*)>1
)
示例数据:http://pastebin.com/EcqQU88J MS SQL Server
有没有更有效的方法来完成这项工作?
答案 0 :(得分:3)
这样的事情可能更有效率。我认为这更简单。
select student_id
, case when personIds = 1 then 0 else 1 end notUnique
from
(select student_id
, count(distinct person_id) personIDs
from person
where person_id <> ''
group by student_id
) temp
答案 1 :(得分:2)