日期列转换成年月树型结构(有点难度)

txt_ly 2007-12-25 03:45:59
表SK_Stork有一日期列InDate,数据如下所示
2007-12-25
2007-12-24(本周星期一)
2007-11-12
2006-10-1
2005-9-1
.....
问题一,现在要构成一个年月树型结构
结果如下:
2007年
12月
11月
2006年
10月
9月
问题二:根据今天的日期(2007-12-25)查询日期列InDate(条件:属于本周且小于今天的日期)形成本周星期数显示
结果如下:
星期一


哪们大大有好的解决方法??
...全文
254 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
chuifengde 2007-12-25
  • 打赏
  • 举报
回复
declare @a table(InDate   datetime   not   null )
insert @a select '2007-12-25'
insert @a select '2007-12-24'
insert @a select '2007-11-12'
insert @a select '2006-10-1'
insert @a select '2005-9-1'

select a,b from (
select a=case when b is null then a+'年' else '' end,
b=case when b is null then '' else b+'月' end,
c=a+case when b is null then '90' else b end from(
select ltrim(year(indate)) a,right('0'+ltrim(month(indate)),2) b
from @a
group by ltrim(year(indate)),right('0'+ltrim(month(indate)),2)
)bb group by a,b with rollup
)cc where a is not null order by c desc

--result
/*a b
-------------- ------
2007年
12月
11月
2006年
10月
2005年
09月

*/
txt_ly 2007-12-25
  • 打赏
  • 举报
回复
高!!!
pt1314917 2007-12-25
  • 打赏
  • 举报
回复

哦换这种:
declare @t table (InDate datetime)
insert into @t select '2007-12-25'
insert into @t select '2007-12-24'
insert into @t select '2007-11-12'
insert into @t select '2006-10-1'
insert into @t select '2005-9-1'


declare @b table(id int identity(1,1),writer varchar(50),parentid varchar(10),lev int)
insert into @b select distinct datepart(yy,indate),'',1 from @t order by datepart(yy,indate) desc

insert into @b select distinct datepart(mm,a.indate),b.id,2 from @t a,@b b
where b.writer=datepart(yy,a.indate)
order by datepart(mm,a.indate) desc

select * from @b

pt1314917 2007-12-25
  • 打赏
  • 举报
回复
上面得出树结构。在程序中可以转换一下显示。。
txt_ly 2007-12-25
  • 打赏
  • 举报
回复
问题二已经被pt1314917 大佬搞定了,
第一个问题是不是可以这样做,根据年,月分组,
组成这种视图
父 , 子
0 2007
2007 12
2007 11
0 2006
2006 10
然后我到程序中用递归实现!
pt1314917 2007-12-25
  • 打赏
  • 举报
回复

1。declare @t table (InDate datetime)
insert into @t select '2007-12-25'
insert into @t select '2007-12-24'
insert into @t select '2007-11-12'
insert into @t select '2006-10-1'
insert into @t select '2005-9-1'


declare @b table(writer varchar(50),parentid varchar(10),lev int)
insert into @b select distinct datepart(yy,indate),'',1 from @t order by datepart(yy,indate) desc
insert into @b select distinct datepart(mm,a.indate),b.writer,2 from @t a,@b b
where b.writer=datepart(yy,a.indate)
order by datepart(mm,a.indate) desc

select * from @b




dawugui 2007-12-25
  • 打赏
  • 举报
回复
--问题1
create table tb(InDate datetime not null )
go
insert tb select '2007-12-25'
insert tb select '2007-12-24'
insert tb select '2007-11-12'
insert tb select '2006-10-1'
insert tb select '2005-9-1'
go

select yy = case when mm = (select min(mm) from
(
select distinct datename(yy,indate) + '年' yy , mm = '' from tb
union all
select distinct datename(yy,indate) + '年' yy , datename(mm,indate) + '月' mm from tb
) t
where t.yy = m.yy) then yy else '' end,
mm
from
(
select distinct datename(yy,indate) + '年' yy , mm = '' from tb
union all
select distinct datename(yy,indate) + '年' yy , datename(mm,indate) + '月' mm from tb
) m

drop table tb

/*
yy mm
-------------------------------- --------------------------------
2005年
09月
2006年
10月
2007年
11月
12月

(7 行受影响)
*/
txt_ly 2007-12-25
  • 打赏
  • 举报
回复
楼上的大大,强!加上这一句and DATEPART(yy, indate) = DATEPART(yy, GETDATE()) 就更完美了
pt1314917 2007-12-25
  • 打赏
  • 举报
回复

2。。

declare @t table (InDate datetime)
insert into @t select '2007-12-25'
insert into @t select '2007-12-24'
insert into @t select '2007-11-12'
insert into @t select '2006-10-1'
insert into @t select '2005-9-1'


declare @date datetime
set @date=getdate()
select case datepart(dw,indate)
when '1' then '星期日'
when '2' then '星期一'
when '3' then '星期二'
when '4' then '星期三'
when '5' then '星期四'
when '6' then '星期五'
when '7' then '星期六' end xq
from @t where datepart(ww,indate)=datepart(ww,@date) and datediff(dd,indate,@date)>=1

txt_ly 2007-12-25
  • 打赏
  • 举报
回复
建表
create table SK_Stork
(
InDate datetime not null
)
go



insert SK_Stork select '2007-12-25'
insert SK_Stork select '2007-12-24'
insert SK_Stork select '2007-11-12'
insert SK_Stork select '2006-10-1'
insert SK_Stork select '2005-9-1'
txt_ly 2007-12-25
  • 打赏
  • 举报
回复
不好意思,结果应该如下
2007年
12月
11月
2006年
10月
2005年
9月

34,587

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