需要查询才能通过将其中一个字段与不同的值进行比较来获取记录
这是我的表
重复 1表示每日 2表示每周 3表示每月
我需要查询来获取所有通过合并当前日期和时间进入Tele caste并进行远程投射的记录。
谢谢
答案 0 :(得分:1)
你的意思是这样吗?
--case for daily: only need to check times
select
*
from
schedules
where
repeated = 1
--this is to compare if it starts within the next hour, may have bugs not tested
--logic is if start time - 1 hour is less than now but start time is more than now must start within the next hour
and DATEADD(day, -DATEDIFF(day, 0, dateadd(hour,startdate),-1) ), dateadd(hour,startdate),-1) ) < DATEADD(day, -DATEDIFF(day, 0, Now()), Now())
and DATEADD(day, -DATEDIFF(day, 0, startdate), startdate) > DATEADD(day, -DATEDIFF(day, 0, Now()), Now())
union
--case for weekly: is the day of the week the same, if so check times
select
*
from
schedules
where
repeated = 2
and datepart(dw,startdate) = datepart(dw,Now)
and DATEADD(day, -DATEDIFF(day, 0, dateadd(hour,startdate),-1) ), dateadd(hour,startdate),-1) ) < DATEADD(day, -DATEDIFF(day, 0, Now()), Now())
and DATEADD(day, -DATEDIFF(day, 0, startdate), startdate) > DATEADD(day, -DATEDIFF(day, 0, Now()), Now())
union
--case for monthly is the day of the month the same, if so compare times
select
*
from
schedules
where
repeated = 3
and datepart(day,startdate) = datepart(day,Now)
and DATEADD(day, -DATEDIFF(day, 0, dateadd(hour,startdate),-1) ), dateadd(hour,startdate),-1) ) < DATEADD(day, -DATEDIFF(day, 0, Now()), Now())
and DATEADD(day, -DATEDIFF(day, 0, startdate), startdate) > DATEADD(day, -DATEDIFF(day, 0, Now()), Now())
并且是根据月份的日期,周数(例如4)或月份的一周+工作日(例如该月的第二个星期二)每月
编辑:我认为这应该适用于月度周期是每个月的那一天的手段。
上述内容可以修改为使用单独的时间列,只需使用该字段进行所有时间比较,例如:
...
where
repeated = 3
and datepart(day,startdate) = datepart(day,Now)
and DATEADD(day, -DATEDIFF(day, 0, dateadd(hour,starttime),-1) ), dateadd(hour,starttime),-1) ) < DATEADD(day, -DATEDIFF(day, 0, Now()), Now())
and DATEADD(day, -DATEDIFF(day, 0, starttime), starttime) > DATEADD(day, -DATEDIFF(day, 0, Now()), Now())
说明:
我将以人的方式处理这个问题的正常方式是:
这是一个程序性的,基于步骤的解决方案
当我将此视为基于集合的问题时,我需要知道现在的程序集是什么
我被允许添加集合(联合)但我不能逐行考虑每一个..所以我把它分成三组并将它们加在一起:
希望这会有所帮助..对不起,如果我的解释是废话,只是试图帮助:D
答案 1 :(得分:0)
示例:
SELECT column_name
FROM table_name
WHERE column_name IN (value1, value2, value3)
你的意思是什么?
答案 2 :(得分:0)
Declare @Today Datetime
Select @Today = Getdate()
Select s.ProgramId
,s.ProgramName
From schedules As s With (Nolock)
Where (Cast(s.startdate As Datetime) + Cast(s.StartTime As Datetime)) >= @Today
Order By s.repeated
,s.ProgramId