Update query criteria

R

Radhika

I have the following SQL for my update query:

UPDATE tbl_NewPatientInfo INNER JOIN tbl_TEPRecords ON
tbl_NewPatientInfo.[MDACC#] = tbl_TEPRecords.[MDACC#] SET
tbl_NewPatientInfo.[Reason for referral] = "Initial Fit"
WHERE (((DateDiff("d",[tbl_NewPatientInfo].[Date 1st TEP],[Date]))<30 And
(DateDiff("d",[tbl_NewPatientInfo].[Date 1st TEP],[Date]))>0)) OR
(((DateDiff("d",[tbl_NewPatientInfo].[Date 2nd TEP],[Date]))<30 And
(DateDiff("d",[tbl_NewPatientInfo].[Date 2nd TEP],[Date]))>0)) OR
(((DateDiff("d",[tbl_NewPatientInfo].[Surgery date],[Date]))<30 And
(DateDiff("d",[tbl_NewPatientInfo].[Surgery date],[Date]))>0));

For some reason, it is updating only the those <30, but not those >0. What
am i doing wrong?

Thank you,
Radhika
 
K

Ken Sheridan

Radhika:

Anything less than 30 is also greater than 0 unless its 0 or less, so I'm
not sure what you mean when you say 'not those >0'.

In effect you are asking with each criterion for rows where the difference
between the TEP or surgery date and the value of [Date] (is this a
parameter?) is between 1 and 30 days, i.e. the TEP or surgery date is less
than 30 days before [Date], but not [Date] or later. If you want to also
include those with dates of [Date] or later, then you don't need to test for
0 at all. If you want to include rows with dates on [Date] but not later,
then test for >-1.

Ken Sheridan
Stafford, England

Radhika said:
I have the following SQL for my update query:

UPDATE tbl_NewPatientInfo INNER JOIN tbl_TEPRecords ON
tbl_NewPatientInfo.[MDACC#] = tbl_TEPRecords.[MDACC#] SET
tbl_NewPatientInfo.[Reason for referral] = "Initial Fit"
WHERE (((DateDiff("d",[tbl_NewPatientInfo].[Date 1st TEP],[Date]))<30 And
(DateDiff("d",[tbl_NewPatientInfo].[Date 1st TEP],[Date]))>0)) OR
(((DateDiff("d",[tbl_NewPatientInfo].[Date 2nd TEP],[Date]))<30 And
(DateDiff("d",[tbl_NewPatientInfo].[Date 2nd TEP],[Date]))>0)) OR
(((DateDiff("d",[tbl_NewPatientInfo].[Surgery date],[Date]))<30 And
(DateDiff("d",[tbl_NewPatientInfo].[Surgery date],[Date]))>0));

For some reason, it is updating only the those <30, but not those >0. What
am i doing wrong?

Thank you,
Radhika
 
R

Radhika

Sorry for the ambiguity. I want 'ReasonForReferral' to be selected as
'Initial Fit', if the date difference is between 0 and 30. Therefore, I want
to include rows with dates on [Date] and 30 days before [Date] but not later.
I have some TEP and surgery dates that are later than [Date] and they give me
negative values for the date difference, e.g. -1000. I do not want such date
difference values to update the 'ReasonForReferral' field. However, this is
what is happening. I tried replacing >0 with >-1, but that gives me the same
results.

[Date] is the name of a field.

I am not sure about whether there is a problem with my SQL or whether there
is another issue?

Thank you!
Radhika


Ken Sheridan said:
Radhika:

Anything less than 30 is also greater than 0 unless its 0 or less, so I'm
not sure what you mean when you say 'not those >0'.

In effect you are asking with each criterion for rows where the difference
between the TEP or surgery date and the value of [Date] (is this a
parameter?) is between 1 and 30 days, i.e. the TEP or surgery date is less
than 30 days before [Date], but not [Date] or later. If you want to also
include those with dates of [Date] or later, then you don't need to test for
0 at all. If you want to include rows with dates on [Date] but not later,
then test for >-1.

Ken Sheridan
Stafford, England

Radhika said:
I have the following SQL for my update query:

UPDATE tbl_NewPatientInfo INNER JOIN tbl_TEPRecords ON
tbl_NewPatientInfo.[MDACC#] = tbl_TEPRecords.[MDACC#] SET
tbl_NewPatientInfo.[Reason for referral] = "Initial Fit"
WHERE (((DateDiff("d",[tbl_NewPatientInfo].[Date 1st TEP],[Date]))<30 And
(DateDiff("d",[tbl_NewPatientInfo].[Date 1st TEP],[Date]))>0)) OR
(((DateDiff("d",[tbl_NewPatientInfo].[Date 2nd TEP],[Date]))<30 And
(DateDiff("d",[tbl_NewPatientInfo].[Date 2nd TEP],[Date]))>0)) OR
(((DateDiff("d",[tbl_NewPatientInfo].[Surgery date],[Date]))<30 And
(DateDiff("d",[tbl_NewPatientInfo].[Surgery date],[Date]))>0));

For some reason, it is updating only the those <30, but not those >0. What
am i doing wrong?

Thank you,
Radhika
 
J

John Spencer

UPDATE tbl_NewPatientInfo INNER JOIN tbl_TEPRecords
ON tbl_NewPatientInfo.[MDACC#] = tbl_TEPRecords.[MDACC#]
SET tbl_NewPatientInfo.[Reason for referral] = "Initial Fit"
WHERE
DateDiff("d",[tbl_NewPatientInfo].[Date 1st TEP],[Date])Between 1 and 29
OR DateDiff("d",[tbl_NewPatientInfo].[Date 2nd TEP],[Date])Between 1 and 29
OR DateDiff("d",[tbl_NewPatientInfo].[Surgery date],[Date])Between 1 and 29


That means that Reason For Referral will be set to "Initial Fit" if any of the
3 dates are within 1 to 29 days before [Date]. Is that what you are trying to
accomplish. You original query looks as if it should give the same results.

John Spencer
Access MVP 2002-2005, 2007-2008
The Hilltop Institute
University of Maryland Baltimore County
 
R

Radhika

Thank you!

I tried the SQL you sent me, however, I am still getting -ve values for the
date diff in my results. I also get this when I use the SQL I was trying. I
fail to understand why this is not working.

How would I convert this into an IIf statement. Maybe that would work???
Sincerely,
Radhika


John Spencer said:
UPDATE tbl_NewPatientInfo INNER JOIN tbl_TEPRecords
ON tbl_NewPatientInfo.[MDACC#] = tbl_TEPRecords.[MDACC#]
SET tbl_NewPatientInfo.[Reason for referral] = "Initial Fit"
WHERE
DateDiff("d",[tbl_NewPatientInfo].[Date 1st TEP],[Date])Between 1 and 29
OR DateDiff("d",[tbl_NewPatientInfo].[Date 2nd TEP],[Date])Between 1 and 29
OR DateDiff("d",[tbl_NewPatientInfo].[Surgery date],[Date])Between 1 and 29


That means that Reason For Referral will be set to "Initial Fit" if any of the
3 dates are within 1 to 29 days before [Date]. Is that what you are trying to
accomplish. You original query looks as if it should give the same results.

John Spencer
Access MVP 2002-2005, 2007-2008
The Hilltop Institute
University of Maryland Baltimore County
I have the following SQL for my update query:

UPDATE tbl_NewPatientInfo INNER JOIN tbl_TEPRecords ON
tbl_NewPatientInfo.[MDACC#] = tbl_TEPRecords.[MDACC#] SET
tbl_NewPatientInfo.[Reason for referral] = "Initial Fit"
WHERE (((DateDiff("d",[tbl_NewPatientInfo].[Date 1st TEP],[Date]))<30 And
(DateDiff("d",[tbl_NewPatientInfo].[Date 1st TEP],[Date]))>0)) OR
(((DateDiff("d",[tbl_NewPatientInfo].[Date 2nd TEP],[Date]))<30 And
(DateDiff("d",[tbl_NewPatientInfo].[Date 2nd TEP],[Date]))>0)) OR
(((DateDiff("d",[tbl_NewPatientInfo].[Surgery date],[Date]))<30 And
(DateDiff("d",[tbl_NewPatientInfo].[Surgery date],[Date]))>0));

For some reason, it is updating only the those <30, but not those >0. What
am i doing wrong?

Thank you,
Radhika
 
J

John Spencer

I doubt that using IIF will help. I think your problem is that while
one of the three conditions reports negative numbers one or more of the
other three conditions is returning a value between 1 and 29.

Instead of an update query, try running a select query and see what the
results are

SELECT
DateDiff("d",[tbl_NewPatientInfo].[Date 1st TEP],[Date]) as 1stTep
, DateDiff("d",[tbl_NewPatientInfo].[Date 2nd TEP],[Date]) as 2ndTep
, DateDiff("d",[tbl_NewPatientInfo].[Surgery date],[Date]) as Surg
FROM tbl_NewPatientInfo INNER JOIN tbl_TEPRecords
ON tbl_NewPatientInfo.[MDACC#] = tbl_TEPRecords.[MDACC#]
WHERE DateDiff("d",[tbl_NewPatientInfo].[Date 1st TEP],[Date]) <= 0
And (DateDiff("d",[tbl_NewPatientInfo].[Date 2nd TEP],[Date]) > 0
OR DateDiff("d",[tbl_NewPatientInfo].[Surgery date],[Date]) > 0)

See if you get results where 1st Tep is negative and one of the other is
positive

'====================================================
John Spencer
Access MVP 2002-2005, 2007-2008
The Hilltop Institute
University of Maryland Baltimore County
'====================================================

Thank you!

I tried the SQL you sent me, however, I am still getting -ve values for the
date diff in my results. I also get this when I use the SQL I was trying. I
fail to understand why this is not working.

How would I convert this into an IIf statement. Maybe that would work???
Sincerely,
Radhika


John Spencer said:
UPDATE tbl_NewPatientInfo INNER JOIN tbl_TEPRecords
ON tbl_NewPatientInfo.[MDACC#] = tbl_TEPRecords.[MDACC#]
SET tbl_NewPatientInfo.[Reason for referral] = "Initial Fit"
WHERE
DateDiff("d",[tbl_NewPatientInfo].[Date 1st TEP],[Date])Between 1 and 29
OR DateDiff("d",[tbl_NewPatientInfo].[Date 2nd TEP],[Date])Between 1 and 29
OR DateDiff("d",[tbl_NewPatientInfo].[Surgery date],[Date])Between 1 and 29


That means that Reason For Referral will be set to "Initial Fit" if any of the
3 dates are within 1 to 29 days before [Date]. Is that what you are trying to
accomplish. You original query looks as if it should give the same results.

John Spencer
Access MVP 2002-2005, 2007-2008
The Hilltop Institute
University of Maryland Baltimore County
I have the following SQL for my update query:

UPDATE tbl_NewPatientInfo INNER JOIN tbl_TEPRecords ON
tbl_NewPatientInfo.[MDACC#] = tbl_TEPRecords.[MDACC#] SET
tbl_NewPatientInfo.[Reason for referral] = "Initial Fit"
WHERE (((DateDiff("d",[tbl_NewPatientInfo].[Date 1st TEP],[Date]))<30 And
(DateDiff("d",[tbl_NewPatientInfo].[Date 1st TEP],[Date]))>0)) OR
(((DateDiff("d",[tbl_NewPatientInfo].[Date 2nd TEP],[Date]))<30 And
(DateDiff("d",[tbl_NewPatientInfo].[Date 2nd TEP],[Date]))>0)) OR
(((DateDiff("d",[tbl_NewPatientInfo].[Surgery date],[Date]))<30 And
(DateDiff("d",[tbl_NewPatientInfo].[Surgery date],[Date]))>0));

For some reason, it is updating only the those <30, but not those >0. What
am i doing wrong?

Thank you,
Radhika
 
K

Ken Sheridan

At present, because you are using Boolean OR operations, rows will be updated
where the date difference is within 30 days for any of the three criteria.
Are you saying that if one of the criteria falls outside the 30 day
difference than you don't want that row updated, even if another falls within
it? If so then you would need to use Boolean AND operations, but also handle
any Nulls as comparing a Null to anything evaluates not to True or to False,
but to Null. So the query would be:

UPDATE tbl_NewPatientInfo INNER JOIN tbl_TEPRecords
ON tbl_NewPatientInfo.[MDACC#] = tbl_TEPRecords.[MDACC#]
SET tbl_NewPatientInfo.[Reason for referral] = "Initial Fit"
WHERE
((DateDiff("d",[tbl_NewPatientInfo].[Date 1st TEP],[Date])<30
AND DateDiff("d",[tbl_NewPatientInfo].[Date 1st TEP],[Date])>=0)
OR [tbl_NewPatientInfo].[Date 1st TEP] IS NULL)
AND
((DateDiff("d",[tbl_NewPatientInfo].[Date 2nd TEP],[Date])<30
AND DateDiff("d",[tbl_NewPatientInfo].[Date 2nd TEP],[Date])>=0)
OR [tbl_NewPatientInfo].[Date 2nd TEP] IS NULL)
AND
((DateDiff("d",[tbl_NewPatientInfo].[Surgery date],[Date])<30
AND DateDiff("d",[tbl_NewPatientInfo].[Surgery date],[Date])>=0)
OR [tbl_NewPatientInfo].[Surgery date] IS NULL);

Note that the parentheses in the above are important to ensure the correct
order of evaluation of the various expressions. Access puts in unnecessary,
albeit harmless, extra parentheses when a query is constructed in design
view. I've included only those which are necessary in the above. The use of
carriage returns on the other hand is purely to enhance the readability of
the SQL, and has no bearing on its execution.

What the above should do is update a row where at least one of the three
criteria falls within the 30 days but none fall outside the 30 days, either
by virtue of falling within the 30 days or the TEP or surgery date not having
a value entered, i.e. being Null. That's the only other interpretation I can
see other than that of your current query, that any of the three criteria
fall within the 30 days.

BTW Date is not a good choice for a field name as it’s the name of a built
in function. Wrapping it in square brackets [Date] when you refer to it
should avoid any confusion, but its nevertheless better to use a more
specifically descriptive field name as you've done for the TEP and surgery
date fields.

Ken Sheridan
Stafford, England

Radhika said:
Sorry for the ambiguity. I want 'ReasonForReferral' to be selected as
'Initial Fit', if the date difference is between 0 and 30. Therefore, I want
to include rows with dates on [Date] and 30 days before [Date] but not later.
I have some TEP and surgery dates that are later than [Date] and they give me
negative values for the date difference, e.g. -1000. I do not want such date
difference values to update the 'ReasonForReferral' field. However, this is
what is happening. I tried replacing >0 with >-1, but that gives me the same
results.

[Date] is the name of a field.

I am not sure about whether there is a problem with my SQL or whether there
is another issue?

Thank you!
Radhika


Ken Sheridan said:
Radhika:

Anything less than 30 is also greater than 0 unless its 0 or less, so I'm
not sure what you mean when you say 'not those >0'.

In effect you are asking with each criterion for rows where the difference
between the TEP or surgery date and the value of [Date] (is this a
parameter?) is between 1 and 30 days, i.e. the TEP or surgery date is less
than 30 days before [Date], but not [Date] or later. If you want to also
include those with dates of [Date] or later, then you don't need to test for
0 at all. If you want to include rows with dates on [Date] but not later,
then test for >-1.

Ken Sheridan
Stafford, England

Radhika said:
I have the following SQL for my update query:

UPDATE tbl_NewPatientInfo INNER JOIN tbl_TEPRecords ON
tbl_NewPatientInfo.[MDACC#] = tbl_TEPRecords.[MDACC#] SET
tbl_NewPatientInfo.[Reason for referral] = "Initial Fit"
WHERE (((DateDiff("d",[tbl_NewPatientInfo].[Date 1st TEP],[Date]))<30 And
(DateDiff("d",[tbl_NewPatientInfo].[Date 1st TEP],[Date]))>0)) OR
(((DateDiff("d",[tbl_NewPatientInfo].[Date 2nd TEP],[Date]))<30 And
(DateDiff("d",[tbl_NewPatientInfo].[Date 2nd TEP],[Date]))>0)) OR
(((DateDiff("d",[tbl_NewPatientInfo].[Surgery date],[Date]))<30 And
(DateDiff("d",[tbl_NewPatientInfo].[Surgery date],[Date]))>0));

For some reason, it is updating only the those <30, but not those >0. What
am i doing wrong?

Thank you,
Radhika
 
R

Radhika

I have figured out what the problem is. My query before I update it looks
something like this:

MDACC# Date Reason for referral Date 1st TEP Date 2nd TEP
Surgery dt
111111 02/02/2008 xxxxx 02/03/2008 05/10/2008
02/01/2008
111111 04/02/2008 xxxxx 02/03/2008 05/10/2008
02/01/2008
And so on for different ID #'s, each having multiple different values for
date.

I want Reason for referral to be Initial visit only if the date difference
between Date 1st TEP and first Date or Date 2nd TEP and first date or Surgery
date and First Date is between 0 and 30.

How do I go about changing the SQL to calculate use the first [Date] value ?

Thank you
Radhika

John Spencer said:
I doubt that using IIF will help. I think your problem is that while
one of the three conditions reports negative numbers one or more of the
other three conditions is returning a value between 1 and 29.

Instead of an update query, try running a select query and see what the
results are

SELECT
DateDiff("d",[tbl_NewPatientInfo].[Date 1st TEP],[Date]) as 1stTep
, DateDiff("d",[tbl_NewPatientInfo].[Date 2nd TEP],[Date]) as 2ndTep
, DateDiff("d",[tbl_NewPatientInfo].[Surgery date],[Date]) as Surg
FROM tbl_NewPatientInfo INNER JOIN tbl_TEPRecords
ON tbl_NewPatientInfo.[MDACC#] = tbl_TEPRecords.[MDACC#]
WHERE DateDiff("d",[tbl_NewPatientInfo].[Date 1st TEP],[Date]) <= 0
And (DateDiff("d",[tbl_NewPatientInfo].[Date 2nd TEP],[Date]) > 0
OR DateDiff("d",[tbl_NewPatientInfo].[Surgery date],[Date]) > 0)

See if you get results where 1st Tep is negative and one of the other is
positive

'====================================================
John Spencer
Access MVP 2002-2005, 2007-2008
The Hilltop Institute
University of Maryland Baltimore County
'====================================================

Thank you!

I tried the SQL you sent me, however, I am still getting -ve values for the
date diff in my results. I also get this when I use the SQL I was trying. I
fail to understand why this is not working.

How would I convert this into an IIf statement. Maybe that would work???
Sincerely,
Radhika


John Spencer said:
UPDATE tbl_NewPatientInfo INNER JOIN tbl_TEPRecords
ON tbl_NewPatientInfo.[MDACC#] = tbl_TEPRecords.[MDACC#]
SET tbl_NewPatientInfo.[Reason for referral] = "Initial Fit"
WHERE
DateDiff("d",[tbl_NewPatientInfo].[Date 1st TEP],[Date])Between 1 and 29
OR DateDiff("d",[tbl_NewPatientInfo].[Date 2nd TEP],[Date])Between 1 and 29
OR DateDiff("d",[tbl_NewPatientInfo].[Surgery date],[Date])Between 1 and 29


That means that Reason For Referral will be set to "Initial Fit" if any of the
3 dates are within 1 to 29 days before [Date]. Is that what you are trying to
accomplish. You original query looks as if it should give the same results.

John Spencer
Access MVP 2002-2005, 2007-2008
The Hilltop Institute
University of Maryland Baltimore County

Radhika wrote:
I have the following SQL for my update query:

UPDATE tbl_NewPatientInfo INNER JOIN tbl_TEPRecords ON
tbl_NewPatientInfo.[MDACC#] = tbl_TEPRecords.[MDACC#] SET
tbl_NewPatientInfo.[Reason for referral] = "Initial Fit"
WHERE (((DateDiff("d",[tbl_NewPatientInfo].[Date 1st TEP],[Date]))<30 And
(DateDiff("d",[tbl_NewPatientInfo].[Date 1st TEP],[Date]))>0)) OR
(((DateDiff("d",[tbl_NewPatientInfo].[Date 2nd TEP],[Date]))<30 And
(DateDiff("d",[tbl_NewPatientInfo].[Date 2nd TEP],[Date]))>0)) OR
(((DateDiff("d",[tbl_NewPatientInfo].[Surgery date],[Date]))<30 And
(DateDiff("d",[tbl_NewPatientInfo].[Surgery date],[Date]))>0));

For some reason, it is updating only the those <30, but not those >0. What
am i doing wrong?

Thank you,
Radhika
 
K

Ken Sheridan

Am I right in thing that [Date] is a column in tbl_TEPRecords? If so try this:

UPDATE tbl_NewPatientInfo
SET [Reason for referral] = "Initial Fit"
WHERE
DATEDIFF("d", [Date 1st TEP],
(SELECT MIN([Date])
FROM tbl_TEPRecords
WHERE tbl_TEPRecords.[MDACC#] = tbl_NewPatientInfo.[MDACC#]))
BETWEEN 0 AND 30
OR
DATEDIFF("d",[Date 2nd TEP],
(SELECT MIN([Date])
FROM tbl_TEPRecords
WHERE tbl_TEPRecords.[MDACC#] = tbl_NewPatientInfo.[MDACC#]))
BETWEEN 0 AND 30
OR
DATEDIFF("d",[Surgery date],
(SELECT MIN([Date])
FROM tbl_TEPRecords
WHERE tbl_TEPRecords.[MDACC#] = tbl_NewPatientInfo.[MDACC#]))
BETWEEN 0 AND 30;

It works by correlating the subqueries on the outer query on the MDACC#
columns. the subquery returns the earliest (MIN) [date] value for the outer
query's current MDACC#, and is used as the third argument of the DateDiff
function to get the difference in days between each TEP date and the surgery
date for the current MDACC# and the earliest [Date]. If any fall within zero
to thirty days then the row in the outer query is updated.

Ken Sheridan
Stafford, England

Radhika said:
I have figured out what the problem is. My query before I update it looks
something like this:

MDACC# Date Reason for referral Date 1st TEP Date 2nd TEP
Surgery dt
111111 02/02/2008 xxxxx 02/03/2008 05/10/2008
02/01/2008
111111 04/02/2008 xxxxx 02/03/2008 05/10/2008
02/01/2008
And so on for different ID #'s, each having multiple different values for
date.

I want Reason for referral to be Initial visit only if the date difference
between Date 1st TEP and first Date or Date 2nd TEP and first date or Surgery
date and First Date is between 0 and 30.

How do I go about changing the SQL to calculate use the first [Date] value ?

Thank you
Radhika

John Spencer said:
I doubt that using IIF will help. I think your problem is that while
one of the three conditions reports negative numbers one or more of the
other three conditions is returning a value between 1 and 29.

Instead of an update query, try running a select query and see what the
results are

SELECT
DateDiff("d",[tbl_NewPatientInfo].[Date 1st TEP],[Date]) as 1stTep
, DateDiff("d",[tbl_NewPatientInfo].[Date 2nd TEP],[Date]) as 2ndTep
, DateDiff("d",[tbl_NewPatientInfo].[Surgery date],[Date]) as Surg
FROM tbl_NewPatientInfo INNER JOIN tbl_TEPRecords
ON tbl_NewPatientInfo.[MDACC#] = tbl_TEPRecords.[MDACC#]
WHERE DateDiff("d",[tbl_NewPatientInfo].[Date 1st TEP],[Date]) <= 0
And (DateDiff("d",[tbl_NewPatientInfo].[Date 2nd TEP],[Date]) > 0
OR DateDiff("d",[tbl_NewPatientInfo].[Surgery date],[Date]) > 0)

See if you get results where 1st Tep is negative and one of the other is
positive

'====================================================
John Spencer
Access MVP 2002-2005, 2007-2008
The Hilltop Institute
University of Maryland Baltimore County
'====================================================

Thank you!

I tried the SQL you sent me, however, I am still getting -ve values for the
date diff in my results. I also get this when I use the SQL I was trying. I
fail to understand why this is not working.

How would I convert this into an IIf statement. Maybe that would work???
Sincerely,
Radhika


:

UPDATE tbl_NewPatientInfo INNER JOIN tbl_TEPRecords
ON tbl_NewPatientInfo.[MDACC#] = tbl_TEPRecords.[MDACC#]
SET tbl_NewPatientInfo.[Reason for referral] = "Initial Fit"
WHERE
DateDiff("d",[tbl_NewPatientInfo].[Date 1st TEP],[Date])Between 1 and 29
OR DateDiff("d",[tbl_NewPatientInfo].[Date 2nd TEP],[Date])Between 1 and 29
OR DateDiff("d",[tbl_NewPatientInfo].[Surgery date],[Date])Between 1 and 29


That means that Reason For Referral will be set to "Initial Fit" if any of the
3 dates are within 1 to 29 days before [Date]. Is that what you are trying to
accomplish. You original query looks as if it should give the same results.

John Spencer
Access MVP 2002-2005, 2007-2008
The Hilltop Institute
University of Maryland Baltimore County

Radhika wrote:
I have the following SQL for my update query:

UPDATE tbl_NewPatientInfo INNER JOIN tbl_TEPRecords ON
tbl_NewPatientInfo.[MDACC#] = tbl_TEPRecords.[MDACC#] SET
tbl_NewPatientInfo.[Reason for referral] = "Initial Fit"
WHERE (((DateDiff("d",[tbl_NewPatientInfo].[Date 1st TEP],[Date]))<30 And
(DateDiff("d",[tbl_NewPatientInfo].[Date 1st TEP],[Date]))>0)) OR
(((DateDiff("d",[tbl_NewPatientInfo].[Date 2nd TEP],[Date]))<30 And
(DateDiff("d",[tbl_NewPatientInfo].[Date 2nd TEP],[Date]))>0)) OR
(((DateDiff("d",[tbl_NewPatientInfo].[Surgery date],[Date]))<30 And
(DateDiff("d",[tbl_NewPatientInfo].[Surgery date],[Date]))>0));

For some reason, it is updating only the those <30, but not those >0. What
am i doing wrong?

Thank you,
Radhika
 
K

Ken Sheridan

Am I right in thing that [Date] is a column in tbl_TEPRecords? If so try this:

UPDATE tbl_NewPatientInfo
SET [Reason for referral] = "Initial Fit"
WHERE
DATEDIFF("d", [Date 1st TEP],
(SELECT MIN([Date])
FROM tbl_TEPRecords
WHERE tbl_TEPRecords.[MDACC#] = tbl_NewPatientInfo.[MDACC#]))
BETWEEN 0 AND 30
OR
DATEDIFF("d",[Date 2nd TEP],
(SELECT MIN([Date])
FROM tbl_TEPRecords
WHERE tbl_TEPRecords.[MDACC#] = tbl_NewPatientInfo.[MDACC#]))
BETWEEN 0 AND 30
OR
DATEDIFF("d",[Surgery date],
(SELECT MIN([Date])
FROM tbl_TEPRecords
WHERE tbl_TEPRecords.[MDACC#] = tbl_NewPatientInfo.[MDACC#]))
BETWEEN 0 AND 30;

It works by correlating the subqueries on the outer query on the MDACC#
columns. the subquery returns the earliest (MIN) [date] value for the outer
query's current MDACC#, and is used as the third argument of the DateDiff
function to get the difference in days between each TEP date and the surgery
date for the current MDACC# and the earliest [Date]. If any fall within zero
to thirty days then the row in the outer query is updated.

Ken Sheridan
Stafford, England

Radhika said:
I have figured out what the problem is. My query before I update it looks
something like this:

MDACC# Date Reason for referral Date 1st TEP Date 2nd TEP
Surgery dt
111111 02/02/2008 xxxxx 02/03/2008 05/10/2008
02/01/2008
111111 04/02/2008 xxxxx 02/03/2008 05/10/2008
02/01/2008
And so on for different ID #'s, each having multiple different values for
date.

I want Reason for referral to be Initial visit only if the date difference
between Date 1st TEP and first Date or Date 2nd TEP and first date or Surgery
date and First Date is between 0 and 30.

How do I go about changing the SQL to calculate use the first [Date] value ?

Thank you
Radhika

John Spencer said:
I doubt that using IIF will help. I think your problem is that while
one of the three conditions reports negative numbers one or more of the
other three conditions is returning a value between 1 and 29.

Instead of an update query, try running a select query and see what the
results are

SELECT
DateDiff("d",[tbl_NewPatientInfo].[Date 1st TEP],[Date]) as 1stTep
, DateDiff("d",[tbl_NewPatientInfo].[Date 2nd TEP],[Date]) as 2ndTep
, DateDiff("d",[tbl_NewPatientInfo].[Surgery date],[Date]) as Surg
FROM tbl_NewPatientInfo INNER JOIN tbl_TEPRecords
ON tbl_NewPatientInfo.[MDACC#] = tbl_TEPRecords.[MDACC#]
WHERE DateDiff("d",[tbl_NewPatientInfo].[Date 1st TEP],[Date]) <= 0
And (DateDiff("d",[tbl_NewPatientInfo].[Date 2nd TEP],[Date]) > 0
OR DateDiff("d",[tbl_NewPatientInfo].[Surgery date],[Date]) > 0)

See if you get results where 1st Tep is negative and one of the other is
positive

'====================================================
John Spencer
Access MVP 2002-2005, 2007-2008
The Hilltop Institute
University of Maryland Baltimore County
'====================================================

Thank you!

I tried the SQL you sent me, however, I am still getting -ve values for the
date diff in my results. I also get this when I use the SQL I was trying. I
fail to understand why this is not working.

How would I convert this into an IIf statement. Maybe that would work???
Sincerely,
Radhika


:

UPDATE tbl_NewPatientInfo INNER JOIN tbl_TEPRecords
ON tbl_NewPatientInfo.[MDACC#] = tbl_TEPRecords.[MDACC#]
SET tbl_NewPatientInfo.[Reason for referral] = "Initial Fit"
WHERE
DateDiff("d",[tbl_NewPatientInfo].[Date 1st TEP],[Date])Between 1 and 29
OR DateDiff("d",[tbl_NewPatientInfo].[Date 2nd TEP],[Date])Between 1 and 29
OR DateDiff("d",[tbl_NewPatientInfo].[Surgery date],[Date])Between 1 and 29


That means that Reason For Referral will be set to "Initial Fit" if any of the
3 dates are within 1 to 29 days before [Date]. Is that what you are trying to
accomplish. You original query looks as if it should give the same results.

John Spencer
Access MVP 2002-2005, 2007-2008
The Hilltop Institute
University of Maryland Baltimore County

Radhika wrote:
I have the following SQL for my update query:

UPDATE tbl_NewPatientInfo INNER JOIN tbl_TEPRecords ON
tbl_NewPatientInfo.[MDACC#] = tbl_TEPRecords.[MDACC#] SET
tbl_NewPatientInfo.[Reason for referral] = "Initial Fit"
WHERE (((DateDiff("d",[tbl_NewPatientInfo].[Date 1st TEP],[Date]))<30 And
(DateDiff("d",[tbl_NewPatientInfo].[Date 1st TEP],[Date]))>0)) OR
(((DateDiff("d",[tbl_NewPatientInfo].[Date 2nd TEP],[Date]))<30 And
(DateDiff("d",[tbl_NewPatientInfo].[Date 2nd TEP],[Date]))>0)) OR
(((DateDiff("d",[tbl_NewPatientInfo].[Surgery date],[Date]))<30 And
(DateDiff("d",[tbl_NewPatientInfo].[Surgery date],[Date]))>0));

For some reason, it is updating only the those <30, but not those >0. What
am i doing wrong?

Thank you,
Radhika
 
R

Radhika

Thank you very much. That worked!

Radhika

Ken Sheridan said:
Am I right in thing that [Date] is a column in tbl_TEPRecords? If so try this:

UPDATE tbl_NewPatientInfo
SET [Reason for referral] = "Initial Fit"
WHERE
DATEDIFF("d", [Date 1st TEP],
(SELECT MIN([Date])
FROM tbl_TEPRecords
WHERE tbl_TEPRecords.[MDACC#] = tbl_NewPatientInfo.[MDACC#]))
BETWEEN 0 AND 30
OR
DATEDIFF("d",[Date 2nd TEP],
(SELECT MIN([Date])
FROM tbl_TEPRecords
WHERE tbl_TEPRecords.[MDACC#] = tbl_NewPatientInfo.[MDACC#]))
BETWEEN 0 AND 30
OR
DATEDIFF("d",[Surgery date],
(SELECT MIN([Date])
FROM tbl_TEPRecords
WHERE tbl_TEPRecords.[MDACC#] = tbl_NewPatientInfo.[MDACC#]))
BETWEEN 0 AND 30;

It works by correlating the subqueries on the outer query on the MDACC#
columns. the subquery returns the earliest (MIN) [date] value for the outer
query's current MDACC#, and is used as the third argument of the DateDiff
function to get the difference in days between each TEP date and the surgery
date for the current MDACC# and the earliest [Date]. If any fall within zero
to thirty days then the row in the outer query is updated.

Ken Sheridan
Stafford, England

Radhika said:
I have figured out what the problem is. My query before I update it looks
something like this:

MDACC# Date Reason for referral Date 1st TEP Date 2nd TEP
Surgery dt
111111 02/02/2008 xxxxx 02/03/2008 05/10/2008
02/01/2008
111111 04/02/2008 xxxxx 02/03/2008 05/10/2008
02/01/2008
And so on for different ID #'s, each having multiple different values for
date.

I want Reason for referral to be Initial visit only if the date difference
between Date 1st TEP and first Date or Date 2nd TEP and first date or Surgery
date and First Date is between 0 and 30.

How do I go about changing the SQL to calculate use the first [Date] value ?

Thank you
Radhika

John Spencer said:
I doubt that using IIF will help. I think your problem is that while
one of the three conditions reports negative numbers one or more of the
other three conditions is returning a value between 1 and 29.

Instead of an update query, try running a select query and see what the
results are

SELECT
DateDiff("d",[tbl_NewPatientInfo].[Date 1st TEP],[Date]) as 1stTep
, DateDiff("d",[tbl_NewPatientInfo].[Date 2nd TEP],[Date]) as 2ndTep
, DateDiff("d",[tbl_NewPatientInfo].[Surgery date],[Date]) as Surg
FROM tbl_NewPatientInfo INNER JOIN tbl_TEPRecords
ON tbl_NewPatientInfo.[MDACC#] = tbl_TEPRecords.[MDACC#]
WHERE DateDiff("d",[tbl_NewPatientInfo].[Date 1st TEP],[Date]) <= 0
And (DateDiff("d",[tbl_NewPatientInfo].[Date 2nd TEP],[Date]) > 0
OR DateDiff("d",[tbl_NewPatientInfo].[Surgery date],[Date]) > 0)

See if you get results where 1st Tep is negative and one of the other is
positive

'====================================================
John Spencer
Access MVP 2002-2005, 2007-2008
The Hilltop Institute
University of Maryland Baltimore County
'====================================================


Radhika wrote:
Thank you!

I tried the SQL you sent me, however, I am still getting -ve values for the
date diff in my results. I also get this when I use the SQL I was trying. I
fail to understand why this is not working.

How would I convert this into an IIf statement. Maybe that would work???
Sincerely,
Radhika


:

UPDATE tbl_NewPatientInfo INNER JOIN tbl_TEPRecords
ON tbl_NewPatientInfo.[MDACC#] = tbl_TEPRecords.[MDACC#]
SET tbl_NewPatientInfo.[Reason for referral] = "Initial Fit"
WHERE
DateDiff("d",[tbl_NewPatientInfo].[Date 1st TEP],[Date])Between 1 and 29
OR DateDiff("d",[tbl_NewPatientInfo].[Date 2nd TEP],[Date])Between 1 and 29
OR DateDiff("d",[tbl_NewPatientInfo].[Surgery date],[Date])Between 1 and 29


That means that Reason For Referral will be set to "Initial Fit" if any of the
3 dates are within 1 to 29 days before [Date]. Is that what you are trying to
accomplish. You original query looks as if it should give the same results.

John Spencer
Access MVP 2002-2005, 2007-2008
The Hilltop Institute
University of Maryland Baltimore County

Radhika wrote:
I have the following SQL for my update query:

UPDATE tbl_NewPatientInfo INNER JOIN tbl_TEPRecords ON
tbl_NewPatientInfo.[MDACC#] = tbl_TEPRecords.[MDACC#] SET
tbl_NewPatientInfo.[Reason for referral] = "Initial Fit"
WHERE (((DateDiff("d",[tbl_NewPatientInfo].[Date 1st TEP],[Date]))<30 And
(DateDiff("d",[tbl_NewPatientInfo].[Date 1st TEP],[Date]))>0)) OR
(((DateDiff("d",[tbl_NewPatientInfo].[Date 2nd TEP],[Date]))<30 And
(DateDiff("d",[tbl_NewPatientInfo].[Date 2nd TEP],[Date]))>0)) OR
(((DateDiff("d",[tbl_NewPatientInfo].[Surgery date],[Date]))<30 And
(DateDiff("d",[tbl_NewPatientInfo].[Surgery date],[Date]))>0));

For some reason, it is updating only the those <30, but not those >0. What
am i doing wrong?

Thank you,
Radhika
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top