Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Calculating number of days worked excluding holida

Author  Topic 

Ghouri
Starting Member

2 Posts

Posted - 2014-03-10 : 02:36:22
I want to Calculate Number of Days that an Employee can work having Week Marked with Bit 1:
CREATE TABLE #EMP
(
eid INT default 0,
sun bit default 1,
mon bit default 1,
tue bit default 1,
wed bit default 1,
thu bit default 1,
fri bit default 1,
sat bit default 1
)
CREATE TABLE #EMPCHILD
(
eid INT default 0,
[date] DATETIME
)
INSERT INTO #EMP VALUES(1,0,1,0,0,1,1,1)
INSERT INTO #EMP VALUES(2,0,0,1,0,1,1,1)
INSERT INTO #EMP VALUES(3,0,1,0,0,1,1,1)
INSERT INTO #EMP VALUES(4,1,1,0,0,1,1,1)


INSERT INTO #EMPCHILD VALUES(1,'2014-03-01')
INSERT INTO #EMPCHILD VALUES(1,'2014-03-02')
INSERT INTO #EMPCHILD VALUES(1,'2014-03-03')
INSERT INTO #EMPCHILD VALUES(1,'2014-03-04')
INSERT INTO #EMPCHILD VALUES(1,'2014-03-05')
INSERT INTO #EMPCHILD VALUES(1,'2014-03-06')
INSERT INTO #EMPCHILD VALUES(1,'2014-03-07')
INSERT INTO #EMPCHILD VALUES(1,'2014-03-08')
INSERT INTO #EMPCHILD VALUES(1,'2014-03-09')
INSERT INTO #EMPCHILD VALUES(3,'2014-03-10')
INSERT INTO #EMPCHILD VALUES(3,'2014-03-01')
INSERT INTO #EMPCHILD VALUES(3,'2014-03-02')
INSERT INTO #EMPCHILD VALUES(3,'2014-03-03')
INSERT INTO #EMPCHILD VALUES(2,'2014-03-04')
INSERT INTO #EMPCHILD VALUES(3,'2014-03-05')
INSERT INTO #EMPCHILD VALUES(4,'2014-03-06')
INSERT INTO #EMPCHILD VALUES(2,'2014-03-07')
INSERT INTO #EMPCHILD VALUES(4,'2014-03-08')
INSERT INTO #EMPCHILD VALUES(3,'2014-03-09')
INSERT INTO #EMPCHILD VALUES(3,'2014-03-10')
INSERT INTO #EMPCHILD VALUES(2,'2014-03-01')
INSERT INTO #EMPCHILD VALUES(3,'2014-03-01')
INSERT INTO #EMPCHILD VALUES(4,'2014-03-01')
INSERT INTO #EMPCHILD VALUES(2,'2014-03-02')
INSERT INTO #EMPCHILD VALUES(3,'2014-03-01')
INSERT INTO #EMPCHILD VALUES(1,'2014-03-01')
INSERT INTO #EMPCHILD VALUES(2,'2014-03-03')
INSERT INTO #EMPCHILD VALUES(4,'2014-03-04')
INSERT INTO #EMPCHILD VALUES(3,'2014-03-01')

SELECT * from #EMP
SELECT * from #EMPCHILD




DROP TABLE #EMP
DROP TABLE #EMPCHILD

I have applied this query and got the result:
SELECT #EMP.eid,COUNT(*) FROM #EMP
LEFT JOIN #EMPCHILD ON #EMPCHILD.eid = #EMP.eid
WHERE #EMPCHILD.[Date] between '2014-03-01' AND '2014-03-08'

GROUP BY #EMP.eid

Now I want result set as following Image:
[url]https://drive.google.com/file/d/0ByQA_cbUshlua0FNRDBrc1k2ZjQ/edit?usp=sharing[/url]
Kindly anybody help

RickD
Slow But Sure Yak Herding Master

3608 Posts

Posted - 2014-03-10 : 07:17:15
I can't see your image in your link, but why not just do a sum instead of a count for a total, or if you want it by date, add a calendar table in to your database and query and then you can sum by week/month/quarter etc?
Go to Top of Page

Ghouri
Starting Member

2 Posts

Posted - 2014-03-10 : 08:08:05
I have updated image URL
Go to Top of Page

RickD
Slow But Sure Yak Herding Master

3608 Posts

Posted - 2014-03-10 : 11:11:45
OK, for the actual, just do a sum instead of your count. for the total, would the working days be the same from week to week as you have nothing for bank holidays, shift etc, so each week would be the same? If each week is the same, then it is very easy to sum for one week and then extrapolate that over the time you need it, otherwise you will need a date or weekno in your #emp table, in which case you could pivot and then sum.
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2014-03-10 : 20:41:35
What does dates the #EMPCHILD represent ?

How do you calculate the Absolute Working ? and how is it different from Actual Days ?


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

sqlsaga
Yak Posting Veteran

93 Posts

Posted - 2014-03-11 : 13:26:28
Use this query and it will do the needful...

;WITH CTE AS
(
SELECT p.eid, p.Value, c.[date ]
FROM #EMP a
unpivot (Value FOR [WeekName] IN ([sun],[mon],[tue],[wed],[thu],[fri],[sat])) p
INNER JOIN #EMPCHILD c ON LEFT(DATENAME(DW, c.date),3) = p.[WeekName] AND c.eid = p.eid
)
SELECT eid, COUNT(value) as Cnt
FROM CTE
WHERE [date] BETWEEN '03/01/2014' AND '03/08/2014' AND value = 1
GROUP BY eid

Visit www.sqlsaga.com for more t-sql snippets and BI related how to's.
Go to Top of Page

sqlsaga
Yak Posting Veteran

93 Posts

Posted - 2014-03-11 : 13:26:45
Read more about unpivoting here http://sqlsaga.com/sql-server/how-to-unpivot-single-column-in-sql-server/

Visit www.sqlsaga.com for more t-sql snippets and BI related how to's.
Go to Top of Page
   

- Advertisement -