Solved

Sql Query

Posted on 2011-03-10
10
229 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
  • 5
  • 5
10 Comments
 
LVL 32

Expert Comment

by:ewangoya
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:
ewangoya 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
 

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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 32

Expert Comment

by:ewangoya
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:ewangoya
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:ewangoya
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

758 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now