oracle 我如何从不同的日期得到季节?

ztigrdn8  于 2023-11-17  发布在  Oracle
关注(0)|答案(2)|浏览(155)

我有这个专栏

Date
-------
01-Jun-2005
06-Sep-2008
25-Dec-2010
30-Mar-2003
31-Aug-2001
17-Oct-2002
20-May-2007
15-Apr-2006
18-Feb-2007
16-Jan-2002

字符串
我想把这些

Spring dates - 20 march - 21 June 
Summer dates 21 June - 22 september
Autumn dates 22 September - 22 December
Winter dates 22 December - 20 march

Date         Season
-------      -------
01-Jun-2005   Summer
06-Sep-2008   Spring
25-Dec-2010   Winter
30-Mar-2003   Spring
31-Aug-2001   Summer
17-Oct-2002   Autumn
20-May-2007   Summer
15-Apr-2006   Spring
18-Feb-2007   Winter
16-Jan-2002   Winter


查询:

SELECT Dates,
CASE WHEN Dates BETWEEN '22-Dec-%%' AND '20-Mar-%%' THEN 'Winter'
     WHEN Dates BETWEEN '21-Mar-%%' AND '21-Jun-%%' THEN 'Spring'
     WHEN Dates BETWEEN '22-Jun-%%' AND '22-Sep-%%' THEN 'Summer'
     ELSE 'Winter' END AS Season
FROM T1

cx6n0qe3

cx6n0qe31#

CASE表达式是一个选项,但是-使其有效。例如像这样(注意,我修改了季节日期,以便它们实际上代表我们在学校学到的东西)。
样本数据:

SQL> with test (dates) as
  2  (select
  3  date '2005-06-01' from dual union all select
  4  date '2008-09-06' from dual union all select
  5  date '2010-12-25' from dual union all select
  6  date '2003-03-30' from dual union all select
  7  date '2001-08-31' from dual union all select
  8  date '2002-10-17' from dual union all select
  9  date '2007-05-20' from dual union all select
 10  date '2006-04-15' from dual union all select
 11  date '2007-02-18' from dual union all select
 12  date '2002-01-16' from dual
 13  )

字符串
查询:

14  select dates,
 15         case when to_char(dates, 'mmdd') between '0321' and '0620' then 'Spring'
 16              when to_char(dates, 'mmdd') between '0621' and '0922' then 'Summer'
 17              when to_char(dates, 'mmdd') between '0923' and '1220' then 'Autumn'
 18              --
 19              when to_char(dates, 'mmdd') between '1221' and '1231'
 20                or to_char(dates, 'mmdd') between '0101' and '0320' then 'Winter'
 21         end as season
 22  from test;

DATES       SEASON
----------- ------
01-Jun-2005 Spring
06-Sep-2008 Summer
25-Dec-2010 Winter
30-Mar-2003 Spring
31-Aug-2001 Summer
17-Oct-2002 Autumn
20-May-2007 Spring
15-Apr-2006 Spring
18-Feb-2007 Winter
16-Jan-2002 Winter

10 rows selected.

SQL>

v1l68za4

v1l68za42#

一种可能性是规范化输入日期,使其始终是同一年。在这个例子中,我使用表达式ADD_MONTHS(Dates, 12 * (2000 - EXTRACT(YEAR FROM Dates)))将其始终设置为2000年:

WITH cte AS (
    SELECT
        Dates,
        ADD_MONTHS(Dates, 12 * (2000 - EXTRACT(YEAR FROM Dates))) AS d2000
    FROM T1
)
SELECT
    Dates,
    CASE
        WHEN d2000 BETWEEN TO_DATE('20-Mar-2000', 'DD-MON-YYYY') AND
                           TO_DATE('20-Jun-2000', 'DD-MON-YYYY') THEN 'Spring'
        WHEN d2000 BETWEEN TO_DATE('21-Jun-2000', 'DD-MON-YYYY') AND
                           TO_DATE('21-Sep-2000', 'DD-MON-YYYY') THEN 'Summer'
        WHEN d2000 BETWEEN TO_DATE('22-Sep-2000', 'DD-MON-YYYY') AND
                           TO_DATE('20-Dec-2000', 'DD-MON-YYYY') THEN 'Autumn'
        ELSE 'Winter'
    END AS Season
FROM cte

字符串
请注意,冬天跨越了两年,这会使我们与BETWEEN的比较失败。我通过处理ELSE案例中的冬天来避免这个困难。

相关问题