SQL-获取与同一表中其他记录匹配的记录
我正在使用亚马逊红移PostgreSQL数据库 我的数据库中有一个表,其中存储了如下约会记录:SQL-获取与同一表中其他记录匹配的记录,sql,postgresql,amazon-redshift,Sql,Postgresql,Amazon Redshift,我正在使用亚马逊红移PostgreSQL数据库 我的数据库中有一个表,其中存储了如下约会记录: | id | patientname | providername | eventid | eventstart | eventend | isactive | segmentcode | Description | |----|---------------|--------------|---------|-------------
| id | patientname | providername | eventid | eventstart | eventend | isactive | segmentcode | Description |
|----|---------------|--------------|---------|---------------------|---------------------|----------|-------------|------------------------|
| 1 | Susie Jones | John Melton | 340000 | 2017-08-08 10:00:00 | 2017-08-08 10:00:00 | true | 845685 | Reminder Call |
| 2 | Susie Jones | John Melton | 340000 | 2017-08-08 10:00:00 | 2017-08-08 10:30:00 | true | 365478 | Steam Therapy Session |
| 3 | Roschel Ross | Kate Winny | 350000 | 2017-08-09 11:00:00 | 2017-08-09 11:00:00 | true | 845685 | Reminder Call |
| 4 | Roschel Ross | Kate Winny | 350000 | 2017-08-09 11:00:00 | 2017-08-09 13:30:00 | true | 367545 | Physio Therapy Session |
| 5 | Lilly Hodge | Jessica | 360000 | 2017-08-09 11:00:00 | 2017-08-09 11:00:00 | true | 754544 | Doctor appointment |
| 6 | Jack Richards | Mike Chong | 37000 | 2017-08-10 17:00:00 | 2017-08-10 17:30:00 | true | 889754 | Sample Appointment |
| 7 | Sammy Jones | Winsten | 38000 | 2017-08-10 17:00:00 | 2017-08-10 18:30:00 | true | 845685 | Physio Therapy Session |
| 8 | Sammy Jones | Winsten | 38000 | 2017-08-10 17:00:00 | 2017-08-10 17:00:00 | true | 454542 | Reminder Call |
在这里,您可以看到一些段代码为845685的记录,所有这些记录都有一个重复的记录,段代码不同,但事件ID相同
我想要的是,使用SQL查询获取段代码为845685的记录及其重复记录。因此,结果表将为:
| id | patientname | providername | eventid | eventstart | eventend | isactive | segmentcode | Description |
|----|---------------|--------------|---------|---------------------|---------------------|----------|-------------|------------------------|
| 1 | Susie Jones | John Melton | 340000 | 2017-08-08 10:00:00 | 2017-08-08 10:00:00 | true | 845685 | Reminder Call |
| 2 | Susie Jones | John Melton | 340000 | 2017-08-08 10:00:00 | 2017-08-08 10:30:00 | true | 365478 | Steam Therapy Session |
| 3 | Roschel Ross | Kate Winny | 350000 | 2017-08-09 11:00:00 | 2017-08-09 11:00:00 | true | 845685 | Reminder Call |
| 4 | Roschel Ross | Kate Winny | 350000 | 2017-08-09 11:00:00 | 2017-08-09 13:30:00 | true | 367545 | Physio Therapy Session |
| 7 | Sammy Jones | Winsten | 38000 | 2017-08-10 17:00:00 | 2017-08-10 18:30:00 | true | 845685 | Physio Therapy Session |
| 8 | Sammy Jones | Winsten | 38000 | 2017-08-10 17:00:00 | 2017-08-10 17:00:00 | true | 454542 | Reminder Call |
我怎么做 看来简单的子查询(作为一种方式)就足够了:
select * from t
where eventid in (select eventid from t where segmentcode = 845685)
-- order by eventid, ...
另一种方法是使用exists
:
select * from t t1
where
exists (select 1 from t t2 where eventid = t1.eventid and segmentcode = 845685 )
对于现代数据库来说,数千条记录的数量并不是很大,无论如何,如果性能不理想,那么在
eventid
和segmentcode
上添加索引(如果还没有)应该会加快选择查询的速度 具有IN的子查询是否会执行得更好?那里有成千上万的记录。