473,569 Members | 2,759 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Date Difference for different records with same GroupId

Hello All

I have table with following fields:

GroupId VisitDate
1 10/19/1993
1 11/24/1998
2 10/18/1993
2 10/29/1998
3 10/21/1993
3 11/03/1998
4 10/25/1993
4 10/29/1998
4 04/29/1999
5 04/26/1994
5 03/29/1999
6 09/06/2001
7 09/07/1995
7 09/12/2000

In this table, I want to create a third field dynamically, where the
difference in date between the two visits for the same GroupId should
be displayed, so the output should be like this...

GroupId VisitDate Diff_VisitDate
1 10/19/1993 0
1 11/24/1998 5 Years

2 10/18/1993 0
2 10/29/1997 4 Years

3 10/21/1993 0
3 11/03/1998 5 Years

4 10/25/1993 0
4 10/29/1994 1 Year
4 04/29/1999 5 Years 6 Months

5 04/26/1994 0
5 03/29/1999 5 Years
5 03/28/2000 6 Years

6 09/06/2001 0

7 09/07/1995 0
7 09/12/2001 6 Years

So basically for the first VisitDate for every GroupId, Diff_VisitDate
should be 0 and for every successive VisitDate, this date should be
subtracted from the first VisitDate and displayed in Diff_VisitDate. Is
it possible to do this for a database of 3000 records?
Any help in this regard will be highly appreciated.
Thanks in advance.
Anita

Nov 13 '05 #1
5 2151
On 31 Jan 2005 20:11:15 -0800, ta*******@yahoo .com wrote:

This example uses the Northwind sample application. Pretend that
CustomerID=Grou pId, and OrderDate=Visit Date.
Since you are asking for difference in dates, we know we have to use
the DateDiff function. It takes two dates: the one for the current
row, and the maximum date smaller than that of the current row (which
we get using a subselect).

Paste this into a new query (sql view) in the Northwind database:

SELECT Orders.Customer ID, Orders.OrderDat e, DateDiff("d",(s elect
max(OrderDate) from Orders as O2 where O2.CustomerID=O rders.CustomerI D
and O2.OrderDate<Or ders.OrderDate) ,[Orders].[OrderDate]) AS
DiffOrderDate FROM Orders;

If you like this solution, promise me one thing: do NOT store this
"calculated value" in a new field. Rather use this query to calculate
the difference on the fly. That way you're not violating an important
relational database design rule.

3000 records: peanuts.

-Tom.

Hello All

I have table with following fields:

GroupId VisitDate
1 10/19/1993
1 11/24/1998
2 10/18/1993
2 10/29/1998
3 10/21/1993
3 11/03/1998
4 10/25/1993
4 10/29/1998
4 04/29/1999
5 04/26/1994
5 03/29/1999
6 09/06/2001
7 09/07/1995
7 09/12/2000

In this table, I want to create a third field dynamically, where the
difference in date between the two visits for the same GroupId should
be displayed, so the output should be like this...

GroupId VisitDate Diff_VisitDate
1 10/19/1993 0
1 11/24/1998 5 Years

2 10/18/1993 0
2 10/29/1997 4 Years

3 10/21/1993 0
3 11/03/1998 5 Years

4 10/25/1993 0
4 10/29/1994 1 Year
4 04/29/1999 5 Years 6 Months

5 04/26/1994 0
5 03/29/1999 5 Years
5 03/28/2000 6 Years

6 09/06/2001 0

7 09/07/1995 0
7 09/12/2001 6 Years

So basically for the first VisitDate for every GroupId, Diff_VisitDate
should be 0 and for every successive VisitDate, this date should be
subtracted from the first VisitDate and displayed in Diff_VisitDate. Is
it possible to do this for a database of 3000 records?
Any help in this regard will be highly appreciated.
Thanks in advance.
Anita


Nov 13 '05 #2
Tom
THank you so much for your reply. I am sorry to tell you that I have 2
problems while I used your solution, it subtracts from the previus
orderdate and not from the max orderdate of that group and the second
problem is that it gives number of days, is it anyway possible to
convert it to years/months.
My apologises if I am asking for too much.
Thanks again.

Nov 13 '05 #3
On 1 Feb 2005 05:17:32 -0800, ta*******@yahoo .com wrote:

No problem; just take out the part that says that the date needs to be
less than that of the current row:
SELECT Orders.Customer ID, Orders.OrderDat e,
DateDiff("d",[Orders].[OrderDate],(select max(OrderDate) from Orders
as O2 where O2.CustomerID=O rders.CustomerI D)) AS DiffOrderDate
FROM Orders;

I'll leave the conversion from days to years up to you. One hint: if
you can get away with months, just change a single letter in the
DateDiff function. If you truly need "Four Years and Eight Months" a
custom function would likely be needed.

-Tom.
Tom
THank you so much for your reply. I am sorry to tell you that I have 2
problems while I used your solution, it subtracts from the previus
orderdate and not from the max orderdate of that group and the second
problem is that it gives number of days, is it anyway possible to
convert it to years/months.
My apologises if I am asking for too much.
Thanks again.


Nov 13 '05 #4
Thanks Tom
It worked and i had to write a custom function to make it to "Four Year
and Eight Months".
Thanks for your wonderful help. I really appreciate it.
Tom van Stiphout wrote:
On 1 Feb 2005 05:17:32 -0800, ta*******@yahoo .com wrote:

No problem; just take out the part that says that the date needs to be less than that of the current row:
SELECT Orders.Customer ID, Orders.OrderDat e,
DateDiff("d",[Orders].[OrderDate],(select max(OrderDate) from Orders
as O2 where O2.CustomerID=O rders.CustomerI D)) AS DiffOrderDate
FROM Orders;

