view、udf、triggerに書き込むことができます。これはさまざまな実装方法があります。誰かがあなたに最適なものをアドバイスするためには、状況についてより多くのコンテキストを提供する必要があるかもしれません。
にかかわらず、あなたがそれを行う方法の、ロジックは次のようになります。
declare @example table
(
creationdate datetime
, slahours int
)
insert into @example
values ('2017-09-01 12:30', 9)
, ('2017-09-01 16:00', 12)
declare @hrs_in_day int = 8
, @end_time time = '17:00'
select b.creationdate
, b.slahours
, dateadd(hh, b.hr_cnt, dateadd(d, b.day_cnt, b.creationdate)) as completebydate
from (
select a.creationdate
, a.slahours
, a.day_cnt + case when a.hr_cnt > a.first_day_remaining_hours then 1 else 0 end as day_cnt
, a.hr_cnt + case when a.hr_cnt > a.first_day_remaining_hours then -1 * @hrs_in_day else 0 end as hr_cnt
from (
select e.creationdate
, e.slahours
, datediff(mi, cast(e.creationdate as time), @end_time)/60.0 as first_day_remaining_hours
, floor(e.slahours/@hrs_in_day) as day_cnt
, e.slahours - (floor(e.slahours/@hrs_in_day) * @hrs_in_day) as hr_cnt
from @example as e
) as a
) as b
最初のサブ問合せは、単に理解したコードが容易になり、必要ありません。
出力:
creationdate slahours completebydate
2017-09-01 12:30:00.000 9 2017-09-02 13:30:00.000
2017-09-01 16:00:00.000 12 2017-09-03 12:00:00.000
限り別の質問を保証するかもしれませんが、私も同じコンセプトの周りexisting questionsを見てみましょう週末/祝日の問題、など。
1430ではありませんか?だからcreationdateのために、あなたは基本的にdateadd関数を実行するときに常に0900にしますか? SLAhoursが15の場合はどうなりますか?あなたはロールオーバーをどのように扱いますか? – scsimon
勤務時間はどのように分かりますか?週末と祝日はこれを少し複雑にします。 – jarlh
私は[UDF](https://docs.microsoft.com/en-us/sql/relational-databases/user-defined-functions/create-user-defined-functions-database-engine)ルートに行きます。あなたが休日などを追跡する必要があるならば、非常に複雑になることもあります(前述のように)。私はちょうどアプリケーション自体で終了日を計算し、計算された日付をSQLに格納します。 – RobIII