Solved

Sql Query

Posted on 2011-03-10
10
233 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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

696 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