我是oracle 11g的新手,不知道这样做的最好方法。感谢您的任何提示。
T1
id_set id_
1 100
1 101
1 102
1 103
2 100
2 101
2 102
2 108
T2
id_ tag name
100 A a
101 B1 b
102 B2 c
103 B3 d
108 B8 e
这里的规则是检查id_set,检查它是否包含tag ='A'和tag = B *,选择B之后具有最高整数的那个。 所以样本输出将是:
id_set_ name
1 d
2 e
我有B15,达到这个目标的最佳方法是什么?谢谢! PS。 id增加时标签号不会增加
答案 0 :(得分:0)
Oracle 11g R2架构设置:
CREATE TABLE T1 (id_set, id_) AS
SELECT 1, 100 FROM DUAL
UNION ALL SELECT 1, 101 FROM DUAL
UNION ALL SELECT 1, 102 FROM DUAL
UNION ALL SELECT 1, 103 FROM DUAL
UNION ALL SELECT 2, 100 FROM DUAL
UNION ALL SELECT 2, 101 FROM DUAL
UNION ALL SELECT 2, 102 FROM DUAL
UNION ALL SELECT 2, 108 FROM DUAL;
CREATE TABLE T2 (id_, tag, name) AS
SELECT 100, 'A', 'a' FROM DUAL
UNION ALL SELECT 101, 'B1', 'b' FROM DUAL
UNION ALL SELECT 102, 'B2', 'c' FROM DUAL
UNION ALL SELECT 103, 'B3', 'd' FROM DUAL
UNION ALL SELECT 108, 'B8', 'e' FROM DUAL;
查询1 :
WITH max_tags AS (
SELECT id_set, MAX( tag ) AS max_tag
FROM T1
INNER JOIN
T2
ON (T1.id_ = T2.id_)
GROUP BY id_set
HAVING MIN( tag ) = 'A'
AND MAX( tag ) LIKE 'B%'
)
SELECT id_set, name
FROM max_tags m
INNER JOIN
T2 t
ON (m.max_tag = t.tag)
<强> Results 强>:
| ID_SET | NAME |
|--------|------|
| 1 | d |
| 2 | e |