如何根据SQL中满足两个条件得到整个分区
How to get the entire partition based on two conditions are met in SQL
TelNO
Type
rank
date
76567
a
1
20210915
76567
b
2
20210611
76567
a
3
20210810
56597
b
1
20210818
56597
a
2
20210916
97658
b
1
20210610
97658
a
2
20210811
97658
b
3
20210915
76567
a
1
20210210
76567
a
2
20210619
我想 return 整个块(按 TelNO 分组)如果 Type= a 时
等级=1。预期输出如下。
TelNO
Type
rank
date
76567
a
1
20210915
76567
b
2
20210611
76567
a
3
20210810
76567
a
1
20210210
76567
a
2
20210619
我正在尝试以下代码。但它只给出满足条件的记录。我需要整个分区出现。由于没有可执行的聚合函数,我正在努力如何使用分区函数来获取相关输出
select *
from table
where Type=a and rank=1
group by TelNo
我是这样理解问题的:
SQL> with test (telno, type, rank) as
2 (select 76567, 'a', 1 from dual union all
3 select 76567, 'b', 2 from dual union all
4 select 76567, 'c', 3 from dual union all
5 --
6 select 56597, 'b', 1 from dual union all
7 select 56597, 'a', 2 from dual union all
8 --
9 select 97658, 'b', 1 from dual union all
10 select 97658, 'a', 2 from dual union all
11 select 97658, 'b', 3 from dual union all
12 --
13 select 76567, 'a', 1 from dual union all
14 select 76567, 'a', 2 from dual
15 )
16 select *
17 from test
18 where telno in (select telno from test
19 where type = 'a'
20 and rank = 1
21 );
TELNO T RANK
---------- - ----------
76567 a 2
76567 a 1
76567 c 3
76567 b 2
76567 a 1
SQL>
TelNO | Type | rank | date |
---|---|---|---|
76567 | a | 1 | 20210915 |
76567 | b | 2 | 20210611 |
76567 | a | 3 | 20210810 |
56597 | b | 1 | 20210818 |
56597 | a | 2 | 20210916 |
97658 | b | 1 | 20210610 |
97658 | a | 2 | 20210811 |
97658 | b | 3 | 20210915 |
76567 | a | 1 | 20210210 |
76567 | a | 2 | 20210619 |
我想 return 整个块(按 TelNO 分组)如果 Type= a 时 等级=1。预期输出如下。
TelNO | Type | rank | date |
---|---|---|---|
76567 | a | 1 | 20210915 |
76567 | b | 2 | 20210611 |
76567 | a | 3 | 20210810 |
76567 | a | 1 | 20210210 |
76567 | a | 2 | 20210619 |
我正在尝试以下代码。但它只给出满足条件的记录。我需要整个分区出现。由于没有可执行的聚合函数,我正在努力如何使用分区函数来获取相关输出
select *
from table
where Type=a and rank=1
group by TelNo
我是这样理解问题的:
SQL> with test (telno, type, rank) as
2 (select 76567, 'a', 1 from dual union all
3 select 76567, 'b', 2 from dual union all
4 select 76567, 'c', 3 from dual union all
5 --
6 select 56597, 'b', 1 from dual union all
7 select 56597, 'a', 2 from dual union all
8 --
9 select 97658, 'b', 1 from dual union all
10 select 97658, 'a', 2 from dual union all
11 select 97658, 'b', 3 from dual union all
12 --
13 select 76567, 'a', 1 from dual union all
14 select 76567, 'a', 2 from dual
15 )
16 select *
17 from test
18 where telno in (select telno from test
19 where type = 'a'
20 and rank = 1
21 );
TELNO T RANK
---------- - ----------
76567 a 2
76567 a 1
76567 c 3
76567 b 2
76567 a 1
SQL>