Solved

Sql Query

Posted on 2011-03-10
10
232 Views
Last Modified: 2012-05-11
I would like to do a sql that does the following:

Sum the number of days over a non-consecutive date range per emplid.  Here is data example
emplid          start date       end date
9999999      02/01/2010      04/30/2010
9999999      09/08/2009      01/29/2010      
9999999      09/07/2010      12/20/2010      

The query would give the total number of days worke within the date range of
Jan 1 2010 - Dec 31 2010

Any help would be greatly appreciated,

Nigluc
0
Comment
Question by:Lucia
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
10 Comments
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 35099452
try

select  emplid, sum(DATEDIFF(DD, [start date], [end date]) [Days Worked]
from table1
where startdate between '01/01/2010' and '12/31/2010'
0
 
LVL 32

Accepted Solution

by:
Ephraim Wangoya earned 500 total points
ID: 35099463
you need a group by as well

select  emplid, sum(DATEDIFF(DD, [start date], [end date]) [Days Worked]
from table1
where startdate between '01/01/2010' and '12/31/2010'
group by emplid
0
 

Author Comment

by:Lucia
ID: 35108150
Hi ewangoya,

If I run this query, I get the following answer:

SELECT * FROM [TableName] WHERE Date BETWEEN #12/31/1900# AND #12/31/2000# group on emplid, start date , end date

Emplid      start date      end date      DaysWorked
9999999      02/01/2010      30/04/2010      118
9999999      09/07/2010      20/12/2010      164

I still want part of the 3 record that applies to be included... ie

09/07/2010      12/20/2010      

I have to think about this .....

Do you have any ideas.  

Thanks,
Nigluc
I still wanty
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 

Author Comment

by:Lucia
ID: 35108361
Sorry I meant this record:

9999999      09/08/2009      01/29/2010      

Thanks, Nigluc
0
 

Author Comment

by:Lucia
ID: 35108908
select  
Emplid,
[start date],
[end date],
sum(DATEDIFF("d",[start date], [end date])) as DaysWorked
from table1

WHERE [end date] > #01/01/2010# and
[start date] <  #12/31/2010#

group by emplid,[start date], [end date]



So this will give me all three date ranges but I want only partial number of days in the

9999999      09/08/2009      01/29/201

ie only those that applly to the  Jan 1, 2010.

Is this possible?
0
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 35109151
Your where clause does not seem to be correct

select  
Emplid,
[start date],
[end date],
sum(DATEDIFF("d",[start date], [end date])) as DaysWorked
from table1
WHERE [start date] >= #01/01/2010#
and  [end date] <=  #12/31/2010#
0
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 35109163


select  
Emplid,
[start date],
[end date],
sum(DATEDIFF("d",[start date], [end date])) as DaysWorked
from table1
WHERE [start date] >= #01/01/2010#
and  [end date] <=  #12/31/2010#
group by emplid,[start date], [end date]
0
 

Author Comment

by:Lucia
ID: 35109230
Hi ewangoya,

I still want that third record.  The client still wants to count days that are in date ranges that would sill have the jan 1 -2010  onward.  so for the range in question I would like to see something like  29 days worked instead of 173....

9999999      09/08/2009      01/29/2010    

Thank you,
Nigluc
0
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 35109294
If you want the days worked for each record entry, you dont need the group by


select  
Emplid,
[start date],
[end date],
DATEDIFF(DD,[start date], [end date]) as DaysWorked
from table1
WHERE [start date] >= #01/01/2010#
and  [end date] <=  #12/31/2010#


0
 

Author Comment

by:Lucia
ID: 35110233
Hi  ewangoya,

This worked fine for me:

select  
Emplid,
[start date],
[end date],
SUM ( DATEDIFF("d",
 (CASE WHEN [start date] < '2010-1-1' then '2010-1-1' ELSE [end date] END),
  [end date])) AS DAYS_WORKED
from dbo.days_worked_2
WHERE [end date] > '01/01/2010'
and  [end date] <  '12/31/2010'
and  [start date] < '01/01/2010'
AND EMPLID='4147737'
group by emplid,[start date], [end date]

UNION

select  
Emplid,
[start date],
[end date],
 SUM ( DATEDIFF("d",
 (CASE WHEN [start date] > '2010-1-1' then [start date] ELSE [end date] END),
  [end date])) AS 'DAYS_WORKED'
from dbo.days_worked_2
WHERE [end date] > '01/01/2010'
and  [end date] <  '12/31/2010'
AND [start date] > '01/01/2010'
AND EMPLID='4147737'
group by emplid,[start date], [end date]


Results in :

'4147737'         2009-09-08 00:00:00      2010-01-29 00:00:00      28
'4147737'           2010-02-01 00:00:00      2010-04-30 00:00:00      88
'4147737'      2010-09-07 00:00:00      2010-12-20 00:00:00      104

Thanks for your  help.  

Nigluc
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question