简体   繁体   English

oracle 查询只上报上半年或下半年的数据

[英]oracle query to report data only for the 1st or 2nd half of the year

I have a report which should display enrollment data only within 2 date ranges Jan-June or July-dec depending on current date.我有一份报告,该报告应仅显示 1 月至 6 月或 7 月至 12 月 2 个日期范围内的注册数据,具体取决于当前日期。 Scenarios:场景:

  • If the current date is 042020 then I should display enrollement data between this range: 072019-122019如果当前日期是 042020 那么我应该显示这个范围内的注册数据:072019-122019
  • If the current date is 072020 then I should display enrollement data between this range: 012020-062020如果当前日期是 072020,那么我应该显示以下范围内的注册数据:012020-062020
  • If the current date is 022021 then I should display enrollement data between this range: 072020-122020如果当前日期是 022021 那么我应该显示这个范围内的注册数据:072020-122020

Current query reports everything past 6 months with his query.当前查询通过他的查询报告过去 6 个月的所有内容。

select * from enrollement where enrollement_dt > add_months(sysdate - 6);

Is there any function available in oracle to do the same or how do i get the logic in a single statement? oracle 中是否有可用的 function 来做同样的事情,或者我如何在单个语句中获得逻辑?

Any help with this is highly appreciated.非常感谢您对此的任何帮助。

You can use the below to get the start date and end date for enrollment您可以使用以下获取注册的开始日期和结束日期

        WITH data
         AS (SELECT TRUNC(SYSDATE) curr_date from dual
             ),
         d2
         AS (SELECT curr_date,
                    To_date('0107'
                            ||( Extract (year FROM curr_date) - 1 ), 'ddmmyyyy')
                       start_first_half,
                    To_date('3112'
                            ||( Extract (year FROM curr_date) - 1 ), 'ddmmyyyy')
                       end_first_half,
                    To_date('0101'
                            ||Extract (year FROM curr_date), 'ddmmyyyy')
                       start_second_half,
                    To_date('3006'
                            ||Extract (year FROM curr_date), 'ddmmyyyy')
                       end_second_half
             FROM   data)
    SELECT curr_date,
           CASE
             WHEN To_char(curr_date, 'MM') >= To_char(start_first_half, 'MM')
                  AND To_char(curr_date, 'MM') <= To_char(end_first_half, 'MM') THEN
             start_second_half
             ELSE start_first_half
           END start_date1,
           CASE
             WHEN To_char(curr_date, 'MM') >= To_char(start_first_half, 'MM')
                  AND To_char(curr_date, 'MM') <= To_char(end_first_half, 'MM') THEN
             end_second_half
             ELSE end_first_half
           END end_date1
    FROM   d2 

You can use it in your query like below您可以在查询中使用它,如下所示

  Select * from enrollment_table a, (WITH data
     AS (SELECT TRUNC(SYSDATE) curr_date from dual
         ),
     d2
     AS (SELECT curr_date,
                To_date('0107'
                        ||( Extract (year FROM curr_date) - 1 ), 'ddmmyyyy')
                   start_first_half,
                To_date('3112'
                        ||( Extract (year FROM curr_date) - 1 ), 'ddmmyyyy')
                   end_first_half,
                To_date('0101'
                        ||Extract (year FROM curr_date), 'ddmmyyyy')
                   start_second_half,
                To_date('3006'
                        ||Extract (year FROM curr_date), 'ddmmyyyy')
                   end_second_half
         FROM   data)
SELECT curr_date,
       CASE
         WHEN To_char(curr_date, 'MM') >= To_char(start_first_half, 'MM')
              AND To_char(curr_date, 'MM') <= To_char(end_first_half, 'MM') THEN
         start_second_half
         ELSE start_first_half
       END start_date1,
       CASE
         WHEN To_char(curr_date, 'MM') >= To_char(start_first_half, 'MM')
              AND To_char(curr_date, 'MM') <= To_char(end_first_half, 'MM') THEN
         end_second_half
         ELSE end_first_half
       END end_date1
FROM   d2 ) b
where a.enrollment_date >=b.start_date1
and a.enrollment_date <=b.end_date1

