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

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

 SQL Server Forums Profile | Active Topics | Members | Search | Forum FAQ
 All Forums  SQL Server 2008 Forums  Transact-SQL (2008)  Calculating number of days worked excluding holida Reply to Topic  Printer Friendly
Author  Topic

Ghouri
Starting Member

Pakistan
2 Posts

 Posted - 03/10/2014 :  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:https://drive.google.com/file/d/0ByQA_cbUshlua0FNRDBrc1k2ZjQ/edit?usp=sharingKindly 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 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?

Ghouri
Starting Member

Pakistan
2 Posts

 Posted - 03/10/2014 :  08:08:05 I have updated image URL

RickD
Slow But Sure Yak Herding Master

United Kingdom
3608 Posts

 Posted - 03/10/2014 :  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.

khtan
In (Som, Ni, Yak)

Singapore
17689 Posts

 Posted - 03/10/2014 :  20:41:35 What does dates the #EMPCHILD represent ?How do you calculate the Absolute Working ? and how is it different from Actual Days ?KHTime is always against us

sqlsaga
Yak Posting Veteran

USA
93 Posts

 Posted - 03/11/2014 :  13:26:28 Use this query and it will do the needful...;WITH CTE AS(SELECT p.eid, p.Value, c.[date ]FROM #EMP aunpivot (Value FOR [WeekName] IN ([sun],[mon],[tue],[wed],[thu],[fri],[sat])) pINNER JOIN #EMPCHILD c ON LEFT(DATENAME(DW, c.date),3) = p.[WeekName] AND c.eid = p.eid)SELECT eid, COUNT(value) as CntFROM CTEWHERE [date] BETWEEN '03/01/2014' AND '03/08/2014' AND value = 1GROUP BY eidVisit www.sqlsaga.com for more t-sql snippets and BI related how to's.

sqlsaga
Yak Posting Veteran

USA
93 Posts

 Posted - 03/11/2014 :  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.
Topic
 Reply to Topic  Printer Friendly Jump To: Select Forum General SQL Server Forums       New to SQL Server Programming       New to SQL Server Administration       Script Library       Data Corruption Issues       Database Design and Application Architecture SQL Server 2012 Forums       Transact-SQL (2012)       SQL Server Administration (2012)       SSIS and Import/Export (2012)       Analysis Server and Reporting Services (2012)       Replication (2012)       Availability Groups and DR (2012)       Other SQL Server 2012 Topics SQL Server 2008 Forums       Transact-SQL (2008)       SQL Server Administration (2008)       SSIS and Import/Export (2008)       High Availability (2008)       Replication (2008)       Analysis Server and Reporting Services (2008)       Other SQL Server 2008 Topics SQL Server 2005 Forums       Transact-SQL (2005)       SQL Server Administration (2005)       .NET Inside SQL Server (2005)       SSIS and Import/Export (2005)       Service Broker (2005)       Replication (2005)       High Availability (2005)       Analysis Server and Reporting Services (2005)       Express Edition and Compact Edition (2005)       Other SQL Server Topics (2005) SQL Server 2000 Forums       SQL Server Development (2000)       SQL Server Administration (2000)       Import/Export (DTS) and Replication (2000)       Transact-SQL (2000)       Analysis Services (2000)       MSDE (2000) Development Tools       ASP.NET       Reporting Services Development       Other Development Tools Site Related Forums       Site Related Discussions       Article Discussion       Poll Discussion       The Yak Corral Other Forums       SQL Server 6.5 \ SQL Server 7.0       Other Topics       MS Access       ClearTrace Support Forum Old Forums       CLOSED - General SQL Server       CLOSED - SQL Server 2005/Yukon  -------------------- Home Active Topics Frequently Asked Questions Member Information Search Page
 SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC