SQL - 获取与同一表中其他记录匹配的记录

时间:2017-08-08 06:19:55

标签: sql postgresql amazon-redshift

我正在使用Amazon RedShift PostgreSQL数据库。

我的数据库中有一个表存储如下的约会记录:

| 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 的记录,并且所有这些记录都包含具有不同段代码但具有相同eventid 的重复记录。

我想要的是,使用 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          |

我该怎么办?

1 个答案:

答案 0 :(得分:1)

似乎简单的子查询(作为一种方式)就足够了:

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 )

数千条记录对于现代数据库而言并不是那么大,无论如何,如果性能不合适,在eventidsegmentcode上添加索引(如果还没有)应该加快{{1查询。