我有一个名为 timedetail 的表,其中输入了工作时间(doecode w)或休假时间(doecode vac)。这是我到目前为止所拥有的,但它也在返回工作时间。
TimeDetail
empnum tsdate doecode
123 1/1/2013 VAC
123 1/2/2013 VAC
123 1/3/2013 VAC
123 1/4/2013 VAC
123 1/5/2013 VAC
123 1/6/2013 W
123 1/7/2013 W
123 1/8/2013 W
123 1/9/2013 VAC
111 1/1/2013 W
111 1/2/2013 VAC
111 1/3/2013 W
111 1/4/2013 VAC
111 1/5/2013 VAC
empnum 123 应该被返回,但不是 111
WITH R AS (
SELECT
empnum, tsdate,
ROW_NUMBER() OVER (PARTITION BY empnum ORDER BY tsdate)
- ROW_NUMBER() OVER (PARTITION BY empnum ORDER BY tsdate, doecode) as grp
FROM
timedetail t
WHERE
doecode in ('VAC')
-- and not exists (
-- select * from timedetail b where doecode < ' '
-- and b.tsdate = t.tsdate and b.empnum = t.empnum
-- )
)
SELECT empnum, MIN(tsdate) AS fdt, MAX(tsdate) AS tdt, COUNT(*) AS no_of_days
FROM R
GROUP BY empnum, grp
HAVING COUNT(*) > 4
ORDER BY empnum, MIN(tsdate), MAX(tsdate)