I'll leave the conversion from days to years up to you. One hint: if
you can get away with months, just change a single letter in the
DateDiff function. If you truly need "Four Years and Eight Months" a
custom function would likely be needed.

-Tom.
Tom
THank you so much for your reply. I am sorry to tell you that I have 2problems while I used your solution, it subtracts from the previus
orderdate and not from the max orderdate of that group and the secondproblem is that it gives number of days, is it anyway possible to
convert it to years/months.
My apologises if I am asking for too much.
Thanks again.


Nov 13 '05 #5
Hello Everybody
With regard to the same query, I have another problem and I just dont
understand where am I going wrong and I m not getting the result I
need.
Following the suggestion given by Tom van and everybody else, I solved
my previous problem and I also converted the Diff_VisitDate into Months
like this :

GroupId VisitDate Diff_VisitDate( in months)
1 10/19/1993 0
1 11/24/1998 60

2 10/18/1993 0
2 10/29/1997 48

3 10/21/1993 0
3 11/03/1998 60

4 10/25/1993 0
4 10/29/1994 12
4 04/29/1999 66

5 04/26/1994 0
5 03/29/1999 60
5 03/28/2000 72

6 09/06/2001 0

7 09/07/1995 0
7 09/12/2001 72

Now I am trying to create a query, which gives me the GroupId that
does/does not have a particular value.
But when I create the query, it runs for each and every record, but i
just want the result for the set of each GroupId.

Say if I want to find the list of GroupId that does not have 60
(Diff_VisitDate ), the result should be

GroupId
2
4
6
7

Is it possible to do this ? Can somebody tell me how to group the
records in the query by GroupId.

Thanks again in advance for all your help. Kindly excuse me if this is
a silly question.
ta*******@yahoo .com wrote:
Thanks Tom
It worked and i had to write a custom function to make it to "Four Year and Eight Months".
Thanks for your wonderful help. I really appreciate it.
Tom van Stiphout wrote:
On 1 Feb 2005 05:17:32 -0800, ta*******@yahoo .com wrote:

No problem; just take out the part that says that the date needs to be
less than that of the current row:
SELECT Orders.Customer ID, Orders.OrderDat e,
DateDiff("d",[Orders].[OrderDate],(select max(OrderDate) from Orders
as O2 where O2.CustomerID=O rders.CustomerI D)) AS DiffOrderDate
FROM Orders;

I'll leave the conversion from days to years up to you. One hint: if you can get away with months, just change a single letter in the
DateDiff function. If you truly need "Four Years and Eight Months" a custom function would likely be needed.

-Tom.
Tom
THank you so much for your reply. I am sorry to tell you that I

have 2problems while I used your solution, it subtracts from the previus
orderdate and not from the max orderdate of that group and the secondproblem is that it gives number of days, is it anyway possible to
convert it to years/months.
My apologises if I am asking for too much.
Thanks again.


Nov 13 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2145
by: iam247 | last post by:
Hi I'm using Access 2002. I have 2 tables tblGroupContact, tblGroupPermission, both have 2 fields identical structure: ContactID GroupID (Both are Composite keys and both hold integers) tblGroupContact holds everybody and the groups they are members of. tblGroupPermission holds only those people who have permission to make
2
2448
by: Riegn Man | last post by:
I have a problem with access and our time clocks. We have time clocks that put out a .log file with the badge swipes for everybody. There is one .log file for each day. I am pulling that data into an access database to manipulate it. In the table I have: FIRSTNAME | LASTNAME | BADGE# | DATE | TIME The problem is that the...
12
6353
by: Steve Elliott | last post by:
I have a query set up to gather together data between two specified dates. Shown in the query column as: Between #24/09/2004# And #01/10/2004# Is it possible to enter several different date ranges, ie between 24/09/2004 and 01/10/2004 together with 05/10/2004 and 07/10/2004 ? If I enter the "Between" criteria on different lines it...
0
1051
by: tamilan71 | last post by:
Hello Everybody With regard to the same query, I have another problem and I just dont understand where am I going wrong and I m not getting the result I need. Following the suggestion given by Tom van and everybody else, I solved my previous problem and I also converted the Diff_VisitDate into Months like this : GroupId VisitDate...
6
17905
by: Tony Miller | last post by:
All I have an aggregate query using the function Month & Year on a datereceived field ie: TheYear: Year() TheMonth: Month() These are the group by fields to give me a Count on another field by year & month When I try to place a date filter 'Between x And y ' on an expression field
29
9071
by: james | last post by:
I have a problem that at first glance seems not that hard to figure out. But, so far, the answer has escaped me. I have an old database file that has the date(s) stored in it as number of days. An example is: 36,525 represents 01/01/1900. The starting point date is considered to be : 00/00/0000. I have looked thru Help and used Google and...
2
9457
by: Deepamathi | last post by:
Thanks in advance for your help. I need a Stored Procedure that will do the following steps: 1. delete a single record from table GROUP 2. delete all records from table SUBGROUP with a matching 'groupID' from the deleted GROUP 2. as each record from SUBGROUP is deleted there are multiple associated records in the table...
1
1164
by: IsMale | last post by:
Hi. I am new to web services. Could someone please help me with the scripts below? I will be parsing a GroupID to the web service. This web services will the connect to a stored procedure which will return all records belonging to the GroupID. My problem is how do i "return" the row and columns of data to the C# page that is requesting the...
4
1758
by: RussCRM | last post by:
I have a subform in datasheet form with the fields Date, Type and Note (sorted by Date). There are 4-5 records generally per date. Is there a way to group them visually somehow by date such as alternating colors/shading per date or a bold line between groups of records of a particular date. Any ideas?
0
7924
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7970
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6284
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5513
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3640
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1213
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.