【问题标题】:SQL Query that joins one table where the second table has duplicates连接一个表的 SQL 查询,其中第二个表有重复项
【发布时间】:2014-03-23 13:27:26
【问题描述】:

我有 2 个表加入了 StudentID 和 ParkingID。我的表 B 有重复的停车信息。我想获取 StudentID、StudentName、ParkingSpace 号码和重复次数。这是我的第一篇文章,如果我不遵循这里所有正确的协议,请原谅我。我很感激帮助。

例子:

Table A:

StudentID     StudentName
----          ------
001           Mary
002           Jane
003           Peter
004           Smith
005           Kathy

Table B:

ParkingID     ParkingSpace
-----         -----
001           25
001           25
002           18
003           74
004           22
005           31
005           31
005           31
005           31
005           31

这是我的目标。

StudentID     StudentName  ParkingSpace  dupCount
----          ------       ------        ------
001           Mary         25            2
005           Kathy        31            5

【问题讨论】:

  • 这两个表是如何链接的?
  • 学生证和停车证
  • 那么 StudentID 和 ParkingID 代表同一个东西?如果是这样,您可以考虑将它们命名为相同的..
  • 这里只是一个旁注,但您的 DupCount 列实际上并不是重复的数量。它是空格数。 Mary 有 2 个空格,其中只有 1 个是重复的。 Kathy 有 5 个空格,其中 4 个是重复的。

标签: sql join duplicates


【解决方案1】:

测试数据

DECLARE @Table_1 TABLE (StudentID VARCHAR(100),StudentName VARCHAR(100))
INSERT INTO @Table_1 VALUES 
('001','Mary'),('002','Jane'),('003','Peter'),
('004','Smith'),('005','Kathy')

DECLARE @Table_2 TABLE 
(ParkingID VARCHAR(100),ParkingSpace INT)
INSERT INTO @Table_2 VALUES
('001',25),('001',25),('002',18),('003',74),('004',22),('005',31),
('005',31),('005',31),('005',31),('005',31)

查询

SELECT T1.StudentID
      ,T1.StudentName
      ,T2.ParkingSpace
      ,COUNT(T2.ParkingSpace) AS Duplicates

FROM @Table_1 T1 INNER JOIN @Table_2 T2
ON T1.StudentID = T2.ParkingID
GROUP BY  T1.StudentID
         ,T1.StudentName
         ,T2.ParkingSpace
HAVING COUNT(T2.ParkingSpace) > 1

结果集

╔═══════════╦═════════════╦══════════════╦════════════╗
║ StudentID ║ StudentName ║ ParkingSpace ║ Duplicates ║
╠═══════════╬═════════════╬══════════════╬════════════╣
║       001 ║ Mary        ║           25 ║          2 ║
║       005 ║ Kathy       ║           31 ║          5 ║
╚═══════════╩═════════════╩══════════════╩════════════╝

【讨论】:

  • 这不太正确。 Jane、Peter 和 Smith 没有 1 个重复项。它们有 1 个空格和 0 个重复项。
【解决方案2】:

这是您的问题的解决方案。

select studentid, studentname, parkingspace , count(*) dupcount
from tablea inner join tableb on tablea.studentid=tableb.parkingid
group by studentid, studentname, parkingspace having count(*)>1

我们计算重复次数,having count(*)>1 只显示真正的重复次数。

http://sqlfiddle.com/#!2/29c2d/2

【讨论】:

    【解决方案3】:
    SELECT
      A.studentID,
      A.studentName,
      B.parkingSpace,
      COUNT(B.parkingSpace)
    FROM A
    
    JOIN B ON A.StudentID = B.ParkingID
    
    GROUP BY
    A.studentID,
      A.studentName,
      B.parkingSpace 
    

    【讨论】:

      【解决方案4】:
      select a.studentId, a.studentName, 
      b.parkingspace, count(1) as dupcount
      from a
      join b on a.studentId = b.parkingid
      group by a.studentId, a.studentName, b.parkingspace
      having dupcount > 1
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-27
        • 2011-09-12
        • 2015-10-24
        • 1970-01-01
        • 2019-01-18
        相关资源
        最近更新 更多