0

次のようなクエリ結果があります

|----------------------------------------------|
|StaffId|BranchId|StartTime|EndTime |PatientId |
|----------------------------------------------|
|1      |1       |09:30:00 |09:35:00|Null      |
|1      |1       |09:35:00 |09:40:00|Null      |
|1      |1       |09:40:00 |09:55:00|1         |
|1      |1       |09:55:00 |10:00:00|Null      |
|1      |1       |10:00:00 |10:05:00|Null      |
|1      |1       |10:05:00 |10:20:00|2         |
|1      |1       |10:20:00 |10:25:00|NULL      |
|1      |1       |10:25:00 |10:40:00|3         |
|1      |1       |10:40:00 |10:45:00|Null      |
|1      |1       |10:45:00 |10:50:00|Null      |
|1      |1       |10:50:00 |10:55:00|Null      |
|1      |1       |10:55:00 |11:00:00|Null      |
|----------------------------------------------|

しかし、すべての未使用のタイムスロット (患者 ID のないもの) をグループ化できるようにしたいので、結果は

|----------------------------------------------|
|StaffId|BranchId|StartTime|EndTime |PatientId |
|----------------------------------------------|
|1      |1       |09:30:00 |09:40:00|Null      |
|1      |1       |09:40:00 |09:55:00|1         |
|1      |1       |09:55:00 |10:05:00|Null      |
|1      |1       |10:05:00 |10:20:00|2         |
|1      |1       |10:20:00 |10:25:00|NULL      |
|1      |1       |10:25:00 |10:40:00|3         |
|1      |1       |10:40:00 |11:00:00|Null      |
|----------------------------------------------|

これを行う方法がよくわかりませんが、ガイダンスをいただければ幸いです。簡単かどうかはわかりませんが、クエリの結果は、SQL を使用して患者と 5 分間隔でテーブルを結合することによって得られます

INSERT INTO @Results(BranchId, StaffId, StartTime, EndTime)
SELECT us.BranchId, us.StaffId, us.StartTime, us.EndTime
FROM @UnusedSlots us
    left join @Results r
        on (NOT ((r.StartTime >= us.EndTime)
            OR (r.EndTime <= us.StartTime))) AND
            (us.StaffId = r.StaffId)
    where r.BranchId is Null

@UnusedSlots は利用可能な 5 分間のスロットで、@Results には予約済みのスロット (患者 ID 付き) が含まれます。このステートメントにマージを組み込む方法があれば、はるかに優れています。

Min(StartTime)を使用できると思っていましMax(EndTime)たが、PatientId なしで隣接する結果を分離できる場合は、その方法がわかりません。09:30 から 11:00 までマージされた結果になり続けます。

4

2 に答える 2

3
select 
    StaffId,
    BranchId,
    MIN(StartTime),
    MAX(EndTime),
    PatientId
from
(
select *,       
    ROW_NUMBER() over (order by starttime) 
      - ROW_NUMBER() over (partition by isnull(PatientID,-1) order by starttime) rn     
from @results
) v
group by staffid, branchid, patientid, rn
order by MIN (starttime)

StaffIDとBranchIDが@resultsテーブルで変化する可能性があるかどうかはわかりません。そのため、可能であれば、これを微調整する必要があるかもしれません。

于 2012-08-23T10:04:39.963 に答える
0

カーソルを使用することもできますが、パフォーマンスはあまりうまくスケーリングしません:

    create table #tempTimes (
        staffid int,
        branchid int,
        startTime datetime,
        endTime datetime,
        patientid int)

    insert into #temptimes values (1,1,'2012-09-09 09:30', '2012-09-09 09:35', null)
    insert into #temptimes values (1,1,'2012-09-09 09:35', '2012-09-09 09:40', null)
    insert into #temptimes values (1,1,'2012-09-09 09:40', '2012-09-09 09:45', null)
    insert into #temptimes values (1,1,'2012-09-09 09:45', '2012-09-09 09:55', 1)
    insert into #temptimes values (1,1,'2012-09-09 09:55', '2012-09-09 10:00', null)
    insert into #temptimes values (1,1,'2012-09-09 10:00', '2012-09-09 10:05', null)
    insert into #temptimes values (1,1,'2012-09-09 10:05', '2012-09-09 10:20', 2)

    declare @currentPeriodStart datetime
    declare @currentPeriodEnd datetime
    declare @startTime datetime
    declare @endTime datetime
    declare @lastEndTime datetime
    declare @patientId int

    declare myCurs cursor for select startTime, endTime, patientid from #tempTimes order by startTime 

    open myCurs

    fetch next from myCurs into @startTime, @endTime, @patientId

    while @@fetch_status = 0
    begin
        if @patientId is null
        begin
            if (@currentPeriodStart is null)
                set @currentPeriodStart = @startTime

            set @currentPeriodEnd = @endTime
        end else
        begin
            print   cast(@currentPeriodStart as nvarchar) + ' to ' + cast(@currentPeriodEnd as nvarchar)
            set @currentPeriodStart = null
        end

        fetch next from myCurs into @startTime, @endTime, @patientId
    end

    close myCurs
    deallocate myCurs
于 2012-08-23T10:16:32.183 に答える