使用oracle开发人员,我运行了一个导致下表的查询。但我只想得到column1匹配所有值列2(3,4,8)的结果。所以输出将是2,3,但不是4.我确定有一种方法可以在没有硬编码的情况下得到这个结果吗?我在考虑某种自我加入?
select column1, column2
from table1
where column1 in (
select column1
from table2
where depth >= 100)
order by column2;
输出:
column1 column2
3 2
8 2
4 2
3 3
4 3
8 3
4 4
表2
Column1 Area_Name Depth
1 Lake 40
2 River 50
3 Ocean 150
4 Cliff 150
5 Mountain 90
6 Construction 60
7 Building 50
8 Random 100
9 Also Random 50
10 Another one 80
需要输出:
column2
2
3
好的,这就是我要找的东西:
SELECT table1.column1
FROM table1
INNER JOIN table2
ON table1.column2 = table2.column2
WHERE table2.depth >= 100
GROUP BY boat_id
HAVING COUNT(*) >= (
select count(*)
from table2
where depth >= 100);
答案 0 :(得分:0)
<强>已更新强>
WITH qry AS (
SELECT column1, column2
FROM table1
WHERE column1 IN (
SELECT column1
FROM table2
WHERE depth >= 100)
)
SELECT t1.column2
FROM qry t1 LEFT JOIN qry t2
ON t1.column1 = t2.column1 AND t1.column2 = t2.column2
GROUP BY t1.column2
HAVING COUNT(*) = (SELECT COUNT(DISTINCT column1) FROM qry)
ORDER BY t1.column2
输出:
| COLUMN2 |
-----------
| 2 |
| 3 |
<强> SQLFiddle 强>