获取 table 中多个分区上与另一条记录的特定条件匹配的所有记录
Get all records that matches a specific criteria of another records over multiple partitions in the table
我有以下 view/table T:
+--------+--------------+-------+----------+
| row_id | batch_status | class | batch_id |
+--------+--------------+-------+----------+
| 1 | new | K1 | B1 |
+--------+--------------+-------+----------+
| 2 | proc | K1 | B2 |
+--------+--------------+-------+----------+
| 3 | proc | K1 | B3 |
+--------+--------------+-------+----------+
| 4 | proc | K1 | B1 |
+--------+--------------+-------+----------+
| 5 | finish | K1 | B1 |
+--------+--------------+-------+----------+
| 6 | new | K2 | B5 |
+--------+--------------+-------+----------+
| 7 | proc | K2 | B2 |
+--------+--------------+-------+----------+
| 8 | new | K3 | B2 |
+--------+--------------+-------+----------+
| 9 | proc | K3 | B5 |
+--------+--------------+-------+----------+
| 10 | finish | K3 | B1 |
+--------+--------------+-------+----------+
| 11 | finish | K3 | B2 |
+--------+--------------+-------+----------+
| 12 | new | K4 | B7 |
+--------+--------------+-------+----------+
| 13 | new | K5 | B2 |
+--------+--------------+-------+----------+
其中记录分布在 5 个 class 中:k1 -> k5
。
现在 对于每个 class 组,我需要显示其 ID 与状态 'new'
的 batch_id
相匹配的批次].这意味着需要排除的是(红色交叉线)。
没有划线的是需要显示的记录:
关系数据库管理系统:Oracle 12c
是否:
Select这一行如果存在,在同一个table,同一个行batch_id和class和batch_status=new.
Select *
From T as T1
Where
Exists (select 1
From T as T2
Where T2.batch_status='new'
And T2.batch_id=T1.batch_id
And T2.class=T1.class)
您可以使用带条件的 COUNT() OVER ()
分析函数来检查每个 class
和 [=16] 是否存在等于值 new
的任何 batch_status
=] 比如
SELECT row_id, batch_status, class, batch_id
FROM (SELECT t.*,
COUNT(CASE
WHEN batch_status = 'new' THEN
1
END) OVER(PARTITION BY class, batch_id) AS cnt
FROM t)
WHERE cnt > 0
ORDER BY class
或者不使用子查询(因为你的数据库版本是 12c)
SELECT t.*,
COUNT(CASE
WHEN batch_status = 'new' THEN
1
END) OVER (PARTITION BY class, batch_id) AS cnt
FROM t
ORDER BY SIGN(cnt) DESC
FETCH FIRST 1 ROW WITH TIES
我有以下 view/table T:
+--------+--------------+-------+----------+
| row_id | batch_status | class | batch_id |
+--------+--------------+-------+----------+
| 1 | new | K1 | B1 |
+--------+--------------+-------+----------+
| 2 | proc | K1 | B2 |
+--------+--------------+-------+----------+
| 3 | proc | K1 | B3 |
+--------+--------------+-------+----------+
| 4 | proc | K1 | B1 |
+--------+--------------+-------+----------+
| 5 | finish | K1 | B1 |
+--------+--------------+-------+----------+
| 6 | new | K2 | B5 |
+--------+--------------+-------+----------+
| 7 | proc | K2 | B2 |
+--------+--------------+-------+----------+
| 8 | new | K3 | B2 |
+--------+--------------+-------+----------+
| 9 | proc | K3 | B5 |
+--------+--------------+-------+----------+
| 10 | finish | K3 | B1 |
+--------+--------------+-------+----------+
| 11 | finish | K3 | B2 |
+--------+--------------+-------+----------+
| 12 | new | K4 | B7 |
+--------+--------------+-------+----------+
| 13 | new | K5 | B2 |
+--------+--------------+-------+----------+
其中记录分布在 5 个 class 中:k1 -> k5
。
现在 对于每个 class 组,我需要显示其 ID 与状态 'new'
的 batch_id
相匹配的批次].这意味着需要排除的是(红色交叉线)。
没有划线的是需要显示的记录:
关系数据库管理系统:Oracle 12c
是否: Select这一行如果存在,在同一个table,同一个行batch_id和class和batch_status=new.
Select *
From T as T1
Where
Exists (select 1
From T as T2
Where T2.batch_status='new'
And T2.batch_id=T1.batch_id
And T2.class=T1.class)
您可以使用带条件的 COUNT() OVER ()
分析函数来检查每个 class
和 [=16] 是否存在等于值 new
的任何 batch_status
=] 比如
SELECT row_id, batch_status, class, batch_id
FROM (SELECT t.*,
COUNT(CASE
WHEN batch_status = 'new' THEN
1
END) OVER(PARTITION BY class, batch_id) AS cnt
FROM t)
WHERE cnt > 0
ORDER BY class
或者不使用子查询(因为你的数据库版本是 12c)
SELECT t.*,
COUNT(CASE
WHEN batch_status = 'new' THEN
1
END) OVER (PARTITION BY class, batch_id) AS cnt
FROM t
ORDER BY SIGN(cnt) DESC
FETCH FIRST 1 ROW WITH TIES