StudentID StudentID NO
1 111
1 211
1 111
2 444
2 444
2 444
5 555
5 555
5 NULL
6 66
6 66
6 66
6 66
输出 1 5
输出为1和5,因为它们具有不同的studentIDnos wheareas 2和6具有相同的studentIDnos。我们也应该照顾无效。考虑5
我需要一个SQL server Query来获取此输出
答案 0 :(得分:0)
你可以通过这样的group by和having子句实现这个目的:
SELECT studentID
FROM YourTable
GROUP BY studentID
HAVING count(distinct isnull(studentID_NO,1)) > 1
它将返回每个学生ID超过1的学生
答案 1 :(得分:0)
INSERT INTO @Table1
(StudentID, StudentIDNO)
VALUES
(1, '111'),
(1, '211'),
(1, '111'),
(2, '444'),
(2, '444'),
(2, '444'),
(5, '555'),
(5, '555'),
(5, NULL),
(6, '66'),
(6, '66'),
(6, '66'),
(6, '66')
;
Select T.StudentID from (
select StudentID, StudentIDNO from @Table1
GRoup by StudentID, StudentIDNO)T
GROUP BY T.StudentID
HAVING COUNT(T.StudentID) > 1
答案 2 :(得分:0)
一种方式:
select StudentID from T
group by StudentID
having count(distinct isnull([StudentID NO], -1)) > 1
假设-1不会出现