0

查询应返回每个月的最后一周。例如,如果 2013 年 3 月的最后一周是星期日,则查询应显示 2013 年 3 月 29 日(星期五)。

4

2 回答 2

1

尝试这个:

select dte as last_day, dayofweek_abbr as original_dayofweek, 
dte - (decode(dayofweek, 7, 1, 1, 2, 0)) as last_weekday_america
from (
    select last_day(to_date(to_char(level, '09') || '2013', 'MMYYYY')) as dte,
    to_char(last_day(to_date(to_char(level, '09') || '2013', 'MMYYYY')), 'D') as dayofweek,
    to_char(last_day(to_date(to_char(level, '09') || '2013', 'MMYYYY')), 'DAY') as dayofweek_abbr
    from dual
    connect by level <= 12
)

输出:

LAST_DAY    ORIGINAL_DAYOFWEEK  LAST_WEEKDAY_AMERICA
1/31/2013   THURSDAY    1/31/2013
2/28/2013   THURSDAY    2/28/2013
3/31/2013   SUNDAY      3/29/2013
4/30/2013   TUESDAY     4/30/2013
5/31/2013   FRIDAY      5/31/2013
6/30/2013   SUNDAY      6/28/2013
7/31/2013   WEDNESDAY   7/31/2013
8/31/2013   SATURDAY    8/30/2013
9/30/2013   MONDAY      9/30/2013
10/31/2013  THURSDAY    10/31/2013
11/30/2013  SATURDAY    11/29/2013
12/31/2013  TUESDAY     12/31/2013

请注意,如果您的 nls_territory 不同,您可能需要调整解码。

于 2013-06-04T14:27:49.317 回答
0

跟进 Ed 的提示和 tbone 的代码。以您想要的方式格式化日期。FMDAY 格式化程序会在一周中的某一天之后删除多余的空格。

SELECT dte AS last_day, 
       dayofweek_abbr AS original_dayofweek, 
       to_char(dte - (decode(dayofweek, 7, 1, 1, 2, 0)),'DD MON YYYY(FMDAY)') AS     last_weekday_america
FROM (
      SELECT last_day(to_date(to_char(level, '09') || '2013', 'MMYYYY')) as dte,
             to_char(last_day(to_date(to_char(level, '09') || '2013', 'MMYYYY')), 'D') as dayofweek,
             to_char(last_day(to_date(to_char(level, '09') || '2013', 'MMYYYY')), 'DAY') as      dayofweek_abbr
      FROM dual
      CONNECT BY LEVEL <= 12
)
于 2013-06-04T14:40:53.520 回答