나는 다음 코드를 가지고 있지만 @test 테이블과 그 기본 where 절을 두 번 이상 참조하지 않는 것을 선호한다. ROWCOUNT로 리팩토링을 시도했지만 해결할 수는 없다.이 SQL을 리팩터링 할 수 있습니까?
declare @test2 table(ID int, ExpiryDate datetime, userid int, siteid int, Status int)
insert into @test2 (ID, ExpiryDate, userid, siteid, Status)
--not expired/status=40 and not status=40 entries
select 1, '2013-08-16', 1, 1, 40
union
select 2, '2013-08-16', 1, 1, 10
union
--not expired/status=40 and status=40 entries
select 3, '2013-08-16', 2, 2, 40
union
select 4, '2013-08-16', 2, 2, 40
union
--expired/status=40 and not status=40 entries
select 5, '2013-08-13', 3, 3, 40
union
select 6, '2013-08-16', 3, 3, 10
union
--expired/status=40 and status=40 entries
select 7, '2013-08-13', 4, 4, 40
union
select 8, '2013-08-16', 4, 4, 40
union
--not expired/status=40 single entry
select 9, '2013-08-16', 5, 5, 40
union
--expired/status=40 single entry
select 10, '2013-08-13', 6, 6, 40
/* scenarios:
not expired/status=40 and not status=40 entries - sees both - userid = 1
not expired/status=40 and status=40 entries - sees both - userid = 2
expired/status=40 and not status=40 entries - sees both - userid = 3
expired/status=40 and status=40 entries - sees single - userid = 4
not expired/status=40 single entry - sees single - userid = 5
expired/status=40 single entry - sees single - userid = 6
*/
내가이 리팩토링 할 수 있습니다 모르겠어요 :
select *
from @test2
where userid = @userid
and (
ExpiryDate > getdate()
or
not exists(select * from @test2
where userid = @userid
and ExpiryDate > Getdate()
and Status = 40)
)
이
내가 테스트를 위해 사용하고 데이터입니다. 이 아이디어를 향상시키는 방법에 대한 아이디어가있는 분은 정말로 감사하겠습니다.감사
존
초기 데이터를 예상 ... – MrSimpleMind