【问题标题】:SQL Multiple record : Time SchedulerSQL 多条记录:时间调度程序
【发布时间】:2014-02-05 11:49:58
【问题描述】:

我在存储过程中合并表时遇到问题。
注意:字段“时间”是 varchar
第一个表(tbTime)

Time
08:00:00
08:30:00
09:00:00
09:30:00
10:00:00
10:30:00
11:00:00
11:30:00
12:00:00
12:30:00
13:00:00
13:30:00
14:00:00
14:30:00
15:00:00
15:30:00
16:00:00
16:30:00
17:00:00
17:30:00
18:00:00
18:30:00
19:00:00
19:30:00
20:00:00

第二个表(tbClassRsv)

select * from tbclassrsv where transdate='2014-02-05 00:00:00' and status<>'DEL'

transDate    time       until    status  studentCode  tutor   class    description  userID
2014-02-05   16:48:14   17:48:14 OPN     ET-7201      ET-444  ROOM 01  try          ADMIN

我想要这样的条件调度结果

Time       Student
08:00:00   -
08:30:00   -
09:00:00   -
09:30:00   -
10:00:00   -
10:30:00   -
11:00:00   -
11:30:00   -
12:00:00   -
12:30:00   -
13:00:00   -
13:30:00   -
14:00:00   -
14:30:00   -
15:00:00   -
15:30:00   -
16:00:00   -
16:30:00   ET-7201
17:00:00   ET-7201
17:30:00   ET-7201
18:00:00   ET-7201
18:30:00   -
19:00:00   -
19:30:00   -
20:00:00   -

感谢阅读或回答^_^

GBU

我试过了

select t.time, 
isnull(
(select c.studentCode 
from tbclassrsv c 
where c.transdate='2014-02-05 00:00:00' 
and c.class='ROOM 01'
and c.status<>'DEL' 
and t.time>=c.time 
and t.time<=c.until
),'-') [Student] 

结果是……

Time       Student
08:00:00   -
08:30:00   -
09:00:00   -
09:30:00   -
10:00:00   -
10:30:00   -
11:00:00   -
11:30:00   -
12:00:00   -
12:30:00   -
13:00:00   -
13:30:00   -
14:00:00   -
14:30:00   -
15:00:00   -
15:30:00   -
16:00:00   -
16:30:00   -
17:00:00   ET-7201
17:30:00   ET-7201
18:00:00   -
18:30:00   -
19:00:00   -
19:30:00   -
20:00:00   -

【问题讨论】:

  • 问题是什么?
  • 您使用的是什么 DBMS?

标签: sql scheduler multiple-records


【解决方案1】:

试试这个。您没有将 varchar 时间转换为 datetime 以便您的时间比较有效。

select t.time, 
isnull(
(select c.studentCode 
from tbClassRsv c 
where c.transdate='2014-02-05 00:00:00' 
and c.class='ROOM 01'
and c.status<>'DEL' 
and DateAdd(MINUTE, 30, Convert(datetime, t.time))>= Convert(datetime, c.time) 
and Convert(datetime, t.time) <= Convert(datetime, c.until)
),'-') from [tbTime] t

【讨论】:

    【解决方案2】:

    您需要将c.time 向下舍入到最近的30 分钟间隔,并将c.until 向上舍入到最近的时间间隔。这样您的 where 子句将具有正确的范围。

    要进行四舍五入,您需要将时间转换为 datetime,您可以这样做:

    CAST(CONVERT(varchar,THE_TIME_AS_VARCHAR,121) AS datetime)
    

    然后你可以像这样向下舍入到最接近的 30 分钟:

    DATEADD(mi, DATEDIFF(mi, 0, THE_TIME_AS_DATETIME)/30*30, 0)
    

    然后像这样四舍五入:

    DATEADD(mi, DATEDIFF(mi, 30, THE_TIME_AS_DATETIME)/30*30, 0)
    

    将所有这些应用到您现有的代码中会得到这样的结果:

    select t.time, 
    isnull(
    (select c.studentCode 
        from tbclassrsv c 
        where c.transdate='2014-02-05 00:00:00' 
        and c.class='ROOM 01'
        and c.status<>'DEL' 
        and t.time>= DATEADD(mi, DATEDIFF(mi, 0, CAST(CONVERT(varchar,c.time,121) AS datetime))/30*30, 0) 
        and t.time<= DATEADD(mi, DATEDIFF(mi, 30, CAST(CONVERT(varchar,c.until,121) AS datetime))/30*30, 0)
    ),'-') [Student] 
    

    【讨论】:

      猜你喜欢
      • 2021-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多