I have a table which has records of user's vacation days. A Sample of that would be:
+---------+-----------+---------+------------+ | country | user_name | user_id | vac_date | +---------+-----------+---------+------------+ | canada | James | 1111 | 2015-02-13 | | canada | James | 1111 | 2015-02-17 | | canada | James | 1111 | 2015-02-18 | | canada | James | 1111 | 2015-02-10 | | canada | James | 1111 | 2015-02-11 | +---------+-----------+---------+------------+
With the above data, the count would be 3 from feb 13th to feb 18th, because 14th and 15th are weekends and the 16th is a holiday here in Canada. So essentially, I am trying to hold and continue the count if the user took the next working day off. I also have a table that has all the holidays which includes the country and the date of the holiday. Sample data for the holiday table would be:
+---------+-------------+-------------+ | country | holidayDesc | holidayDate | +---------+-------------+-------------+ | canada | Family Day | 2015-02-16 | +---------+-------------+-------------+
Currently i have a query in SQL that counts the the dates normally, so it only counts whatever is in the vacation table. For example: if a user took march 3rd 2015, march 4th 2015, and march 5th 2015 off, then it will have a count of 3, but for that above table example, it would only have a count of 1 for feb 13th and 2 from feb 17th to feb 18th.
SELECT DISTINCT user_name ,min(vac_date) as startDate ,max(vac_date) as endDate ,datediff(day, min(vac_date), max(vac_date)) as consecutiveCount FROM ( SELECT user_name ,vac_date ,user_id ,groupDate = DATEADD(DAY, - ROW_NUMBER() OVER ( PARTITION BY user_id ORDER BY vac_date ), vac_date) FROM mytable WHERE country = 'canada' AND vac_date BETWEEN '20150101' AND '20151231' ) z GROUP BY user_name ,groupDate HAVING datediff(day, min(vac_date), max(vac_date)) >= 0 ORDER BY user_name ,min(vac_date);
This is what it currently outputs from the above sample data:
+-----------+------------+------------+------------------+ | user_name | startDate | endDate | consecutiveCount | +-----------+------------+------------+------------------+ | James | 2015-02-10 | 2015-02-11 | 2 | | James | 2015-02-13 | 2015-02-13 | 1 | | James | 2015-02-17 | 2015-02-18 | 2 | +-----------+------------+------------+------------------+
Ideally i would like it to be:
+-----------+------------+------------+------------------+ | user_name | startDate | endDate | consecutiveCount | +-----------+------------+------------+------------------+ | James | 2015-02-10 | 2015-02-11 | 2 | | James | 2015-02-13 | 2015-02-18 | 3 | +-----------+------------+------------+------------------+
But i don't know if that is possible with pure SQL. I can also try to incorporate it into C#.
If it helps I am also using C# and SQL Server Management Studio. Any help would be appreciated. Thanks in advance