查看: 4927|回复: 18

如何用SQL实现unix cal的功能?

[复制链接]
论坛徽章:
407
紫蛋头
日期:2012-05-21 10:19:41迷宫蛋
日期:2012-06-06 16:02:49奥运会纪念徽章:足球
日期:2012-06-29 15:30:06奥运会纪念徽章:排球
日期:2012-07-10 21:24:24鲜花蛋
日期:2012-07-16 15:24:59奥运会纪念徽章:拳击
日期:2012-08-07 10:54:50奥运会纪念徽章:羽毛球
日期:2012-08-21 15:55:33奥运会纪念徽章:蹦床
日期:2012-08-21 21:09:51奥运会纪念徽章:篮球
日期:2012-08-24 10:29:11奥运会纪念徽章:体操
日期:2012-09-07 16:40:00
跳转到指定楼层
1#
发表于 2009-4-22 14:26 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
http://cpansearch.perl.org/src/PIERS/UNIX-Cal-0.01/mylib/cal.c  
/*
* Copyright (c) 1989, 1993, 1994
*        The Regents of the University of California.  All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Kim Letkeman.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
*    notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
*    notice, this list of conditions and the following disclaimer in the
*    documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
*    must display the following acknowledgement:
*        This product includes software developed by the University of
*        California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
*    may be used to endorse or promote products derived from this software
*    without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/

/* 1999-02-01        Jean-Francois Bignolles: added option '-m' to display
*                 monday as the first day of the week.
*/

/* This defines _LINUX_C_LIB_VERSION_MAJOR, dunno about gnulibc.  We
   don't want it to read /usr/i586-unknown-linux/include/_G_config.h
   so we specify fill path.  Were we got /usr/i586-unknown-linux from?
   Dunno. */

/*

  moved all the main stuff out to functions
  and created necessary functions for Perl to get hold of the date
  info

*/

#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"

#include "/usr/include/_G_config.h"

#include <sys/types.h>

#include <ctype.h>
//#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <locale.h>

/* Test changes to deal with gnulibc, Linux libc 5. */
/* #if defined(__linux__) && _LINUX_C_LIB_VERSION_MAJOR > 4 */
#if _LINUX_C_LIB_VERSION_MAJOR - 0 > 4 || __GNU_LIBRARY__ - 0 >= 5
# define LANGINFO 1
#else
# define LANGINFO 0
#endif

#if LANGINFO
# include <langinfo.h>
#else
# include <localeinfo.h>
#endif

#define        THURSDAY                4                /* for reformation */
#define        SATURDAY                 6                /* 1 Jan 1 was a Saturday */

#define        FIRST_MISSING_DAY         639787                /* 3 Sep 1752 */
#define        NUMBER_MISSING_DAYS         11                /* 11 day correction */

#define        MAXDAYS                        43                /* max slots in a month array */
#define        SPACE                        -1                /* used in day array */

