2016-05-19 10 views
1

3か月間に3回以上誰かがいたときにテーブルにデータを読み込むSQLクエリを作成する必要があります。3ストライククエリSQL 2008

私は私に利用可能なデータがある:以下の出力が得られます

SELECT 'John Doe' AS Name, 1406 AS InstanceID, '2016-01-08 00:00:00.000' AS AbsenceStart, '2016-01-13 00:00:00.000' AS AbsenceEnd, 4 AS NoOfDays, 1 AS rnk 
UNION ALL 
SELECT 'John Doe' AS Name, 1493 AS InstanceID, '2016-02-02 00:00:00.000' AS AbsenceStart, '2016-02-05 00:00:00.000' AS AbsenceEnd, 4 AS NoOfDays, 2 AS rnk 
UNION ALL 
SELECT 'John Doe' AS Name, 1536 AS InstanceID, '2016-02-19 00:00:00.000' AS AbsenceStart, '2016-02-22 00:00:00.000' AS AbsenceEnd, 2 AS NoOfDays, 3 AS rnk 
UNION ALL 
SELECT 'John Doe' AS Name, 1547 AS InstanceID, '2016-02-26 00:00:00.000' AS AbsenceStart, '2016-03-10 00:00:00.000' AS AbsenceEnd, 10 AS NoOfDays, 4 AS rnk 
UNION ALL 
SELECT 'John Doe' AS Name, 1660 AS InstanceID, '2016-04-04 00:00:00.000' AS AbsenceStart, '2016-04-04 00:00:00.000' AS AbsenceEnd, 0.5 AS NoOfDays, 5 AS rnk 
UNION ALL 
SELECT 'John Doe' AS Name, 1666 AS InstanceID, '2016-04-07 00:00:00.000' AS AbsenceStart, '2016-04-14 00:00:00.000' AS AbsenceEnd, 6 AS NoOfDays, 6 AS rnk 
UNION ALL 
SELECT 'John Doe' AS Name, 1698 AS InstanceID, '2016-04-27 00:00:00.000' AS AbsenceStart, '2016-04-28 00:00:00.000' AS AbsenceEnd, 1 AS NoOfDays, 7 AS rnk 

enter image description here

私は90日の期間内に3つの以上のインスタンスがあるでしょうフラグクエリを設計する必要があります。したがって、上記の日付の1406,1493,1536,1547,1660,1666はすべて(歴史的に)フラグを立てます。理想的には、クエリは毎日実行され、3番目のインスタンスが記録されるとすぐにアラートを設定します。私は様々なDATEDIFFと派生したクエリを試しましたが、うまく動作するようには見えません。 したがって、希望する出力は上記の表になりますが、最初の不在開始の日付範囲+ 90日以内のものに限定されます。私は何かが簡単でないことを知っている!

+1

「欠席開始+ 90日」に該当する列を比較する必要がありますか?それは「不在」ですか? –

答えて

1

これはループで行うことができます。下のコードを参照してください(コメント)。私は#tempにあなたの初期データを入れます:

if object_id('tempdb..#temp') is not null 
drop table #temp 

SELECT 'John Doe' AS Name, 1406 AS InstanceID, '2016-01-08 00:00:00.000' AS 

AbsenceStart, '2016-01-13 00:00:00.000' AS AbsenceEnd, 4 AS NoOfDays, 1 AS rnk 
into #temp 
UNION ALL 
SELECT 'John Doe' AS Name, 1493 AS InstanceID, '2016-02-02 00:00:00.000' AS AbsenceStart, '2016-02-05 00:00:00.000' AS AbsenceEnd, 4 AS NoOfDays, 2 AS rnk 
UNION ALL 
SELECT 'John Doe' AS Name, 1536 AS InstanceID, '2016-02-19 00:00:00.000' AS AbsenceStart, '2016-02-22 00:00:00.000' AS AbsenceEnd, 2 AS NoOfDays, 3 AS rnk 
UNION ALL 
SELECT 'John Doe' AS Name, 1547 AS InstanceID, '2016-02-26 00:00:00.000' AS AbsenceStart, '2016-03-10 00:00:00.000' AS AbsenceEnd, 10 AS NoOfDays, 4 AS rnk 
UNION ALL 
SELECT 'John Doe' AS Name, 1660 AS InstanceID, '2016-04-04 00:00:00.000' AS AbsenceStart, '2016-04-04 00:00:00.000' AS AbsenceEnd, 0.5 AS NoOfDays, 5 AS rnk 
UNION ALL 
SELECT 'John Doe' AS Name, 1666 AS InstanceID, '2016-04-07 00:00:00.000' AS AbsenceStart, '2016-04-14 00:00:00.000' AS AbsenceEnd, 6 AS NoOfDays, 6 AS rnk 
UNION ALL 
SELECT 'John Doe' AS Name, 1698 AS InstanceID, '2016-04-27 00:00:00.000' AS AbsenceStart, '2016-04-28 00:00:00.000' AS AbsenceEnd, 1 AS NoOfDays, 7 AS rnk 


-- First find the start rank: 
declare @x int = (select top 1 rnk from #temp order by rnk asc) 
-- Find the maximum number of records to loop through 
declare @y int = (select top 1 rnk from #temp order by rnk desc) 
-- This is your threshold for publishing 
declare @a int 

-- start loop 
while @x <= @y 


begin 

if @a >=3 
break; -- if threshold breached, stop loop. 

else  

if object_id('tempdb..#list') is not null 
drop table #list 

declare @z datetime = (select AbsenceStart from #temp where rnk = @x) 
print @z 

select 
instanceid, 
AbsenceStart 
into #list 
from #temp 
where AbsenceStart >= @z 
and AbsenceStart <= dateadd(dd,90,@z) 


set @a = (select count(instanceid) from #list) 



set @x = @x + 1 

print @x 

end 

insert into dbo.DestinationTable 
select * 
from #list