473,396 Members | 2,017 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Datediff help needed

Hi all,

I'm searching for a solution on the following.

In a table I have the following columns:

Row Id - Call Id - Date ---------- Action
001 ------ 230 ---- 10-10-2007 - Logged
002 ------ 230 ---- 10-12-2007 - Closed
003 ------ 230 ---- 10-19-2007 - Reactivated
004 ------ 230 ---- 10-21-2007 - Closed
005 ------ 231 ---- 10-22-2007 - Logged
etc...

row id = unique
Action can be:
- Logged
- Closed
- Reactivated

In this table more actions can be logged on one Call id.

I need a query what calculates the difference in days between logged and (the first following) closed added with the days from (also the first coming) Reactivated untill (the next) closed as long as there are records for that call id.
ordering on row_id will ensure the right order.

I've worked myself to the bone to figure this one out on mssql but can't get there.

I've tried to make a cursor with log and close time in one record, then union it with the reactivated and closed records but I failed to get it work.

Is there anyone who can give me a hand please?

Thanks in advance.

Karsten
Jan 5 '08 #1
3 1120
camel
55
Interestingly a post just the other day on this site "Complex query (compare records in same table / join a 2nd table) " was very similar in nature, and unlike your happy situation did not start off with a unique RowID in hand. Have a look at my solution in that post as it should show how to you deal with your query without any need for a cursor.
Jan 6 '08 #2
Oh yes, I read the post (and many others before posting in here)
forgot to mention, I'm not able to create a table in the database.
I've only read (select) access... :-(

is there any way around?
Jan 6 '08 #3
camel
55
I think this what you want, tested it with your small sample of records.

The inner query (AS C) finds records that are "Closed" and the query around it (AS D) finds records "Closed" that were preceded by "Reactivated". This is the same gig as the post I referrred you to. Finally the outer query joins on CallID to compare the LoggedDate to the ClosedDate, knowing from inner query (AS D) that only Closed preceded by Reactivated records are involved.

Expand|Select|Wrap|Line Numbers
  1.  
  2. SELECT    R.CallID, 
  3.     DATEDIFF(d, R.[Date], D.ClosedDate) ClosedMinusLoggedInDays  
  4. FROM    Records R
  5. JOIN        (SELECT    R.CallID, C.ClosedDate
  6.     FROM    Records R
  7.     JOIN   (SELECT    RowID,CallID, [Date] ClosedDate
  8.                FROM    Records R
  9.                WHERE    Action = 'Closed') AS C
  10.           ON R.CallID = C.CallID
  11.                     AND (R.RowID + 1) = C.RowID
  12.     WHERE    R.Action = 'Reactivated') AS D
  13.           ON    R.CallID = D.CallID
  14. WHERE    R.Action = 'Logged'    
  15. ORDER BY R.RowID
  16.  
Jan 6 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: inamori | last post by:
I face that problems 07/01/2003 06/30/2006 ---------> it should be 3 01/01/2003 02/28/2005 --------->could i get 2 years and 2 months 01/01/2003 03/01/2005 ...
1
by: intl04 | last post by:
I'm trying to set up a query that will include a new field ('Days until completion') whose value is derived from the DateDiff function. I think I have the syntax correct but am not sure. Days...
4
by: Paolo | last post by:
I am having some problem with a Year Function. I have form on which I have 4 field which indicate dates and an additional form which sums those dates: These are the fields: YEARS...
7
by: Adrian | last post by:
I hit on this problem converting a VB.NET insurance application to C#. Age next birthday calculated from date of birth is often needed in insurance premium calculations. Originally done using...
5
by: sr | last post by:
Anyone know of a better way to simulate a datediff for C#, i.e., without referencing the VB.NET runtime? Only added the functionality that was needed for me so it is not the full implementation...
6
by: kevinjwilson | last post by:
I am trying to get the date difference between two dates but I don't want the function to include weekends in the calculation. Does anyone have an idea on how to make this work?
3
by: Dinesh | last post by:
Hi Experts, I am working on SSRS 2005, and I am facing a problem in counting the no of days. My database has many fields but here I am using only two fields They are Placement_Date and...
2
by: ankitmathur | last post by:
Hi Friends, I have been trying to use this query to get all records that will expire within the next 30 days from the current date. While I am getting records with the same query what is...
14
by: cmdolcet69 | last post by:
I'm trying to use the DateDiff function to calculate the difference whether a shift has been setup. when i run the code below with strFirstShiftEnd as a stringor date or datetime. I get an error...
6
by: Wheeler2008 | last post by:
Hi All, I am currently running a query on a MS Access table, in which I want to be able to compare and total the difference in dates between records. I only have one date field within the table. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.