static int days_in_month[2][13] = {
        {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
        {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
};

int sep1752[MAXDAYS] = {
        SPACE,        SPACE,        1,        2,        14,        15,        16,
        17,        18,        19,        20,        21,        22,        23,
        24,        25,        26,        27,        28,        29,        30,
        SPACE,        SPACE,        SPACE,        SPACE,        SPACE,        SPACE,        SPACE,
        SPACE,        SPACE,        SPACE,        SPACE,        SPACE,        SPACE,        SPACE,
        SPACE,        SPACE,        SPACE,        SPACE,        SPACE,        SPACE,        SPACE,
        SPACE
}, j_sep1752[MAXDAYS] = {
        SPACE,        SPACE,        245,        246,        258,        259,        260,
        261,        262,        263,        264,        265,        266,        267,
        268,        269,        270,        271,        272,        273,        274,
        SPACE,        SPACE,        SPACE,        SPACE,        SPACE,        SPACE,        SPACE,
        SPACE,        SPACE,        SPACE,        SPACE,        SPACE,        SPACE,        SPACE,
        SPACE,        SPACE,        SPACE,        SPACE,        SPACE,        SPACE,        SPACE,
        SPACE
}, empty[MAXDAYS] = {
        SPACE,        SPACE,        SPACE,        SPACE,        SPACE,        SPACE,        SPACE,
        SPACE,        SPACE,        SPACE,        SPACE,        SPACE,        SPACE,        SPACE,
        SPACE,        SPACE,        SPACE,        SPACE,        SPACE,        SPACE,        SPACE,
        SPACE,        SPACE,        SPACE,        SPACE,        SPACE,        SPACE,        SPACE,
        SPACE,        SPACE,        SPACE,        SPACE,        SPACE,        SPACE,        SPACE,
        SPACE,        SPACE,        SPACE,        SPACE,        SPACE,        SPACE,        SPACE,
        SPACE
};

char day_headings[]   = " S  M Tu  W Th  F  S ";
/* week1stday = 1  =>   " M Tu  W Th  F  S  S " */
char j_day_headings[] = "  S   M  Tu   W  Th   F   S ";
/* week1stday = 1  =>   "  M  Tu   W  Th   F   S   S " */
const char *full_month[12];

/* leap year -- account for gregorian reformation in 1752 */
#define        leap_year(yr) \
        ((yr) <= 1752 ? !((yr) % 4) : \
        (!((yr) % 4) && ((yr) % 100)) || !((yr) % 400))

/* number of centuries since 1700, not inclusive */
#define        centuries_since_1700(yr) \
        ((yr) > 1700 ? (yr) / 100 - 17 : 0)

/* number of centuries since 1700 whose modulo of 400 is 0 */
#define        quad_centuries_since_1700(yr) \
        ((yr) > 1600 ? ((yr) - 1600) / 400 : 0)

/* number of leap years between year 1 and this year, not inclusive */
#define        leap_years_since_year_1(yr) \
        ((yr) / 4 - centuries_since_1700(yr) + quad_centuries_since_1700(yr))

/* 0 => sunday (default), 1 => monday */
int week1stday;
int julian;
int yflag;

SV*        ascii_day __P((char *, int));
void        day_array __P((int, int, int *));
int        day_in_week __P((int, int, int));
int        day_in_year __P((int, int, int));
//void        j_yearly __P((int));
AV*        j_yearly __P((int));
//void        monthly __P((int, int));
AV*        monthly __P((int, int));
void        usage __P((void));
//void        yearly __P((int));
AV*        yearly __P((int));
//void    headers_init(void);
AV*    headers_init(void);


#define        DAY_LEN                3                /* 3 spaces per day */
#define        J_DAY_LEN        4                /* 4 spaces per day */
#define        WEEK_LEN        21                /* 7 days * 3 characters */
#define        J_WEEK_LEN        28                /* 7 days * 4 characters */
#define        HEAD_SEP        2                /* spaces between day headings */
#define        J_HEAD_SEP        2

int
onLoad(int p_monday, int p_julian, int p_year)
{
        struct tm *local_time;
        time_t now;
        int ch, month, year;
          int i;

        setlocale(LC_ALL,"");
        week1stday = p_monday;
        julian = p_julian;
        yflag = p_year;

  
        for (i = 0; i < 12; i++) {
#if defined(__linux__) && (_LINUX_C_LIB_VERSION_MAJOR > 4 || __GNU_LIBRARY__ > 1)
             full_month[ i] = nl_langinfo(MON_1+i);
#else
             full_month[ i] = _time_info->full_month[ i];
#endif
        }

        return 1;

}


SV*
doCal(int p_month, int p_year)
{
        struct tm *local_time;
        time_t now;
        int ch, month, year;

        month = year = 0;

        if ( p_month != 0 )
          month = p_month;
        if ( p_year != 0 )
          year = p_year;

        if ( p_month == 0 && p_year == 0 )
        {
                (void)time(&now);
                local_time = localtime(&now);
                year = local_time->tm_year + 1900;
                if (!yflag)
                        month = local_time->tm_mon + 1;
        }

        if (month)
                return newRV_noinc((SV*) monthly(month, year));
        else if (julian)
                return newRV_noinc((SV*) j_yearly(year));
        else
                return newRV_noinc((SV*) yearly(year));

}


AV* headers_init(void)
{
  int i, wd;
  AV* array = newAV();


#if defined(__linux__) && (_LINUX_C_LIB_VERSION_MAJOR > 4 || __GNU_LIBRARY__ > 1)
# define weekday(wd)        nl_langinfo(ABDAY_1+wd)
#else
# define weekday(wd)        _time_info->abbrev_wkday[wd]
#endif
  
  for(i = 0 ; i < 7 ; i++ ) {
     wd = (i + week1stday) % 7;
     av_push(array, newSVpv(weekday(wd),julian == 1 ? 3 : 2 ));
  }

#undef weekday

  return array;
}

AV*
monthly(month, year)
        int month, year;
{
        int col, row, len, days[MAXDAYS];
        char *p, lineout[30];
        AV* warray;
        AV* marray = newAV();
        AV* mhdr = newAV();

        day_array(month, year, days);

        av_push(mhdr, newSVpvf(full_month[month - 1]));
        av_push(mhdr, newSViv(year));
        av_push(marray, newRV_noinc((SV*) mhdr));
        av_push(marray, newRV_noinc((SV*) headers_init()));

        for (row = 0; row < 6; row++) {
                if (days[row * 7 + 0] == SPACE && row > 1 )
                   break;

                warray = newAV();
                for (col = 0, p = lineout; col < 7; col++) {
                        av_push(warray, ascii_day(p, days[row * 7 + col]));
                }
                av_push(marray, newRV_noinc((SV*) warray));
        }

  return marray;
}

AV*
j_yearly(year)
        int year;
{
        int col, *dp, i, month, row, which_cal;
        int days[12][MAXDAYS];
        char *p, lineout[80];
        AV* yarray = newAV();
        AV* yhdr = newAV();

        av_push(yhdr, newSViv(year));
        av_push(yarray, newRV_noinc((SV*) yhdr));

        for (i = 0; i < 12; i++)
                av_push(yarray, newRV_noinc((SV*) monthly(i + 1, year)));

        return yarray;
}

AV*
yearly(year)
        int year;
{
        int col, *dp, i, month, row, which_cal;
        int days[12][MAXDAYS];
        char *p, lineout[80];
        AV* yarray = newAV();
        AV* yhdr = newAV();

        av_push(yhdr, newSViv(year));
        av_push(yarray, newRV_noinc((SV*) yhdr));
        for (i = 0; i < 12; i++)
                av_push(yarray, newRV_noinc((SV*) monthly(i + 1, year)));
        return yarray;
}

/*
* day_array --
*        Fill in an array of 42 integers with a calendar.  Assume for a moment
*        that you took the (maximum) 6 rows in a calendar and stretched them
*        out end to end.  You would have 42 numbers or spaces.  This routine
*        builds that array for any month from Jan. 1 through Dec. 9999.
*/
void
day_array(month, year, days)
        int month, year;
        int *days;
{
        int day, dw, dm;
        int *d_sep1752;

        if (month == 9 && year == 1752) {
                d_sep1752 = julian ? j_sep1752 : sep1752;
                memcpy(days, d_sep1752 + week1stday, MAXDAYS * sizeof(int));
                return;
        }
        memcpy(days, empty, MAXDAYS * sizeof(int));
        dm = days_in_month[leap_year(year)][month];
        dw = (day_in_week(1, month, year) - week1stday + 7) % 7;
        day = julian ? day_in_year(1, month, year) : 1;
        while (dm--)
                days[dw++] = day++;
}

/*
* day_in_year --
*        return the 1 based day number within the year
*/
int
day_in_year(day, month, year)
        int day, month, year;
{
        int i, leap;

        leap = leap_year(year);
        for (i = 1; i < month; i++)
                day += days_in_month[leap][ i];
        return (day);
}

/*
* day_in_week
*        return the 0 based day number for any date from 1 Jan. 1 to
*        31 Dec. 9999.  Assumes the Gregorian reformation eliminates
*        3 Sep. 1752 through 13 Sep. 1752.  Returns Thursday for all
*        missing days.
*/
int
day_in_week(day, month, year)
        int day, month, year;
{
        long temp;

        temp = (long)(year - 1) * 365 + leap_years_since_year_1(year - 1)
            + day_in_year(day, month, year);
        if (temp < FIRST_MISSING_DAY)
                return ((temp - 1 + SATURDAY) % 7);
        if (temp >= (FIRST_MISSING_DAY + NUMBER_MISSING_DAYS))
                return (((temp - 1 + SATURDAY) - NUMBER_MISSING_DAYS) % 7);
        return (THURSDAY);
}

SV*
ascii_day(p, day)
        char *p;
        int day;
{
        int display, val;
        static char *aday[] = {
                "",
                " 1", " 2", " 3", " 4", " 5", " 6", " 7",
                " 8", " 9", "10", "11", "12", "13", "14",
                "15", "16", "17", "18", "19", "20", "21",
                "22", "23", "24", "25", "26", "27", "28",
                "29", "30", "31",
        };

        if (day == SPACE) {
                return newSVpvf("");
        }
        if (julian) {
                return newSViv(day);
        } else {
                return newSViv(atoi(aday[day]));
        }
}

[ 本帖最后由 〇〇 于 2009-4-22 14:39 编辑 ]
论坛徽章:
194
红宝石
日期:2014-05-09 08:24:37萤石
日期:2014-01-03 10:25:39奥运会纪念徽章:羽毛球
日期:2008-07-01 10:46:06奥运会纪念徽章:马术
日期:2008-07-07 17:43:24奥运会纪念徽章:射箭
日期:2008-07-25 18:07:39奥运会纪念徽章:皮划艇激流回旋
日期:2008-07-30 10:02:57奥运会纪念徽章:花样游泳
日期:2008-09-26 13:02:43奥运会纪念徽章:排球
日期:2008-12-03 11:23:272010新春纪念徽章
日期:2010-01-04 08:33:082010年世界杯参赛球队:澳大利亚
日期:2010-02-26 11:08:44
2#
发表于 2009-4-22 14:39 | 只看该作者
cal的源码?

使用道具 举报

回复
论坛徽章:
407
紫蛋头
日期:2012-05-21 10:19:41迷宫蛋
日期:2012-06-06 16:02:49奥运会纪念徽章:足球
日期:2012-06-29 15:30:06奥运会纪念徽章:排球
日期:2012-07-10 21:24:24鲜花蛋
日期:2012-07-16 15:24:59奥运会纪念徽章:拳击
日期:2012-08-07 10:54:50奥运会纪念徽章:羽毛球
日期:2012-08-21 15:55:33奥运会纪念徽章:蹦床
日期:2012-08-21 21:09:51奥运会纪念徽章:篮球
日期:2012-08-24 10:29:11奥运会纪念徽章:体操
日期:2012-09-07 16:40:00
3#
 楼主| 发表于 2009-4-22 14:40 | 只看该作者
cal (Unix)
From Wikipedia, the free encyclopedia
Jump to: navigation, search
cal is a standard program on Unix that prints an ASCII calendar of the given month or year. If the user does not specify any options, cal will print a calendar of the current month.

Contents [hide]
1 Examples
2 Features
3 History
4 See also
5 References
6 External links



[edit] Examples
$ cal
    October 2008
Su Mo Tu We Th Fr Sa
          1  2  3  4
5  6  7  8  9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31


$ cal 1992
                              1992                              

      January               February               March        
S  M Tu  W Th  F  S   S  M Tu  W Th  F  S   S  M Tu  W Th  F  S
          1  2  3  4                     1   1  2  3  4  5  6  7
5  6  7  8  9 10 11   2  3  4  5  6  7  8   8  9 10 11 12 13 14
12 13 14 15 16 17 18   9 10 11 12 13 14 15  15 16 17 18 19 20 21
19 20 21 22 23 24 25  16 17 18 19 20 21 22  22 23 24 25 26 27 28
26 27 28 29 30 31     23 24 25 26 27 28 29  29 30 31

       April                  May                   June        
S  M Tu  W Th  F  S   S  M Tu  W Th  F  S   S  M Tu  W Th  F  S
          1  2  3  4                  1  2      1  2  3  4  5  6
5  6  7  8  9 10 11   3  4  5  6  7  8  9   7  8  9 10 11 12 13
12 13 14 15 16 17 18  10 11 12 13 14 15 16  14 15 16 17 18 19 20
19 20 21 22 23 24 25  17 18 19 20 21 22 23  21 22 23 24 25 26 27
26 27 28 29 30        24 25 26 27 28 29 30  28 29 30
                      31
        July                 August              September      
S  M Tu  W Th  F  S   S  M Tu  W Th  F  S   S  M Tu  W Th  F  S
          1  2  3  4                     1         1  2  3  4  5
5  6  7  8  9 10 11   2  3  4  5  6  7  8   6  7  8  9 10 11 12
12 13 14 15 16 17 18   9 10 11 12 13 14 15  13 14 15 16 17 18 19
19 20 21 22 23 24 25  16 17 18 19 20 21 22  20 21 22 23 24 25 26
26 27 28 29 30 31     23 24 25 26 27 28 29  27 28 29 30
                      30 31
      October               November              December      
S  M Tu  W Th  F  S   S  M Tu  W Th  F  S   S  M Tu  W Th  F  S
             1  2  3   1  2  3  4  5  6  7         1  2  3  4  5
4  5  6  7  8  9 10   8  9 10 11 12 13 14   6  7  8  9 10 11 12
11 12 13 14 15 16 17  15 16 17 18 19 20 21  13 14 15 16 17 18 19
18 19 20 21 22 23 24  22 23 24 25 26 27 28  20 21 22 23 24 25 26
25 26 27 28 29 30 31  29 30                 27 28 29 30 31

[edit] Features
$ cal 9 1752
  September 1752
S  M Tu  W Th  F  S
       1  2 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
The Gregorian reformation was adopted by the Kingdom of Great Britain, including its possessions in North America (later to become eastern USA), in September 1752. As a result the September 1752 cal shows the adjusted days missing. This month was the official (British) adoption of the Gregorian calendar from the previously used Julian calendar. This has been documented in the man pages for Sun Solaris as follows. "An unusual calendar is printed for September 1752. That is the month 11 days were skipped to make up for lack of leap year adjustments." The Plan 9 from Bell Labs manual states: "Try cal sep 1752."


[edit] History
The cal command was present in 1st Edition Unix.


[edit] See also
List of Unix programs
Cron process for scheduling jobs to run on a particular date.

[edit] References
This article does not cite any references or sources. Please help improve this article by adding citations to reliable sources (ideally, using inline citations). Unsourced material may be challenged and removed. (July 2008)


[edit] External links
cal: print a calendar – Commands & Utilities Reference, The Single UNIX&reg; Specification, Issue 7 from The Open Group
NetBSD manual page for cal
Source of explanation of cal 9 1752 phenomena (humor)

使用道具 举报

回复
论坛徽章:
519
奥运会纪念徽章:垒球
日期:2008-09-15 01:28:12生肖徽章2007版:鸡
日期:2008-11-17 23:40:58生肖徽章2007版:马
日期:2008-11-18 05:09:48数据库板块每日发贴之星
日期:2008-11-29 01:01:02数据库板块每日发贴之星
日期:2008-12-05 01:01:03生肖徽章2007版:虎
日期:2008-12-10 07:47:462009新春纪念徽章
日期:2009-01-04 14:52:28数据库板块每日发贴之星
日期:2009-02-08 01:01:03生肖徽章2007版:蛇
日期:2009-03-09 22:18:532009日食纪念
日期:2009-07-22 09:30:00
4#
发表于 2009-4-23 00:47 | 只看该作者


  1. 谁叫我是吃饱了撑的呢?

  2. VAR p_year NUMBER;
  3. EXEC :p_year := 1992;


  4. WITH d AS(
  5.      SELECT TO_CHAR(dt,'MM') mm
  6.            ,LPAD(NVL(MAX(DECODE(TO_CHAR(dt,'d'),'1',TO_CHAR(dt,'fmdd'))),' '),3)
  7.           ||LPAD(NVL(MAX(DECODE(TO_CHAR(dt,'d'),'2',TO_CHAR(dt,'fmdd'))),' '),3)
  8.           ||LPAD(NVL(MAX(DECODE(TO_CHAR(dt,'d'),'3',TO_CHAR(dt,'fmdd'))),' '),3)
  9.           ||LPAD(NVL(MAX(DECODE(TO_CHAR(dt,'d'),'4',TO_CHAR(dt,'fmdd'))),' '),3)
  10.           ||LPAD(NVL(MAX(DECODE(TO_CHAR(dt,'d'),'5',TO_CHAR(dt,'fmdd'))),' '),3)
  11.           ||LPAD(NVL(MAX(DECODE(TO_CHAR(dt,'d'),'6',TO_CHAR(dt,'fmdd'))),' '),3)
  12.           ||LPAD(NVL(MAX(DECODE(TO_CHAR(dt,'d'),'7',TO_CHAR(dt,'fmdd'))),' '),3) AS days
  13.            ,DENSE_RANK() OVER(PARTITION BY TO_CHAR(dt,'MM') ORDER BY MIN(dt)) num_of_week
  14.       FROM (SELECT TO_DATE(:p_year||'0101','YYYYMMDD')+ROWNUM-1 dt
  15.               FROM DUAL
  16.             CONNECT BY ROWNUM <= 366
  17.            )
  18.      WHERE TO_CHAR(dt,'YYYY')=:p_year
  19.      GROUP BY TO_CHAR(dt,'MM'), TO_CHAR( dt+1, 'iw' )
  20.      )
  21.    ,cal AS (
  22.     SELECT * FROM d
  23.     UNION ALL SELECT mm,LPAD(' ',21) AS days,  5 AS num_of_week FROM d GROUP BY mm HAVING MAX(num_of_week)<5
  24.     UNION ALL SELECT mm,LPAD(' ',21) AS days,  6 AS num_of_week FROM d GROUP BY mm HAVING MAX(num_of_week)<6
  25.     )
  26. SELECT SUBSTR(days,1,70)
  27.   FROM (SELECT REPLACE(SYS_CONNECT_BY_PATH(days,'**'),'*',' ') AS DAYS,mm,num_of_week
  28.           FROM cal
  29.          WHERE MM IN ('03','06','09','12')  --- WHERE CONNECT_BY_ISLEAF=1 --修改为适合9i版本
  30.          START WITH MM IN ('01','04','07','10')
  31.          CONNECT BY TO_NUMBER(mm)=TO_NUMBER(prior mm)+1 AND num_of_week = prior num_of_week AND MOD(prior mm,3)<>0
  32.         UNION ALL SELECT '       January                February                March        ','03',-1 FROM DUAL
  33.         UNION ALL SELECT '        April                   May                    June        ','06',-1 FROM DUAL
  34.         UNION ALL SELECT '         July                  August               September      ','09',-1 FROM DUAL
  35.         UNION ALL SELECT '       October                November               December      ','12',-1 FROM DUAL
  36.         UNION ALL SELECT '   S  M  Tu  W Th F  S    S  M  Tu  W Th F  S    S  M  Tu  W Th F  S','03',0  FROM DUAL
  37.         UNION ALL SELECT '   S  M  Tu  W Th F  S    S  M  Tu  W Th F  S    S  M  Tu  W Th F  S','06',0  FROM DUAL
  38.         UNION ALL SELECT '   S  M  Tu  W Th F  S    S  M  Tu  W Th F  S    S  M  Tu  W Th F  S','09',0  FROM DUAL
  39.         UNION ALL SELECT '   S  M  Tu  W Th F  S    S  M  Tu  W Th F  S    S  M  Tu  W Th F  S','12',0  FROM DUAL
  40.         ORDER BY 2,3
  41.       )
  42. ;


  43. -------------------------------------------------------------------------
  44.       January                February                March
  45.   S  M  Tu  W Th F  S    S  M  Tu  W Th F  S    S  M  Tu  W Th F  S
  46.             1  2  3  4                      1    1  2  3  4  5  6  7
  47.    5  6  7  8  9 10 11    2  3  4  5  6  7  8    8  9 10 11 12 13 14
  48.   12 13 14 15 16 17 18    9 10 11 12 13 14 15   15 16 17 18 19 20 21
  49.   19 20 21 22 23 24 25   16 17 18 19 20 21 22   22 23 24 25 26 27 28
  50.   26 27 28 29 30 31      23 24 25 26 27 28 29   29 30 31

  51.        April                   May                    June
  52.   S  M  Tu  W Th F  S    S  M  Tu  W Th F  S    S  M  Tu  W Th F  S
  53.             1  2  3  4                   1  2       1  2  3  4  5  6
  54.    5  6  7  8  9 10 11    3  4  5  6  7  8  9    7  8  9 10 11 12 13
  55.   12 13 14 15 16 17 18   10 11 12 13 14 15 16   14 15 16 17 18 19 20
  56.   19 20 21 22 23 24 25   17 18 19 20 21 22 23   21 22 23 24 25 26 27
  57.   26 27 28 29 30         24 25 26 27 28 29 30   28 29 30
  58.                          31
  59.         July                  August               September
  60.   S  M  Tu  W Th F  S    S  M  Tu  W Th F  S    S  M  Tu  W Th F  S
  61.             1  2  3  4                      1          1  2  3  4  5
  62.    5  6  7  8  9 10 11    2  3  4  5  6  7  8    6  7  8  9 10 11 12
  63.   12 13 14 15 16 17 18    9 10 11 12 13 14 15   13 14 15 16 17 18 19
  64.   19 20 21 22 23 24 25   16 17 18 19 20 21 22   20 21 22 23 24 25 26
  65.   26 27 28 29 30 31      23 24 25 26 27 28 29   27 28 29 30
  66.                          30 31
  67.       October                November               December
  68.   S  M  Tu  W Th F  S    S  M  Tu  W Th F  S    S  M  Tu  W Th F  S
  69.                1  2  3    1  2  3  4  5  6  7          1  2  3  4  5
  70.    4  5  6  7  8  9 10    8  9 10 11 12 13 14    6  7  8  9 10 11 12
  71.   11 12 13 14 15 16 17   15 16 17 18 19 20 21   13 14 15 16 17 18 19
  72.   18 19 20 21 22 23 24   22 23 24 25 26 27 28   20 21 22 23 24 25 26
  73.   25 26 27 28 29 30 31   29 30                  27 28 29 30 31
复制代码



[ 本帖最后由 newkid 于 2009-4-23 01:26 编辑 ]

使用道具 举报

回复
论坛徽章:
15
授权会员
日期:2009-04-13 17:01:142010新春纪念徽章
日期:2010-01-04 08:33:082010新春纪念徽章
日期:2010-03-01 11:04:59ITPUB元老
日期:2010-11-30 19:08:482011新春纪念徽章
日期:2011-01-04 10:24:02生肖徽章2007版:兔
日期:2011-01-20 12:58:49ITPUB十周年纪念徽章
日期:2011-11-01 16:24:04
5#
发表于 2009-4-23 00:59 | 只看该作者
9i 下执行报错。
10G 成功执行了。
对 newkid 的仰慕犹如滔滔江水。。。。

[ 本帖最后由 lnwxzyp 于 2009-4-23 01:24 编辑 ]

使用道具 举报

回复
论坛徽章:
519
奥运会纪念徽章:垒球
日期:2008-09-15 01:28:12生肖徽章2007版:鸡
日期:2008-11-17 23:40:58生肖徽章2007版:马
日期:2008-11-18 05:09:48数据库板块每日发贴之星
日期:2008-11-29 01:01:02数据库板块每日发贴之星
日期:2008-12-05 01:01:03生肖徽章2007版:虎
日期:2008-12-10 07:47:462009新春纪念徽章
日期:2009-01-04 14:52:28数据库板块每日发贴之星
日期:2009-02-08 01:01:03生肖徽章2007版:蛇
日期:2009-03-09 22:18:532009日食纪念
日期:2009-07-22 09:30:00
6#
发表于 2009-4-23 01:26 | 只看该作者
原来用了个CONNECT_BY_ISLEAF是9i不支持的,已经改过来了。

使用道具 举报

回复
论坛徽章:
519
奥运会纪念徽章:垒球
日期:2008-09-15 01:28:12生肖徽章2007版:鸡
日期:2008-11-17 23:40:58生肖徽章2007版:马
日期:2008-11-18 05:09:48数据库板块每日发贴之星
日期:2008-11-29 01:01:02数据库板块每日发贴之星
日期:2008-12-05 01:01:03生肖徽章2007版:虎
日期:2008-12-10 07:47:462009新春纪念徽章
日期:2009-01-04 14:52:28数据库板块每日发贴之星
日期:2009-02-08 01:01:03生肖徽章2007版:蛇
日期:2009-03-09 22:18:532009日食纪念
日期:2009-07-22 09:30:00
7#
发表于 2009-4-23 01:36 | 只看该作者

  1. 另一写法:利用TO_CHAR输出月份名称

  2. WITH d AS(
  3.      SELECT TO_CHAR(dt,'MM') mm
  4.            ,LPAD(NVL(MAX(DECODE(TO_CHAR(dt,'d'),'1',TO_CHAR(dt,'fmdd'))),' '),3)
  5.           ||LPAD(NVL(MAX(DECODE(TO_CHAR(dt,'d'),'2',TO_CHAR(dt,'fmdd'))),' '),3)
  6.           ||LPAD(NVL(MAX(DECODE(TO_CHAR(dt,'d'),'3',TO_CHAR(dt,'fmdd'))),' '),3)
  7.           ||LPAD(NVL(MAX(DECODE(TO_CHAR(dt,'d'),'4',TO_CHAR(dt,'fmdd'))),' '),3)
  8.           ||LPAD(NVL(MAX(DECODE(TO_CHAR(dt,'d'),'5',TO_CHAR(dt,'fmdd'))),' '),3)
  9.           ||LPAD(NVL(MAX(DECODE(TO_CHAR(dt,'d'),'6',TO_CHAR(dt,'fmdd'))),' '),3)
  10.           ||LPAD(NVL(MAX(DECODE(TO_CHAR(dt,'d'),'7',TO_CHAR(dt,'fmdd'))),' '),3) AS days
  11.            ,DENSE_RANK() OVER(PARTITION BY TO_CHAR(dt,'MM') ORDER BY MIN(dt)) num_of_week
  12.            ,TO_CHAR(MIN(dt),'Month') AS mon
  13.       FROM (SELECT TO_DATE(:p_year||'0101','YYYYMMDD')+ROWNUM-1 dt
  14.               FROM DUAL
  15.             CONNECT BY ROWNUM <= 366
  16.            )
  17.      WHERE TO_CHAR(dt,'YYYY')=:p_year
  18.      GROUP BY TO_CHAR(dt,'MM'), TO_CHAR( dt+1, 'iw' )
  19.      )
  20.    ,cal AS (
  21.     SELECT mm,days,num_of_week FROM d
  22.     UNION ALL SELECT mm,LPAD(' ',21) AS days,  5 AS num_of_week FROM d GROUP BY mm HAVING MAX(num_of_week)<5
  23.     UNION ALL SELECT mm,LPAD(' ',21) AS days,  6 AS num_of_week FROM d GROUP BY mm HAVING MAX(num_of_week)<6
  24.     UNION ALL SELECT mm,RPAD(MIN(mon),21),-1 FROM d GROUP BY mm
  25.     UNION ALL SELECT mm,' S  M  Tu  W Th F  S ',0 FROM d GROUP BY mm
  26.     )
  27.    SELECT REPLACE(SYS_CONNECT_BY_PATH(days,'**'),'*',' ') AS DAYS
  28.           FROM cal
  29.          WHERE MM IN ('03','06','09','12')
  30.          START WITH MM IN ('01','04','07','10')
  31.          CONNECT BY TO_NUMBER(mm)=TO_NUMBER(prior mm)+1 AND num_of_week = prior num_of_week AND MOD(prior mm,3)<>0
  32.         ORDER BY mm,num_of_week
  33. ;
复制代码

使用道具 举报

回复
论坛徽章:
55
ITPUB元老
日期:2009-12-05 20:26:01
8#
发表于 2009-4-23 07:55 | 只看该作者
如果同时能显示阴历就更好了!



不把newkid难为住,誓不罢休!

嘻嘻

使用道具 举报

回复
论坛徽章:
33
劳斯莱斯
日期:2013-08-08 14:01:23三菱
日期:2013-09-28 10:16:06一汽
日期:2013-11-19 17:01:11凯迪拉克
日期:2013-12-07 17:11:282014年新春福章
日期:2014-02-18 16:42:02马上有房
日期:2014-02-18 16:42:02itpub13周年纪念徽章
日期:2014-09-27 14:20:21itpub13周年纪念徽章
日期:2014-10-08 15:13:38懒羊羊
日期:2015-03-04 14:52:112015年新春福章
日期:2015-03-06 11:58:18
9#
发表于 2009-4-23 08:12 | 只看该作者
NEWKID实在是高

使用道具 举报

回复
论坛徽章:
69
生肖徽章2007版:羊
日期:2008-11-14 14:42:19复活蛋
日期:2011-08-06 08:59:05ITPUB十周年纪念徽章
日期:2011-11-01 16:19:412012新春纪念徽章
日期:2012-01-04 11:49:542012新春纪念徽章
日期:2012-02-13 15:13:202012新春纪念徽章
日期:2012-02-13 15:13:202012新春纪念徽章
日期:2012-02-13 15:13:202012新春纪念徽章
日期:2012-02-13 15:13:202012新春纪念徽章
日期:2012-02-13 15:13:20版主4段
日期:2012-05-15 15:24:11
10#
发表于 2009-4-23 12:21 | 只看该作者
原帖由 wanglina 于 2009-4-23 07:55 发表

不把newkid难为住,誓不罢休!

嘻嘻

使用道具 举报

回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

TOP技术积分榜 社区积分榜 徽章 团队 统计 知识索引树 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛
CopyRight 1999-2011 itpub.net All Right Reserved. 北京盛拓优讯信息技术有限公司版权所有 联系我们 未成年人举报专区 
京ICP备16024965号-8  北京市公安局海淀分局网监中心备案编号:11010802021510 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表