在Oracle数据库中,我有一个表,其内容类似于以下内容:
Market_Intro_Date Change_Date Author
-------------------------------------------
01.06.2025 10.07.2020 Meyer *
01.01.2025 30.06.2020 Harrin
01.01.2025 01.05.2020 Floyd
01.01.2025 15.04.2020 Peterson *
01.12.2024 20.03.2020 George
01.12.2024 10.03.2020 Smith
01.12.2024 15.01.2020 George *
01.01.2025 15.12.2019 Lee
01.01.2025 01.11.2019 Alfonso
01.01.2025 10.10.2019 Peterson *
01.07.2025 30.09.2019 Smith *
01.07.2024 20.09.2019 Lee
01.07.2024 10.09.2019 Meyer
01.07.2024 01.05.2019 Smith *
我需要一个SQL查询,该查询将返回市场引入日期的每一次更改(最后带有*标记)的首次出现以及更改日期和作者。
所以查询的结果将是:
Market_Intro_Date Change_Date Author
-------------------------------------------
01.06.2025 10.07.2020 Meyer
01.01.2025 15.04.2020 Peterson
01.12.2024 15.01.2020 George
01.01.2025 10.10.2019 Peterson
01.07.2024 01.05.2019 Smith
提前谢谢
答案 0 :(得分:1)
您可以使用扩展名为select Market_Intro_Date,
min(Change_Date) as Change_Date,
min(Author) keep (dense_rank first order by Change_Date) as Author
from your_table
group by Market_Intro_Date
MARKET_INTRO_DATE | CHANGE_DATE | AUTHOR
:---------------- | :---------- | :-------
01-JUL-24 | 01-MAY-19 | Smith
01-DEC-24 | 15-JAN-20 | George
01-JAN-25 | 10-OCT-19 | Peterson
01-JUN-25 | 10-JUL-20 | Meyer
01-JUL-25 | 30-SEP-19 | Smith
的聚合:
select Market_Intro_Date,
min(Change_Date) as Change_Date,
min(Author) keep (dense_rank first order by Change_Date) as Author
from (
select Market_Intro_Date, Change_Date, Author,
row_number() over (partition by Market_Intro_Date order by Change_Date)
- row_number() over (order by Change_Date) as grp
from your_table
)
group by Market_Intro_Date, grp
order by Change_Date desc;
MARKET_INTRO_DATE | CHANGE_DATE | AUTHOR
:---------------- | :---------- | :-------
01-JUN-25 | 10-JUL-20 | Meyer
01-JAN-25 | 15-APR-20 | Peterson
01-DEC-24 | 15-JAN-20 | George
01-JAN-25 | 10-OCT-19 | Peterson
01-JUL-25 | 30-SEP-19 | Smith
01-JUL-24 | 01-MAY-19 | Smith
我错过了其中一个日期重复的问题,所以这是一个空白和孤岛的问题。 Tabibitosan进行营救:
$JENKINS_HOME/jobs
Smith也有第二行,但这看起来是正确的;不知道您的样本数据是否遗漏了星号或01.07.2025是错字。
答案 1 :(得分:1)
澄清后已更新
Match_recognize通常比基于解析函数的旧start_of_group解决方案更快:
select *
from your_tab
match_recognize (
order by Change_Date
measures
first(Change_Date) as Change_Date,
first(Market_Intro_Date) as Market_Intro_Date,
first(Author ) as Author
pattern (A B*)
define
b as Market_Intro_Date = prev(Market_Intro_Date) and Change_Date>prev(Change_Date)
)
order by 1;
包含测试数据的完整示例:
with your_tab(Market_Intro_Date, Change_Date, Author) as (
select to_date('01.06.2025','dd.mm.yyyy'), to_date('10.07.2020','dd.mm.yyyy'), 'Meyer ' from dual union all
select to_date('01.01.2025','dd.mm.yyyy'), to_date('30.06.2020','dd.mm.yyyy'), 'Harrin ' from dual union all
select to_date('01.01.2025','dd.mm.yyyy'), to_date('01.05.2020','dd.mm.yyyy'), 'Floyd ' from dual union all
select to_date('01.01.2025','dd.mm.yyyy'), to_date('15.04.2020','dd.mm.yyyy'), 'Peterson' from dual union all
select to_date('01.12.2024','dd.mm.yyyy'), to_date('20.03.2020','dd.mm.yyyy'), 'George ' from dual union all
select to_date('01.12.2024','dd.mm.yyyy'), to_date('10.03.2020','dd.mm.yyyy'), 'Smith ' from dual union all
select to_date('01.12.2024','dd.mm.yyyy'), to_date('15.01.2020','dd.mm.yyyy'), 'George ' from dual union all
select to_date('01.01.2025','dd.mm.yyyy'), to_date('15.12.2019','dd.mm.yyyy'), 'Lee ' from dual union all
select to_date('01.01.2025','dd.mm.yyyy'), to_date('01.11.2019','dd.mm.yyyy'), 'Alfonso ' from dual union all
select to_date('01.01.2025','dd.mm.yyyy'), to_date('10.10.2019','dd.mm.yyyy'), 'Peterson' from dual union all
select to_date('01.07.2025','dd.mm.yyyy'), to_date('30.09.2019','dd.mm.yyyy'), 'Smith ' from dual union all
select to_date('01.07.2024','dd.mm.yyyy'), to_date('20.09.2019','dd.mm.yyyy'), 'Lee ' from dual union all
select to_date('01.07.2024','dd.mm.yyyy'), to_date('10.09.2019','dd.mm.yyyy'), 'Meyer ' from dual union all
select to_date('01.07.2024','dd.mm.yyyy'), to_date('01.05.2019','dd.mm.yyyy'), 'Smith ' from dual
)
select *
from your_tab
match_recognize (
order by Change_Date
measures
first(Change_Date) as Change_Date,
first(Market_Intro_Date) as Market_Intro_Date,
first(Author ) as Author
pattern (A B*)
define
b as Market_Intro_Date = prev(Market_Intro_Date) and Change_Date>prev(Change_Date)
)
order by 1;
CHANGE_DATE MARKET_INTRO_DATE AUTHOR
------------------- ------------------- --------
2019-05-01 00:00:00 2024-07-01 00:00:00 Smith
2019-09-30 00:00:00 2025-07-01 00:00:00 Smith
2019-10-10 00:00:00 2025-01-01 00:00:00 Peterson
2020-01-15 00:00:00 2024-12-01 00:00:00 George
2020-04-15 00:00:00 2025-01-01 00:00:00 Peterson
2020-07-10 00:00:00 2025-06-01 00:00:00 Meyer
答案 2 :(得分:0)
类似的事情应该起作用:
select market_intro_date, change_date, replace(author,' *','') author
from table
where author like '%*%';
答案 3 :(得分:0)
您可以按以下方式使用not exists
:
Select t.*
From your_table t
Where not exists (select 1 from your_table tt
Where t.Market_Intro_Date = tt.Market_Intro_Date
And tt.changed_date < t.changed_date)