You may try below query -您可以尝试以下查询 -

select *
from enrollement
WHERE TO_CHAR(enrollement_dt, 'MMYYYY') >= CASE WHEN TO_CHAR(SYSDATE, 'mm') <= '06'
                                                          THEN TO_DATE('07' || EXTRACT(YEAR FROM SYSDATE) - 1, 'MMYYYY') 
                                                ELSE THEN TO_DATE('01' || EXTRACT(YEAR FROM SYSDATE), 'MMYYYY')
                                           END
  AND TO_CHAR(enrollement_dt, 'MMYYYY') <= CASE WHEN TO_CHAR(SYSDATE, 'mm') <= '06'
                                                          THEN TO_DATE('12' || EXTRACT(YEAR FROM SYSDATE) - 1, 'MMYYYY') 
                                                ELSE THEN TO_DATE('06' || EXTRACT(YEAR FROM SYSDATE), 'MMYYYY')
                                           END

Basically you want to truncate to the half-year.基本上你想截断到半年。 But Oracle doesn't support this.但是 Oracle 不支持这个。

One method counts half-years and compares them.一种方法计算半年并进行比较。 You want the previous half year from the current date.您想要从当前日期算起的前半年。 That would be:那将是:

select (extract(year from sysdate) * 2 + floor(extract(month from sysdate) - 1) / 6) - 1
from dual

You can use this same formula:您可以使用相同的公式:

where (extract(year from enrollement_dt) * 2 + floor(extract(month from enrollement_dt) - 1) / 6) - 1 =
       extract(year from sysdate) * 2 + floor(extract(month from sysdate) - 1) / 6) - 1
      )

from dual;

Unfortunately that can't use an index on the column.不幸的是,它不能在该列上使用索引。 So, we can revisit this.所以,我们可以重新审视这一点。 You can get the first day of the current half using some date arithmetic:您可以使用一些日期算法获取当前一半的第一天:

select trunc(sysdate, 'Q') - mod(floor((extract(month from sysdate) - 1) / 3), 2) * interval '3' month
from dual

That just needs to be plugged into a where clause:这只需要插入一个where子句:

where enrollement_dt >= trunc(sysdate, 'Q') - mod(floor((extract(month from sysdate) - 1) / 3), 2) * interval '3' month - interval '6' month and
      enrollement_dt < trunc(sysdate, 'Q') - mod(floor((extract(month from sysdate) - 1) / 3), 2) * interval '3' month

Voila.瞧。 An expression that can even use an index.一个甚至可以使用索引的表达式。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQL查询:在第1和第2日期之前进行数据透视 - SQL query: pivot data by 1st and 2nd date 如何从第一个查询中获取所有值而从第二个查询中仅获取一些值? - how to get all values from 1st query and only some values from 2nd query? 会不会有这样的情况,比如只把第一列的数据带到第一行,第二行把第二列的数据带到第二行? - Could there be a situation like bringing only the data of the 1st column to first row and the 2nd row the data of the 2nd column? T-sql运行第一个查询,然后执行CASE第二个查询并输出数据或“否” - T-sql run 1st query and then CASE 2nd query and output data or 'No' 仅存在表的第一个数据库中的第二个数据库中的sql更新数据 - sql update data in 2nd db from 1st db only where table exists SQL连接2个表,从第2个条件查询第1个表 - SQL join 2 tables, query 1st with condition from 2nd SQL查询与第一和第二最高薪水有关 - Sql query related to 1st and 2nd highest salary 如何使用第一个查询的输出作为第二个查询的输入? - How to use output of 1st query as input for 2nd one? 将数据从第三张表链接到第一张表和第二张表 - Link data from third table to a 1st and a 2nd table 如果第 2 个表中存在第 1 个表的值,则用第 1 个和第 2 个表的总和更新第 2 个表的值,否则将第一个表数据插入到第 2 个表 - If value of 1 table exists in the 2nd table, update the 2nd table value with sum of 1st and 2nd tables, else insert 1st table data to 2nd table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM