[英]How do I get 1 week data from year 2018 based on 7 days based on this year in oracle?
I am trying to dynamically retrieve data based on dates.我正在尝试根据日期动态检索数据。 For example I want data from 7 days before today and same 7 days from 2020 dynamically.
例如,我想要动态地从今天前 7 天和从 2020 年开始的相同 7 天的数据。
I tried我试过了
SELECT *
FROM table_1
WHERE insert_date > TRUNC(SYSDATE) - 7 or ((insert_date< trunc(sysdate)-365) and (insert_date> trunc(sysdate)-372))
order by insert_date
The problem with this query is if I were to run this query now, this will give me correct data for 2020 and 2021. However if I were to run this same query in 2022, it will give me data from 2022 and 2021 when I want is data based on 2022 and 2020. I was hoping if someone could help me with this issue.此查询的问题是,如果我现在运行此查询,这将为我提供 2020 年和 2021 年的正确数据。但是,如果我要在 2022 年运行相同的查询,它将在我需要时为我提供 2022 年和 2021 年的数据是基于 2022 年和 2020 年的数据。我希望有人可以帮助我解决这个问题。 I was able to figure out if I want to compare month to month but not week..
我能够弄清楚我是否想比较每月而不是每周..
Thank you,谢谢,
Sam山姆
Presume that假设
today is (yyyy-mm-dd) 2021-04-08
which means that you'd want to return rows whose insert_date
is今天是 (yyyy-mm-dd)
2021-04-08
,这意味着您想要返回insert_date
为的行
2021-04-01
and 2021-04-08
2021-04-01
和2021-04-08
2020-04-01
and 2020-04-08
2020-04-01
和2020-04-08
2022-04-08
)2022-04-08
)
2022-04-01
and 2022-04-08
2022-04-01
和2022-04-08
2020-04-01
and 2020-04-08
2020-04-01
和2020-04-08
this is contents of your table:这是您的表格的内容:
SQL> select * from table_1 order by id; ID INSERT_DAT Return in ---------- ---------- 1 2021-04-08 -- 2021 2 2021-04-03 -- 2021 3 2021-04-02 -- 2021 4 2021-03-31 5 2020-04-05 -- 2021 and 2022 6 2020-04-04 -- 2021 and 2022 7 2020-04-12 8 2022-04-08 -- 2022 9 2022-03-30 9 rows selected.
Query;询问; lines #4 and 5 make sure that query returns rows in year 2020:
第 4 行和第 5 行确保查询返回 2020 年的行:
SQL> select sysdate from dual;
SYSDATE
----------
2021-04-08
SQL> select a.*
2 from table_1 a
3 where a.insert_date between trunc(sysdate) - 7 and trunc(sysdate)
4 or a.insert_date between add_months(trunc(sysdate), -12 * (extract(year from sysdate) - 2020)) - 7
5 and add_months(trunc(sysdate), -12 * (extract(year from sysdate) - 2020))
6 order by a.insert_date desc;
ID INSERT_DAT
---------- ----------
1 2021-04-08
2 2021-04-03
3 2021-04-02
5 2020-04-05
6 2020-04-04
SQL>
Next year, on 2022-04-08, query would return明年,2022-04-08,查询将返回
SQL> select sysdate from dual;
SYSDATE
----------
2022-04-08
SQL> select a.*
2 from table_1 a
3 where a.insert_date between trunc(sysdate) - 7 and trunc(sysdate)
4 or a.insert_date between add_months(trunc(sysdate), -12 * (extract(year from sysdate) - 2020)) - 7
5 and add_months(trunc(sysdate), -12 * (extract(year from sysdate) - 2020))
6 order by a.insert_date desc;
ID INSERT_DAT
---------- ----------
8 2022-04-08
5 2020-04-05
6 2020-04-04
SQL>
Didn't quite got the requirement.完全没有达到要求。 But, assuming you want to take 2020 as a standard date and get 7 days of data from today and the same 7 days from 2020.
但是,假设您想以 2020 年为标准日期并获取从今天起 7 天的数据以及从 2020 年起同样的 7 天数据。
I had used the substr to remove the year part and replace it with the standard year.我使用 substr 删除了年份部分并将其替换为标准年份。 As I had difficulty testing with 2022 date, took 2019 as my standard date.
由于我在测试 2022 日期时遇到困难,所以将 2019 年作为我的标准日期。
select distinct cast(to_date(p.insert_date, 'DD-MON-YYYY') as timestamp) as insert_date,
cast(to_date(SYSDATE-7, 'DD-MON-YYYY') as timestamp) as tst_date
from table_1 p
where 1=1 and ( (to_date(p.insert_date, 'DD-MON-YYYY') > to_date(SYSDATE-7, 'DD-MON-YYYY') )
or (to_date(p.insert_date, 'DD-MON-YYYY')> trunc(to_date(substr(to_date(SYSDATE, 'DD-MON-YYYY'),1,7)||'19', 'DD-MON-YYYY')-7)
and to_date(p.insert_date, 'DD-MON-YYYY') <= trunc(to_date(substr(to_date(SYSDATE, 'DD-MON-YYYY'),1,7)||'19', 'DD-MON-YYYY')))
)
order by create_date desc;
-- Standard date for 2020
select substr(to_date(SYSDATE, 'DD-MON-YYYY'),1,7)||'20' from dual;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.