查看: 8528|回复: 14

sql实现当月日历

[复制链接]
论坛徽章:
36
2008新春纪念徽章
日期:2008-02-13 12:43:03ITPUB 11周年纪念徽章
日期:2012-10-09 18:03:322013年新春福章
日期:2013-02-25 14:51:24蛋疼蛋
日期:2013-06-05 17:27:07咸鸭蛋
日期:2013-06-30 16:25:20咸鸭蛋
日期:2013-07-15 10:44:25法拉利
日期:2013-09-29 17:53:45比亚迪
日期:2013-11-02 16:11:38保时捷
日期:2013-12-19 14:21:042014年新春福章
日期:2014-02-18 16:41:11
跳转到指定楼层
1#
发表于 2007-7-9 16:53 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

select max(su) su,max(mo) mo,max(tu) tu,max(we) we,max(th) th,max(fr) fr,max(sa) sa from
(select case when d=1 then dd end su,
       case when d=2 then dd end mo,
       case when d=3 then dd end tu,
       case when d=4 then dd end we,
       case when d=5 then dd end th,
       case when d=6 then dd end fr,
       case when d=7 then dd end sa,
       d,w,iw
from(
select dates,to_char(dates,'dd') dd,to_number(to_char(dates,'d')) d,to_char(dates,'w') w,to_char(dates+1,'iw') iw from
(select trunc(sysdate,'month')+level-1 dates from dual
connect by rownum<last_day(sysdate)-trunc(sysdate,'month')
)
)
)
group by iw
order by iw;
[/COLOR]

环境:oracle 10g
论坛徽章:
281
2015年新春福章
日期:2015-03-06 11:57:312012新春纪念徽章
日期:2012-02-13 15:12:252012新春纪念徽章
日期:2012-01-04 11:51:22蛋疼蛋
日期:2011-12-29 07:37:22迷宫蛋
日期:2011-12-26 14:19:41茶鸡蛋
日期:2011-11-17 09:20:52茶鸡蛋
日期:2011-11-10 22:42:38ITPUB十周年纪念徽章
日期:2011-11-01 16:21:15茶鸡蛋
日期:2011-10-24 09:48:48ITPUB十周年纪念徽章
日期:2011-09-27 16:30:47
2#
发表于 2007-7-9 17:02 | 只看该作者

Re: sql实现当月日历

最初由 wngsheng 发布
[B]
select max(su) su,max(mo) mo,max(tu) tu,max(we) we,max(th) th,max(fr) fr,max(sa) sa from
(select case when d=1 then dd end su,
       case when d=2 then dd end mo,
       case when d=3 then dd end tu,
       case when d=4 then dd end we,
       case when d=5 then dd end th,
       case when d=6 then dd end fr,
       case when d=7 then dd end sa,
       d,w,iw
from(
select dates,to_char(dates,'dd') dd,to_number(to_char(dates,'d')) d,to_char(dates,'w') w,to_char(dates+1,'iw') iw from
(select trunc(sysdate,'month')+level-1 dates from dual
connect by rownum<last_day(sysdate)-trunc(sysdate,'month')
)
)
)
group by iw
order by iw;
[/COLOR]

环境:oracle 10g [/B]


不错,不过w和iw留1个即可

使用道具 举报

回复
论坛徽章:
24
生肖徽章:狗
日期:2006-09-07 10:14:43数据库板块每日发贴之星
日期:2008-07-26 01:02:20生肖徽章2007版:兔
日期:2008-10-13 11:10:11奥运会纪念徽章:铁人三项
日期:2008-10-24 13:27:21开发板块每日发贴之星
日期:2008-12-27 01:01:09生肖徽章2007版:马
日期:2009-11-18 10:45:032010新春纪念徽章
日期:2010-03-01 11:21:02ITPUB9周年纪念徽章
日期:2010-10-08 09:28:51ERP板块每日发贴之星
日期:2011-05-18 01:01:01ITPUB十周年纪念徽章
日期:2011-11-01 16:21:15
3#
发表于 2007-7-9 17:03 | 只看该作者
不错,不过都有查出一年的了!

使用道具 举报

回复
论坛徽章:
123
ITPUB新首页上线纪念徽章
日期:2007-10-20 08:38:44ITPUB 11周年纪念徽章
日期:2012-10-09 18:08:15蛋疼蛋
日期:2012-11-04 21:58:54灰彻蛋
日期:2012-11-29 21:08:172013年新春福章
日期:2013-02-25 14:51:242014年新春福章
日期:2014-02-18 16:42:02马上有房
日期:2014-02-18 16:42:02马上有车
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14
4#
发表于 2007-7-9 21:57 | 只看该作者

