SQL比较多个行或分区以查找匹配项

时间:2015-07-07 01:43:07

标签: sql db2

我正在处理的数据库是DB2,我遇到类似以下情况的问题:

Table Structure
-------------------------------
| Teacher Seating Arrangement |
-------------------------------
| PK | seat_argmt_id          |
|    | teacher_id             |
-------------------------------

-----------------------------
| Seating Arrangement       |
-----------------------------
|PK FK | seat_argmt_id      |
|PK    | Row_num            |
|PK    | seat_num           |
|PK    | child_name         |
-----------------------------

Table Data
------------------------------
| Teacher Seating Arrangement|
------------------------------
| seat_argmt_id | teacher_id |
|         1     |     1      |
|         2     |     1      |
|         3     |     1      |
|         4     |     1      |
|         5     |     2      |
------------------------------

---------------------------------------------------
| Seating Arrangement                             |
---------------------------------------------------
| seat_argmt_id | row_num | seat_num | child_name |
|        1      |    1    |    1     |     Abe    |
|        1      |    1    |    2     |     Bob    |
|        1      |    1    |    3     |     Cat    |
|               |         |          |            |
|        2      |    1    |    1     |     Abe    |
|        2      |    1    |    2     |     Bob    |
|        2      |    1    |    3     |     Cat    |
|               |         |          |            |
|        3      |    1    |    1     |     Abe    |
|        3      |    1    |    2     |     Cat    |
|        3      |    1    |    3     |     Bob    |
|               |         |          |            |
|        4      |    1    |    1     |     Abe    |
|        4      |    1    |    2     |     Bob    |
|        4      |    1    |    3     |     Cat    |
|        4      |    2    |    2     |     Dan    |
---------------------------------------------------

我想看看老师在哪里有重复的座位安排。重复一词,我的意思是row_numseat_numchild_nameseat_argmt_id的{​​{1}}之间的相同位置。因此,使用上面提供的数据,只有座位ID 1和2是我想要撤回的,因为除teacher_id之外的所有内容都是重复的。如果第二个表上的所有孩子都是准确的(没有主键和外键,在这种情况下是seat id),我想看到。

我最初的想法是做seat_argmt_id。计数为>的所有内容1意味着它是一个骗局,= 1意味着它是独一无二的。该逻辑仅在您比较单行时才有效。我需要比较多行。我无法通过SQL找到一种方法。我所涉及的解决方案涉及到SQL之外的工作(可能)。我只是想知道在DB2中是否有办法实现它。

1 个答案:

答案 0 :(得分:1)

这样做你想要的吗?

select d.teacher_id, sa.row_num, sa.seat_num, sa.child_name
from seatingarrangement sa join
     data d
     on sa.seat_argmt_id = d.seat_argmt_id
group by d.teacher_id, sa.row_num, sa.seat_num, sa.child_name
having count(*) > 1;

编辑:

如果你想找到两个相同的安排:

select sa1.seat_argmt_id, sa2.seat_argmt_id
from seatingarrangement sa1 join
     seatingarrangement sa2
     on sa1.seat_argmt_id < sa2.seat_argmt_id and
        sa1.row_num = sa2.row_num and
        sa1.seat_num = sa2.seat_num and
        sa1.child_name = sa2.child_name
group by sa1.seat_argmt_id, sa2.seat_argmt_id
having count(*) = (select count(*) from seatingarrangement sa where sa.seat_argmt_id = sa1.seat_argmt_id) and
       count(*) = (select count(*) from seatingarrangement sa where sa.seat_argmt_id = sa2.seat_argmt_id);

找到两个排列之间的匹配,然后验证计数是否正确。