2014-02-05 4 views
1

저장 프로 시저에서 테이블을 결합하는 데 문제가 있습니다.
참고 : 필드 "시간은"
SQL 다중 레코드 : 시간 스케줄러

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 - 
같은 조건이 일정에 결과를 원하는 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)입니다

읽어 주셔서 감사합니다.^_^

,

GBU는

I`ve이 무엇 당신이해야 할 것은 라운드 가장 가까운 30 분 간격 다운 c.time입니다

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 - 
+0

무엇이 질문입니까? – Miller

+0

어떤 DBMS를 사용하고 있습니까? – gvee

답변

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 
+0

도움 주셔서 감사합니다. .... 그게 .... – Davis

1

시도 가장 가까운 간격까지 반올림 c.until. 이렇게하면 where 절에 올바른 범위가 적용됩니다.

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

는 그런 다음과 같이 가장 가까운 30 분 내림 할 수 있습니다 :

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

은 당신이 당신과 같이 할 수 datetime에 시간을 변환해야합니다 반올림 수행

다음과 같이 반올림하십시오.

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

기존 코드 w에 모두 적용하십시오. ould는 다음을 제공합니다 :

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] 
+0

답변 주셔서 고마워요. 내 질문에 ..^_^ – Davis

관련 문제