之前我就学过一点SQL语句,我就是用普通的SQL语句慢慢的凑,可是发现很麻烦,我以前也见过别人写下面的那样的代码,不是很明白,一会我好好研究一下,感觉也是要学这样的SQL代码来做出这样的程序。CASE WHEN Period='每天' THEN DATEADD(DAY,1,c.Date_time) WHEN Period='每年' THEN DATEADD(YEAR,1,c.Date_time) WHEN Period='每周' THEN DATEADD(WEEK,1,c.Date_time) WHEN Period='每月' THEN DATEADD(MONTH,1,c.Date_time) END 先行谢过,顺便问一下,上面的那些代码有没有什么名字?是叫SQL函数吗?
我有个想法,像下面这样生成一个临时表,或者做成一个视图,然后从这个视图里选择timediff大于零且最小的一个应该就可以了吧.小弟初学SQL,拙见请勿见笑SELECT ID,Title,Period,date_time,CASE period WHEN '每天' THEN Datediff(second,Dateadd(Day, Datediff(Day, Date_time, GetDate()),Date_time),GetDate()) WHEN '每年' THEN Datediff(second,Dateadd(Year, Datediff(Year, Date_time, GetDate()),Date_time),GetDate()) WHEN '每周' THEN Datediff(second,Dateadd(week, Datediff(week,Date_time, GetDate()),Date_time),GetDate()) WHEN '一次' THEN Datediff(second,Date_time,GetDate()) WHEN '每月' THEN Datediff(second,Dateadd(Month, Datediff(Month,Date_time,GetDate()),Date_time),GetDate()) End AS timediff FROM tb
TO32楼 你的程序,现在我还没有查出问题,可是不是很理解,我只知道最简单的SQL语句,对你用的那个 CASE WHEN TEHN等不是很了解。不过,感觉和那个程序语言的语句似的,我也差不多明白一点了。我问这个问题是我现在正在用ASP.NET做一个日程表,那个是用存储过程来查找的,不知道能不能在存储过程中用上面的程序。不知道楼上的朋友们有知道的没有,指点我一下,十分感谢。还有就是兄弟我通过问这个问题学到了不少的东西,也更加发现自己的无知。我决定抽出时间来好好的学习一下SQL。 不知道大家有没有比较好的书?或是有好的见意。请在百忙之中,发点东西,给兄弟,不胜感谢。 E_MAIL: [email protected] MSN: [email protected]
-------------------------------查询出与当前时间最近的一条记录。 --每年--按年计算 --每月--按月计算 --每周--按周计算 --每天--按天计算 --一次--按录入的时间计算 ------------------------------------------- --说明: --这里返回的 idd是对应的数据库真实的字段ID --这里返回的 ntime 是方便我测试,查看日期是否正确而显示的。---(转换后的日期 ----------------------------------------declare @table table ( id int, rownr nvarchar(100), Date datetime ) insert into @table select 1,'每年','2007-02-01 12:15:08' union all select 2,'每月','2007-02-11 12:14:08' union all select 3,'每周','2008-02-01 10:08:00' union all select 4,'每天','2007-02-01 10:09:08' union all select 5,'一次','2008-11-10 12:06:00' union all select 6,'一次','2008-11-10 12:07:08' declare @nowTime datetime set @nowTime=GETDATE() --当前日期select top 1 * from ( select idd=case when Rownr='每年' then case when DATEADD(Year,DATEDIFF(Year,Date,@nowTime),Date)>@nowTime then id else case when DATEADD(Year,DATEDIFF(Year,Date,@nowTime)+1,Date)>@nowTime then id else 0 end end when Rownr='每月' then case when DATEADD(month,DATEDIFF(month,Date,GETDATE()),Date)>@nowTime then id else 0 end when Rownr='每天' then case when DATEADD(day,DATEDIFF(day,Date,GETDATE()),Date)>@nowTime then id else 0 end when Rownr='每周' then case when DATEADD(week,DATEDIFF(week,Date,GETDATE()),Date)>@nowTime then id else 0 end when Rownr='一次' then case when Date>@nowTime then id else 0 end else id end ,ntime=case when Rownr='每年' then case when DATEADD(Year,DATEDIFF(Year,Date,@nowTime),Date)>@nowTime then DATEADD(Year,DATEDIFF(Year,Date,@nowTime),Date) else case when DATEADD(Year,DATEDIFF(Year,Date,@nowTime)+1,Date)>@nowTime then DATEADD(Year,DATEDIFF(Year,Date,@nowTime)+1,Date) else '' end end when Rownr='每月' then case when DATEADD(month,DATEDIFF(month,Date,GETDATE()),Date)>@nowTime then DATEADD(month,DATEDIFF(month,Date,GETDATE()),Date) else '' end when Rownr='每天' then case when DATEADD(day,DATEDIFF(day,Date,GETDATE()),Date)>@nowTime then DATEADD(day,DATEDIFF(day,Date,GETDATE()),Date) else '' end when Rownr='每周' then case when DATEADD(week,DATEDIFF(week,Date,GETDATE()),Date)>@nowTime then DATEADD(week,DATEDIFF(week,Date,GETDATE()),Date) else '' end when Rownr='一次' then case when Date>@nowTime then Date else '' end else '' end from @table )a where idd<>0 order by ntime ----------win2003+SQL2005-------在查询分析器中测试通过,结果正确 ------------结果-----当前时间 2008-11-10 11:51:00-------------------- --idd ntime --5 2008-11-10 12:06:00.000 --6 2008-11-10 12:07:08.000 --2 2008-11-11 12:14:08.000 --3 2008-11-14 10:08:00.000 --1 2009-02-01 12:15:08.000 --------------------------------------------------我想这个应该是你要的结果。这个要放到,存储过程中,仅仅吧那个定义表,与插入数据删除,查询的时候,@table变成你的表,里面的字段变成你对应的字段就可以了。
你是要用CLR还是在直接在执行sqlcommand? 这个存储过程也非常简单啊CREATE PROCEDURE dbo.date_sort AS BEGIN SET NOCOUNT ON; with L0 as (select 1 as c union all select 1 union all select 1 union all select 1 union all select 1), L1 as (select 1 as c from L0 as a,L0 as b), L2 as (select 1 as c from L1 as a,L1 as b), nums as (select row_number() over(order by c) as n from L2), --上述部分是为了生成一个数字辅助表,共计625行 t1 as (select *, date_late=case when Period='每天' then dateadd(dd,n-1,date_time) when Period='每年' then dateadd(yy,n-1,date_time) when Period='每周' then dateadd(wk,n-1,date_time) when Period='每月' then dateadd(mm,n-1,date_time) else Date_time end from tb,--z这里的tb就是你想要排序的表的名称 nums where n<=365) --笛卡尔积,不用担心会生成625条记录后才筛选365,查询优化器会解决 select * from t1 as d where n=(select min(n) from t1 where id=d.id and getdate()<date_late) order by date_late END GO然后前端调用date_sort就行了 调用存储过程应该是已经有现成的代码了吧,这个也不带参数
DATEADD(DAY,1,c.Date_time)
WHEN Period='每年' THEN
DATEADD(YEAR,1,c.Date_time)
WHEN Period='每周' THEN
DATEADD(WEEK,1,c.Date_time)
WHEN Period='每月' THEN
DATEADD(MONTH,1,c.Date_time) END
先行谢过,顺便问一下,上面的那些代码有没有什么名字?是叫SQL函数吗?
我现在在做一个网上的日程表,在新建日程时,你可以选择周期为“每天,每月,每天,每年,一次,每周”五种情况。然后选择创建日期,写上标题,ID为自增的。当你运行这个日程表时,它在后台就去数据库中查找离当前时间最近的日程,为了到时间弹出提示。所以要从数据库中找出离当前时间最近的那一条记录。另:我用的2005,不过对你说的这个05用CTE,2000用表变量不是很理解,不过这个我一会自己查资料就好了,不用麻烦你了。
把数据一改就不行了,例如把ID=1的,时间改为15:22:22
那么最早的应该是ID=4的(为明天14点),可是还是ID=1(明天15点);
WHEN '每天' THEN Datediff(second,Dateadd(Day, Datediff(Day, Date_time, GetDate()),Date_time),GetDate())
WHEN '每年' THEN Datediff(second,Dateadd(Year, Datediff(Year, Date_time, GetDate()),Date_time),GetDate())
WHEN '每周' THEN Datediff(second,Dateadd(week, Datediff(week,Date_time, GetDate()),Date_time),GetDate())
WHEN '一次' THEN Datediff(second,Date_time,GetDate())
WHEN '每月' THEN Datediff(second,Dateadd(Month, Datediff(Month,Date_time,GetDate()),Date_time),GetDate())
End AS timediff
FROM tb
你的程序,现在我还没有查出问题,可是不是很理解,我只知道最简单的SQL语句,对你用的那个
CASE WHEN TEHN等不是很了解。不过,感觉和那个程序语言的语句似的,我也差不多明白一点了。我问这个问题是我现在正在用ASP.NET做一个日程表,那个是用存储过程来查找的,不知道能不能在存储过程中用上面的程序。不知道楼上的朋友们有知道的没有,指点我一下,十分感谢。还有就是兄弟我通过问这个问题学到了不少的东西,也更加发现自己的无知。我决定抽出时间来好好的学习一下SQL。
不知道大家有没有比较好的书?或是有好的见意。请在百忙之中,发点东西,给兄弟,不胜感谢。
E_MAIL: [email protected]
MSN: [email protected]
--每年--按年计算
--每月--按月计算
--每周--按周计算
--每天--按天计算
--一次--按录入的时间计算
-------------------------------------------
--说明:
--这里返回的 idd是对应的数据库真实的字段ID
--这里返回的 ntime 是方便我测试,查看日期是否正确而显示的。---(转换后的日期
----------------------------------------declare @table table
(
id int,
rownr nvarchar(100),
Date datetime
)
insert into @table
select 1,'每年','2007-02-01 12:15:08' union all
select 2,'每月','2007-02-11 12:14:08' union all
select 3,'每周','2008-02-01 10:08:00' union all
select 4,'每天','2007-02-01 10:09:08' union all
select 5,'一次','2008-11-10 12:06:00' union all
select 6,'一次','2008-11-10 12:07:08' declare @nowTime datetime
set @nowTime=GETDATE() --当前日期select top 1 *
from
(
select
idd=case
when Rownr='每年' then case when DATEADD(Year,DATEDIFF(Year,Date,@nowTime),Date)>@nowTime then id else
case when DATEADD(Year,DATEDIFF(Year,Date,@nowTime)+1,Date)>@nowTime then id else 0 end end
when Rownr='每月' then case when DATEADD(month,DATEDIFF(month,Date,GETDATE()),Date)>@nowTime then id else 0 end
when Rownr='每天' then case when DATEADD(day,DATEDIFF(day,Date,GETDATE()),Date)>@nowTime then id else 0 end
when Rownr='每周' then case when DATEADD(week,DATEDIFF(week,Date,GETDATE()),Date)>@nowTime then id else 0 end
when Rownr='一次' then case when Date>@nowTime then id else 0 end
else id end
,ntime=case
when Rownr='每年' then case when DATEADD(Year,DATEDIFF(Year,Date,@nowTime),Date)>@nowTime then DATEADD(Year,DATEDIFF(Year,Date,@nowTime),Date) else
case when DATEADD(Year,DATEDIFF(Year,Date,@nowTime)+1,Date)>@nowTime then DATEADD(Year,DATEDIFF(Year,Date,@nowTime)+1,Date) else '' end end
when Rownr='每月' then case when DATEADD(month,DATEDIFF(month,Date,GETDATE()),Date)>@nowTime then DATEADD(month,DATEDIFF(month,Date,GETDATE()),Date) else '' end
when Rownr='每天' then case when DATEADD(day,DATEDIFF(day,Date,GETDATE()),Date)>@nowTime then DATEADD(day,DATEDIFF(day,Date,GETDATE()),Date) else '' end
when Rownr='每周' then case when DATEADD(week,DATEDIFF(week,Date,GETDATE()),Date)>@nowTime then DATEADD(week,DATEDIFF(week,Date,GETDATE()),Date) else '' end
when Rownr='一次' then case when Date>@nowTime then Date else '' end
else '' end
from @table
)a where idd<>0
order by ntime ----------win2003+SQL2005-------在查询分析器中测试通过,结果正确
------------结果-----当前时间 2008-11-10 11:51:00--------------------
--idd ntime
--5 2008-11-10 12:06:00.000
--6 2008-11-10 12:07:08.000
--2 2008-11-11 12:14:08.000
--3 2008-11-14 10:08:00.000
--1 2009-02-01 12:15:08.000
--------------------------------------------------我想这个应该是你要的结果。这个要放到,存储过程中,仅仅吧那个定义表,与插入数据删除,查询的时候,@table变成你的表,里面的字段变成你对应的字段就可以了。
輔助表需要365條記錄 我也认为32#的正确,可是现在的问题是:加入到ASP.NET中的存储过程中,不过,如果没人会话,我就尽快结贴。在此之前,我想再次说一下:
还有就是兄弟我通过问这个问题学到了不少的东西,也更加发现自己的无知。我决定抽出时间来好好的学习一下SQL。
不知道大家有没有比较好的书?或是有好的见意。 请在百忙之中,发点东西,给兄弟,不胜感谢。
E_MAIL: [email protected]
MSN: [email protected]
或是在这儿直接告诉我。谢谢
多谢上面的各位大牛的帮助,谢谢你们在百忙这中,帮兄弟解决困难,也感谢所有的热心朋友帮兄弟顶贴,让它不沉下去,特别感谢vc_delphi 不仅为我写出了SQL代码,还在QQ上帮我把存储过程写了出来,十分感谢他。我知道200分不足以感谢大家对我的帮助,但好像是不能再加了。当然这儿的人也不是为了分的,我只能表达自己最诚挚的感谢。另:这个帖子让我知道自己多么无知,我想以后学学这方面的东西。有热心朋友给我出点主意,发点资料,下面是我的联系方式:
E_MAIL: [email protected]
MSN: [email protected]
QQ:328806950(注明是来自这个帖子)
这个存储过程也非常简单啊CREATE PROCEDURE dbo.date_sort
AS
BEGIN
SET NOCOUNT ON;
with
L0 as (select 1 as c union all select 1 union all select 1 union all select 1 union all select 1),
L1 as (select 1 as c from L0 as a,L0 as b),
L2 as (select 1 as c from L1 as a,L1 as b),
nums as (select row_number() over(order by c) as n from L2),
--上述部分是为了生成一个数字辅助表,共计625行
t1 as (select *,
date_late=case when Period='每天' then dateadd(dd,n-1,date_time)
when Period='每年' then dateadd(yy,n-1,date_time)
when Period='每周' then dateadd(wk,n-1,date_time)
when Period='每月' then dateadd(mm,n-1,date_time)
else Date_time end
from tb,--z这里的tb就是你想要排序的表的名称
nums
where n<=365)
--笛卡尔积,不用担心会生成625条记录后才筛选365,查询优化器会解决
select *
from t1 as d where
n=(select min(n) from t1 where id=d.id and getdate()<date_late)
order by date_late
END
GO然后前端调用date_sort就行了
调用存储过程应该是已经有现成的代码了吧,这个也不带参数