使用道具 举报

回复
论坛徽章:
1
ITPUB新首页上线纪念徽章
日期:2007-10-20 08:38:44
5#
发表于 2007-7-12 15:46 | 只看该作者

Re: sql实现当月日历

最初由 wngsheng 发布
[B]
select max(su) su,max(mo) mo,max(tu) tu,max(we) we,max(th) th,max(fr) fr,max(sa) sa from
(select case when d=1 then dd end su,
       case when d=2 then dd end mo,
       case when d=3 then dd end tu,
       case when d=4 then dd end we,
       case when d=5 then dd end th,
       case when d=6 then dd end fr,
       case when d=7 then dd end sa,
       d,w,iw
from(
select dates,to_char(dates,'dd') dd,to_number(to_char(dates,'d')) d,to_char(dates,'w') w,to_char(dates+1,'iw') iw from
(select trunc(sysdate,'month')+level-1 dates from dual
connect by rownum<last_day(sysdate)-trunc(sysdate,'month')
)
)
)
group by iw
order by iw;
[/COLOR]

环境:oracle 10g [/B]


代码格式缩进一下,看看清楚都好啊

使用道具 举报

回复
论坛徽章:
10
授权会员
日期:2007-07-18 11:34:32ITPUB新首页上线纪念徽章
日期:2007-10-20 08:38:44生肖徽章2007版:鸡
日期:2008-12-02 12:45:45生肖徽章2007版:龙
日期:2008-12-07 18:27:322009新春纪念徽章
日期:2009-01-04 14:52:28生肖徽章2007版:猪
日期:2009-02-18 12:56:472009日食纪念
日期:2009-07-22 09:30:00
6#
发表于 2007-7-12 17:27 | 只看该作者
非常好。以前也想写,没写出来。

稍微纠正一下。 最后那个要 + 1
LAST_DAY (SYSDATE)- TRUNC (SYSDATE, 'month') + 1

使用道具 举报

回复
论坛徽章:
468
7#
发表于 2007-7-12 17:33 | 只看该作者
9i下也适用..

使用道具 举报

回复
论坛徽章:
0
8#
发表于 2007-7-12 17:36 | 只看该作者
创意不错,楼主真牛。

使用道具 举报

回复
论坛徽章:
66
现任管理团队成员
日期:2011-05-07 01:45:08版主9段
日期:2013-04-21 02:21:02ITPUB年度最佳版主
日期:2014-02-19 10:05:27ITPUB年度最佳版主
日期:2013-01-30 17:30:25ITPUB年度最佳技术原创精华奖
日期:2012-03-13 17:12:05优秀写手
日期:2013-12-18 09:29:15元宝章
日期:2015-02-10 19:57:54金牌徽章
日期:2015-02-10 19:59:42银牌徽章
日期:2015-02-10 19:59:42铜牌徽章
日期:2015-02-10 19:59:41
9#
发表于 2007-7-12 17:58 | 只看该作者
牛尹~~~~

使用道具 举报

回复
论坛徽章:
0
10#
发表于 2007-7-12 18:14 | 只看该作者

2007年8月 9月显示问题

select max(su) su,max(mo) mo,max(tu) tu,max(we) we,max(th) th,max(fr) fr,max(sa) sa from
(select case when d=1 then dd end su,
case when d=2 then dd end mo,
case when d=3 then dd end tu,
case when d=4 then dd end we,
case when d=5 then dd end th,
case when d=6 then dd end fr,
case when d=7 then dd end sa,
d,w
from(
select dates,to_char(dates,'dd') dd,to_number(to_char(dates,'d')) d,to_char(dates,'w') w,to_char(dates+1,'iw') iw from
(select trunc(sysdate,'month')+level-1 dates from dual
connect by rownum<last_day(sysdate)-trunc(sysdate,'month')+1
)
)
)
group by w
order by w;

//8月
SU MO TU WE TH FR SA
-- -- -- -- -- -- --
05 06 07 01 02 03 04
12 13 14 08 09 10 11
19 20 21 15 16 17 18
26 27 28 22 23 24 25
          29 30 31

//9月

SU MO TU WE TH FR SA
-- -- -- -- -- -- --
02 03 04 05 06 07 01
09 10 11 12 13 14 08
16 17 18 19 20 21 15
23 24 25 26 27 28 22
30                          29

使用道具 举报

回复

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

本版积分规则 发表回复

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