Oracle SQL:将某些记录从一个 table 转移到另一个根据条件过滤行
Oracle SQL: Transfer certain records from one table to another filtering rows based on condition
需要将某些列的某些记录从Table1
转移到Table2
,但要根据条件过滤行。
假设 Table1
如下所示,其中有很多列。
Table1
A B C D E F G H ...
1 24-OCT-20 08.22.57.642000000 AM 100 xyz 1 1
2 24-OCT-20 08.22.57.642000000 AM 100 xyz 1 0
13 25-OCT-20 05.47.52.733000000 PM 100 xyz 1 0
34 26-OCT-20 09.22.57.642000000 AM 100 xyz 1 0
25 26-OCT-20 09.25.57.642000000 AM 101 xyz 1 0
26 26-OCT-20 09.25.57.642000000 AM 101 xyz 1 1
6 26-OCT-20 09.25.57.642000000 AM 101 abc 1 1
10 26-OCT-20 09.25.57.642000000 AM 101 xyz 0 1
17 26-OCT-20 04.22.57.642000000 AM 100 xyz 1 0
18 26-OCT-20 06.22.57.642000000 AM 105 xyz 1 1
19 26-OCT-20 06.22.57.642000000 AM 105 xyz 1 0
在表 2 中,需要根据以下条件从表 1 中插入行:
首先,select A, B, C, D, E, F from Table1 where D='xyz' and E=1;
并在此查询的结果上应用以下 condition
以进一步过滤掉不需要的行:
Condition: For same values in columns B, C, D & E in 2 different rows, if column F has 2 different values then only keep the row with greater value in column A.
因此 Table2
中所需的输出如下所示:
Table2
A B C D E F
2 24-OCT-20 08.22.57.642000000 AM 100 xyz 1 0
13 25-OCT-20 05.47.52.733000000 PM 100 xyz 1 0
34 26-OCT-20 09.22.57.642000000 AM 100 xyz 1 0
26 26-OCT-20 09.25.57.642000000 AM 101 xyz 1 1
17 26-OCT-20 04.22.57.642000000 AM 100 xyz 1 0
19 26-OCT-20 06.22.57.642000000 AM 105 xyz 1 0
如何通过最简单、最高效的SQL查询来实现?
我们将不胜感激。
您可以使用 window 函数:
insert into table2 (a, b, c, d, e, f)
select a, b, c, d, e, f
from (
select t1.*,
row_number() over(partition by b, c, d, e order by a desc) rn
from table1 t1
where d = 'xyz' and e = 1
) t1
where rn = 1
这可以使用 GROUP BY
和 KEEP
子句实现,如下所示:
select max(t.a) as a, t.b, t.c, t.d, t.e,
max(t.f) keep (dense_rank last over order by t.a) as f
from t
where t.d = 'xyz' and t.e = 1
group by t.b, t.c, t.d, t.e
需要将某些列的某些记录从Table1
转移到Table2
,但要根据条件过滤行。
假设 Table1
如下所示,其中有很多列。
Table1
A B C D E F G H ...
1 24-OCT-20 08.22.57.642000000 AM 100 xyz 1 1
2 24-OCT-20 08.22.57.642000000 AM 100 xyz 1 0
13 25-OCT-20 05.47.52.733000000 PM 100 xyz 1 0
34 26-OCT-20 09.22.57.642000000 AM 100 xyz 1 0
25 26-OCT-20 09.25.57.642000000 AM 101 xyz 1 0
26 26-OCT-20 09.25.57.642000000 AM 101 xyz 1 1
6 26-OCT-20 09.25.57.642000000 AM 101 abc 1 1
10 26-OCT-20 09.25.57.642000000 AM 101 xyz 0 1
17 26-OCT-20 04.22.57.642000000 AM 100 xyz 1 0
18 26-OCT-20 06.22.57.642000000 AM 105 xyz 1 1
19 26-OCT-20 06.22.57.642000000 AM 105 xyz 1 0
在表 2 中,需要根据以下条件从表 1 中插入行:
首先,select A, B, C, D, E, F from Table1 where D='xyz' and E=1;
并在此查询的结果上应用以下 condition
以进一步过滤掉不需要的行:
Condition: For same values in columns B, C, D & E in 2 different rows, if column F has 2 different values then only keep the row with greater value in column A.
因此 Table2
中所需的输出如下所示:
Table2
A B C D E F
2 24-OCT-20 08.22.57.642000000 AM 100 xyz 1 0
13 25-OCT-20 05.47.52.733000000 PM 100 xyz 1 0
34 26-OCT-20 09.22.57.642000000 AM 100 xyz 1 0
26 26-OCT-20 09.25.57.642000000 AM 101 xyz 1 1
17 26-OCT-20 04.22.57.642000000 AM 100 xyz 1 0
19 26-OCT-20 06.22.57.642000000 AM 105 xyz 1 0
如何通过最简单、最高效的SQL查询来实现?
我们将不胜感激。
您可以使用 window 函数:
insert into table2 (a, b, c, d, e, f)
select a, b, c, d, e, f
from (
select t1.*,
row_number() over(partition by b, c, d, e order by a desc) rn
from table1 t1
where d = 'xyz' and e = 1
) t1
where rn = 1
这可以使用 GROUP BY
和 KEEP
子句实现,如下所示:
select max(t.a) as a, t.b, t.c, t.d, t.e,
max(t.f) keep (dense_rank last over order by t.a) as f
from t
where t.d = 'xyz' and t.e = 1
group by t.b, t.c, t.d, t.e