2
나는이 스키마와 테이블이 있습니다도움말
DeviceID RoomID DateInstalled
0001 Room1 1/1/2000
0002 Room1 1/1/2000
0001 Room2 2/1/2000
I :
의 DeviceID INT
RoomID INT
DateInstalled 날짜를 테이블의 데이터의 샘플처럼 보이는
각 장치가 특정 회의실에 있었던 날짜 범위를 알려주는 쿼리를 작성해야합니다. 어떤 도움을 이해할 수있을 것이다
DeviceID RoomID From Date To Date
0001 Room1 1/1/2000 1/31/2000
0001 Room2 2/1/2000 NULL
0002 Room1 1/1/2000 NULL
,
감사 :처럼 뭔가.
select a.DeviceID, a.RoomID, a.DateInstalled as fromDate,
ISNULL((select DATEADD(day,-1,MIN(DateInstalled)) from myTable
where DeviceID = a.DeviceID
and RoomID <> a.RoomID
and DateInstalled > a.DateInstalled),'') as toDate
from myTable a
당신이 테이블 구조에 대한 제어가 있습니까 : – AllenG