How can I count continuous time frames
My data looks like this:
Id| Em_Name|Em_Reg_Date
--------------------------------
1 | John |2010-03-30 00:00:00
1 | John |2010-03-31 00:00:00
2 | Marc |2010-10-26 00:00:00
2 | Marc |2010-10-27 00:00:00
2 | Marc |2010-10-28 00:00:00
2 | Marc |2010-10-29 00:00:00
2 | Marc |2010-12-16 00:00:00
2 | Marc |2010-12-17 00:00:00
2 | Marc |2010-12-20 00:00:00
2 | Marc |2010-12-21 00:00:00
2 | Marc |2010-12-22 00:00:00
3 | Paul |2010-02-25 00:00:00
3 | Paul |2010-02-26 00:00:00
3 | Paul |2010-12-13 00:00:00
3 | Paul |2010-12-14 00:00:00
3 | Paul |2010-12-15 00:00:00
--------------------------------
A time frame is a continuous period of time.
e.g. Paul has following two (2) time frames FRAME 1 FROM 2010-02-25 00:00:00 to 2010-02-26 00:00:00
FRAME 2 FROM 2010-12-13 00:00:00 to 2010-12-15 00:00:00
So, the result should be like this
1 John 1
2 Marc 3
3 Paul 2
The question is: I need to count time frames for each Employee.
The problem here lies in the fact that I need to isolate the continues time frames in order to count them. I've even tried a declare cursor (works but I've to store the data in a temp table开发者_如何学C) And I want this to be in a "simple" sql statement Using max to find a start date works for only one frame. You can not find the second/third frame with max.
Is there anyone with fresh new ideas?
SQL Server 2005+
select em_name, COUNT(distinct startdate)
from
(
select *, startdate = em_reg_date - ROW_NUMBER() over (
partition by em_name order by em_reg_date) +1
from tbl
) X
group by Em_Name
Oracle, DB2 also support Row_Number(), but you will need some variation to calculating startdate
I'm not sure of the reason for both the ID and em_name fields, so I'll treat it as if the ID is sufficient to use alone.
The logic I'm using is simply this... A group can be represented by the last entry in the group. And the last entry is simply an entry that does not have a matching entry for the following day.
Provided that an Index for (ID, Em_Reg_Date) exists, this should be quite fast.
SELECT
ID,
COUNT(*)
FROM
your_table [source]
WHERE
NOT EXISTS (
SELECT
*
FROM
your_table
WHERE
Em_Reg_Date = [source].Em_Reg_Date + 1
AND ID = [source].ID
)
GROUP BY
ID
EDIT
This changes the logic to look "up to the next monday" if the current record is a Friday, Saturday or Sunday.
SET DATEFIRST 1 -- This just ensures that Monday is counted as Day 1
SELECT
ID,
COUNT(*)
FROM
your_table [source]
WHERE
NOT EXISTS (
SELECT
*
FROM
your_table
WHERE
ID = [source].ID
AND Em_Reg_Date <= [source].Em_Reg_Date + CASE WHEN DATEPART(weekday, [source].Em_Reg_Date) >= 5 THEN 8 - DATEPART(weekday, [source].Em_Reg_Date) ELSE 1 END
AND Em_Reg_Date > [source].Em_Reg_Date
)
GROUP BY
ID
SELECT Id, Name, COUNT( Id )
FROM (
SELECT Id, Name
FROM `<your_table_name>`
GROUP BY Name, MONTH( Em_Reg_Date )
) as X
GROUP BY Id
Tested on MySQL 5.0.7
精彩评论