在相同的ID SQL下查找多个(不同的)值

时间:2014-08-19 00:41:57

标签: sql sql-server

所以我有一张学生桌

FirstName,Lastname,MisID和NetworkName。 (MisID是用户名)

Theres是一个问题,学生可能有两台笔记本电脑(较旧的笔记本电脑)是不允许的。我怎样才能找到例如userID出现两次的时间(因此有两台笔记本电脑)

这是我的代码到目前为止,但它只是将整个MisID分组,因此计数无用。

Select DISTINCT MisID, networkname FROM dbo.CompiledStudentData

GROUP BY MisID, networkname
HAVING COUNT(MisID) >= 2;

干杯

4 个答案:

答案 0 :(得分:2)

您需要使用:

select distinct MisID, networkname
FROM dbo.CompiledStudentData as t1
inner join
(
    Select MisID FROM dbo.CompiledStudentData
    GROUP BY MisID
    HAVING COUNT(distinct networkname) >= 2
) as t2
on (t1.MisID = t2.MisID)
order by 1, 2

这里的主要观点是:

  1. 获取至少有2个不同网络名称(笔记本电脑)的学生列表(MisID),​​这是通过内部查询(创建临时"表")来完成的。
  2. 在原始表(别名:t1)和此临时表(别名:t2)之间加入,为我们提供了使用所有网络名称的学生列表

答案 1 :(得分:1)

Select count(1) as cnt , MisID , networkname FROM dbo.CompiledStudentData GROUP BY MisID, networkname

答案 2 :(得分:1)

我会使用窗口函数来完成此操作。这似乎是最简单的解决方案:

select sd.*
from (select sd.*, count(*) over (partition by msid) as cnt
      from dbo.CompiledStudentData
      ) sd
where cnt >= 2;

答案 3 :(得分:0)

USE adventureworksdw2008r2 

go 

SET nocount ON 

go 

/* 
Default 
*/ 
SET TRANSACTION isolation level READ committed 

go 

BEGIN TRAN 

IF Object_id('tempdb.dbo.#StudentData') IS NOT NULL 
  BEGIN 
      DROP TABLE #studentdata 
  END 

CREATE TABLE #studentdata 
  ( 
     firstname   VARCHAR(50), 
     lastname    VARCHAR(50), 
     misid       VARCHAR(10), 
     networkname VARCHAR(100) 
  ); 

INSERT INTO #studentdata 
VALUES     ('John', 
            'Doe', 
            'jdoe', 
            'NET1') 

INSERT INTO #studentdata 
VALUES     ('Jane', 
            'Doe', 
            'jndoe', 
            'NET1') 

INSERT INTO #studentdata 
VALUES     ('Jack', 
            'Doe', 
            'jkdoe', 
            'NET1') 

INSERT INTO #studentdata 
VALUES     ('Jeff', 
            'Doe', 
            'jfdoe', 
            'NET1') 

INSERT INTO #studentdata 
VALUES     ('Jericho', 
            'Doe', 
            'jdoe', 
            'NET2') 

INSERT INTO #studentdata 
VALUES     ('Jeremy', 
            'Doe', 
            'jdoe', 
            'NET2') 

INSERT INTO #studentdata 
VALUES     ('Jill', 
            'Doe', 
            'jdoe', 
            'NET3') 

INSERT INTO #studentdata 
VALUES     ('John', 
            'Schmoe', 
            'jschmoe', 
            'NET3') 

INSERT INTO #studentdata 
VALUES     ('Jane', 
            'Schmoe', 
            'jschmoe', 
            'NET3') 

INSERT INTO #studentdata 
VALUES     ('Jared', 
            'Schmoe', 
            'jschmoe', 
            'NET2') 

INSERT INTO #studentdata 
VALUES     ('Johnny', 
            'Schmoe', 
            'jschmoe', 
            'NET2') 

/* 
Find multiple occurance of MisID 
*/ 
SELECT Count(*), 
       misid 
FROM   #studentdata 
GROUP  BY misid 
HAVING Count(*) > 1 

/* 
Find user id exists more than once within a network 
*/ 
SELECT Count(*), 
       misid, 
       networkname 
FROM   #studentdata 
GROUP  BY misid, 
          networkname 
HAVING Count(*) > 1 

/* 
List user id that exit multiple times and all the networks to which it belongs 
*/ 
SELECT S1.misid, 
       networkname 
FROM   #studentdata S1 
       JOIN (SELECT DISTINCT misid 
             FROM   #studentdata 
             GROUP  BY misid 
             HAVING Count(misid) > 1) S2 
         ON S1.misid = S2.misid 
WHERE  S1.misid = S2.misid 
GROUP  BY S1.misid, 
          S1.networkname 

IF ( Xact_state() = 1 
     AND Error_state() = 0 ) 
  BEGIN 
      COMMIT TRAN 
  END 
ELSE IF ( @@TRANCOUNT > 0 ) 
  BEGIN 
      ROLLBACK TRAN 
  END