我想搜索一個SQL表並找到兩個連續的缺失日期。搜索連續兩個缺失日期的SQL表
例如,人1個插入「日記」 1天2天的條目,錯過3天和第4天,並進入第5天
,因爲我不知道,我不張貼代碼的條目如何做到這一點。
謝謝!
我想搜索一個SQL表並找到兩個連續的缺失日期。搜索連續兩個缺失日期的SQL表
例如,人1個插入「日記」 1天2天的條目,錯過3天和第4天,並進入第5天
,因爲我不知道,我不張貼代碼的條目如何做到這一點。
謝謝!
我對這個問題的高級方法是從日期的動態表中選擇,使用整數計數器來添加或減去當前的日期時間以獲取您需要的未來或過去的日期,然後LEFT將你的數據表加入到這個數據表中,按日期排序並選擇第一行,或者N個具有NULL連接的行。
所以你的數據最終被
DATE ENTRY_ID
---- -----
2016-01-01 1
2016-01-02 2
2016-01-03 NULL
2016-01-04 3
2016-01-05 4
2016-01-06 NULL
2016-01-07 NULL
2016-01-08 NULL
而且你可以挑選你從這個數據集所需要的值
Try this your problem looks like similar to this :-
Declare @temp Table(id int identity(1,1) not null,CDate smalldatetime ,val int)
insert into @temp select '10/2/2012',1
insert into @temp select '10/3/2012',1
insert into @temp select '10/5/2012',1
insert into @temp select '10/7/2012',2
insert into @temp select '10/9/2012',2
insert into @temp select '10/10/2012',2
insert into @temp select '10/13/2012',2
insert into @temp select '10/15/2012',2
DECLARE @startDate DATE= '10/01/2012'
DECLARE @endDate DATE= '10/15/2012'
SELECT t.Id, X.[Date],Val = COALESCE(t.val,0)
FROM
(SELECT [Date] = DATEADD(Day,Number,@startDate)
FROM master..spt_values
WHERE Type='P'
AND DATEADD(day,Number,@startDate) <= @endDate)X
LEFT JOIN @temp t
ON X.[Date] = t.CDate
Alternative you can try this :-
WITH dates AS (
SELECT CAST('2009-01-01' AS DATETIME) 'date'
UNION ALL
SELECT DATEADD(dd, 1, t.date)
FROM dates t
WHERE DATEADD(dd, 1, t.date) <= '2009-02-01')
SELECT t.eventid, d.date
FROM dates d
JOIN TABLE t ON d.date BETWEEN t.startdate AND t.enddate
這會利用水平總建日曆日期的列表,從第一個輸入到最後一個,然後使用LAG()檢查給定日期與上一個日期,然後檢查這兩個日期中是否有關聯的條目來查找這兩天的間隔:
With diary as (
select to_date('01/01/2016','dd/mm/yyyy') entry_dt from dual union all
select to_date('02/01/2016','dd/mm/yyyy') entry_dt from dual union all
select to_date('04/01/2016','dd/mm/yyyy') entry_dt from dual union all
--leave two day gap of 5th and 6th
select to_date('07/01/2016','dd/mm/yyyy') entry_dt from dual union all
select to_date('08/01/2016','dd/mm/yyyy') entry_dt from dual union all
select to_date('10/01/2016','dd/mm/yyyy') entry_dt from dual)
select calendar_dt -1, calendar_dt
FROM (
select calendar_dt, entry_dt, lag(entry_dt) over (order by calendar_dt) prev_entry_dt
from diary
RIGHT OUTER JOIN (select min(entry_dt) + lvl as calendar_dt
FROM diary
,(select level lvl
from dual connect by level < (select max(entry_dt) - min(entry_dt)+1 from diary))
group by lvl) ON calendar_dt = entry_dt
order by calendar_dt
)
where entry_dt is null and prev_entry_dt is null
個
回報:
CALENDAR_DT-1, CALENDAR_DT
05/01/2016, 06/01/2016
我只是做日曆工作,以簡化建設全2天的差距,因爲如果一個人花了三天假,這將是兩個重疊的爲期兩天的差距(1天2和2-3天)。如果你想有一個輸出的兩個或更多天,那麼下面的作品的任何差距起點和終點更簡單查詢:
With diary as (
select to_date('01/01/2016','dd/mm/yyyy') entry_dt from dual union all
select to_date('02/01/2016','dd/mm/yyyy') entry_dt from dual union all
select to_date('04/01/2016','dd/mm/yyyy') entry_dt from dual union all
select to_date('07/01/2016','dd/mm/yyyy') entry_dt from dual union all
select to_date('08/01/2016','dd/mm/yyyy') entry_dt from dual union all
select to_date('10/01/2016','dd/mm/yyyy') entry_dt from dual)
select prev_entry_dt +1 gap_start, entry_dt -1 gap_end
FROM (
select entry_dt, lag(entry_dt) over (order by entry_dt) prev_entry_dt
from diary
order by entry_dt
) where entry_dt - prev_entry_dt > 2
問題 - 如果他們錯過三日內要看到兩個不同的兩一天的差距?還是三天的差距? –