SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Calculating number of days worked excluding holida
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Ghouri
Starting Member

Pakistan
2 Posts

Posted - 03/10/2014 :  02:36:22  Show Profile  Reply with Quote
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:
https://drive.google.com/file/d/0ByQA_cbUshlua0FNRDBrc1k2ZjQ/edit?usp=sharing
Kindly anybody help

Edited by - Ghouri on 03/10/2014 03:22:09

RickD
Slow But Sure Yak Herding Master

United Kingdom
3608 Posts

Posted - 03/10/2014 :  07:17:15  Show Profile  Reply with Quote
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

Pakistan
2 Posts

Posted - 03/10/2014 :  08:08:05  Show Profile  Reply with Quote
I have updated image URL
Go to Top of Page

RickD
Slow But Sure Yak Herding Master

United Kingdom
3608 Posts

Posted - 03/10/2014 :  11:11:45  Show Profile  Reply with Quote
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)

Singapore
17584 Posts

Posted - 03/10/2014 :  20:41:35  Show Profile  Reply with Quote
What does dates the #EMPCHILD represent ?

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


KH
Time is always against us

Go to Top of Page

sqlsaga
Yak Posting Veteran

USA
93 Posts

Posted - 03/11/2014 :  13:26:28  Show Profile  Reply with Quote
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

USA
93 Posts

Posted - 03/11/2014 :  13:26:45  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.11 seconds. Powered By: Snitz Forums 2000