472,789 Members | 862 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Need SQL Query to determine max value from fields in a row

Hello out there;

This may be a challenge but I'm certain it's possible but I can't seem
to figure out how.

I have a table that has several date fields, e.g., Date1, Date2,
Date3, Date4 ... etc. I wish to find out either of two things for
each row.

1) What is the maximun date in any of the date fields in the row.

OR

2) Which field in the row contains the most recent date.

TIA

Bill
Nov 12 '05 #1
8 17148
Bill wrote:
Hello out there;

This may be a challenge but I'm certain it's possible but I can't seem
to figure out how.

I have a table that has several date fields, e.g., Date1, Date2,
Date3, Date4 ... etc. I wish to find out either of two things for
each row.

1) What is the maximun date in any of the date fields in the row.

OR

2) Which field in the row contains the most recent date.

TIA

Bill


You could write a function. Ex:
Expr1 : GetEarliestDate([Date1],[Date2],[Date3],[Date4])
Expr2 : GetRecentDateDate([Date1],[Date2],[Date3],[Date4])

Private Function GetEarliesDate( d1 As Date....)
If d1 < d2 And D1 < d3 And d1 < d4 then
GetEarliesDate = D1
Else if d2 < d1 And d2 < d3 and d2 < d4 then
...
End Function

Or you could right a hell-atious IIF() statement.
Nov 12 '05 #2
CDB
First thought - any time such a problem arises, check the data schema for
improved normalisation.

Second, if that is not appropriate, or possible, create a temporary
normalised table (vertical slices) using a Union subquery:

SELECT A.RowID, Max(A.Date1) AS Latest
FROM [SELECT R.RowID, R.Date1 FROM RowTable AS R
UNION ALL SELECT R.RowID, R.Date2 FROM RowTable AS R
UNION ALL SELECT R.RowID, R.Date3 FROM RowTable AS R
]. AS A
GROUP BY A.RowID;

Clive
"Bill" <va****@rac.ca> wrote in message
news:68**************************@posting.google.c om...
Hello out there;

This may be a challenge but I'm certain it's possible but I can't seem
to figure out how.

I have a table that has several date fields, e.g., Date1, Date2,
Date3, Date4 ... etc. I wish to find out either of two things for
each row.

1) What is the maximun date in any of the date fields in the row.

OR

2) Which field in the row contains the most recent date.

TIA

Bill

Nov 12 '05 #3
Salad <oi*@vinegar.com> wrote in
news:N4****************@newsread2.news.pas.earthli nk.net:
Bill wrote:
Hello out there;

This may be a challenge but I'm certain it's possible but I can't
seem to figure out how.

I have a table that has several date fields, e.g., Date1, Date2,
Date3, Date4 ... etc. I wish to find out either of two things
for each row.

1) What is the maximun date in any of the date fields in the row.
OR

2) Which field in the row contains the most recent date.


You could write a function. Ex:
Expr1 : GetEarliestDate([Date1],[Date2],[Date3],[Date4])
Expr2 : GetRecentDateDate([Date1],[Date2],[Date3],[Date4])

Private Function GetEarliesDate( d1 As Date....)
If d1 < d2 And D1 < d3 And d1 < d4 then
GetEarliesDate = D1
Else if d2 < d1 And d2 < d3 and d2 < d4 then
...
End Function

Or you could right a hell-atious IIF() statement.


If you really wanted the function to handle many dates, you could
use a parameter array, and then walk the array to check which one
was the max. Check the Help for PARAMETER ARRAY to figure it out.

But it sounds like a probably denormalized structure, though I've
certainly seen situations where you wanted to return the latest of a
handful of independent dates. I've seldom seen more than two or
three, and for those, I've always used my iMax()/iMin() functions.
Here's iMax:

Public Function iMax(X As Long, y As Long)
If X > y Then
iMax = X
Else
iMax = y
End If
End Function

Actually, in looking this up, I see that Trevor Best has already
done the parameter array work for you:

Public Function iMax(ParamArray p()) As Variant
' Idea from Trevor Best in Usenet MessageID
' ri********************************@4ax.com
Dim i As Long
Dim v As Variant

v = p(LBound(p))
For i = LBound(p) + 1 To UBound(p)
If v < p(i) Then
v = p(i)
End If
Next
iMax = v
End Function

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 12 '05 #4
David W. Fenton wrote:
Salad <oi*@vinegar.com> wrote in
news:N4****************@newsread2.news.pas.earthli nk.net:

Bill wrote:
Hello out there;

This may be a challenge but I'm certain it's possible but I can't
seem to figure out how.

I have a table that has several date fields, e.g., Date1, Date2,
Date3, Date4 ... etc. I wish to find out either of two things
for each row.

1) What is the maximun date in any of the date fields in the row.
OR

2) Which field in the row contains the most recent date.


You could write a function. Ex:
Expr1 : GetEarliestDate([Date1],[Date2],[Date3],[Date4])
Expr2 : GetRecentDateDate([Date1],[Date2],[Date3],[Date4])

Private Function GetEarliesDate( d1 As Date....)
If d1 < d2 And D1 < d3 And d1 < d4 then
GetEarliesDate = D1
Else if d2 < d1 And d2 < d3 and d2 < d4 then
...
End Function

Or you could right a hell-atious IIF() statement.

If you really wanted the function to handle many dates, you could
use a parameter array, and then walk the array to check which one
was the max. Check the Help for PARAMETER ARRAY to figure it out.


I understood the problem to be there are many roww of data. Each row
has 4 dates. Out of those 4 dates he wanted the min and max dates for
each row.

I'm not sure how a parameter array would assist in this case. When
calling a function in a query, you need to pass the arguments of that
row withing the query or perhaps the key so the function can waht to
process.

I considered a union query but I thought he might want the two results
on the same returned row.

Nov 12 '05 #5
Salad <oi*@vinegar.com> wrote in
news:RL*****************@newsread2.news.pas.earthl ink.net:
David W. Fenton wrote:
Salad <oi*@vinegar.com> wrote in
news:N4****************@newsread2.news.pas.earthli nk.net:

Bill wrote:

Hello out there;

This may be a challenge but I'm certain it's possible but I
can't seem to figure out how.

I have a table that has several date fields, e.g., Date1, Date2,
Date3, Date4 ... etc. I wish to find out either of two things
for each row.

1) What is the maximun date in any of the date fields in the
row.
OR

2) Which field in the row contains the most recent date.

You could write a function. Ex:
Expr1 : GetEarliestDate([Date1],[Date2],[Date3],[Date4])
Expr2 : GetRecentDateDate([Date1],[Date2],[Date3],[Date4])

Private Function GetEarliesDate( d1 As Date....)
If d1 < d2 And D1 < d3 And d1 < d4 then
GetEarliesDate = D1
Else if d2 < d1 And d2 < d3 and d2 < d4 then
...
End Function

Or you could right a hell-atious IIF() statement.


If you really wanted the function to handle many dates, you could
use a parameter array, and then walk the array to check which one
was the max. Check the Help for PARAMETER ARRAY to figure it out.


I understood the problem to be there are many roww of data. Each
row has 4 dates. Out of those 4 dates he wanted the min and max
dates for each row.

I'm not sure how a parameter array would assist in this case.
When calling a function in a query, you need to pass the arguments
of that row withing the query or perhaps the key so the function
can waht to process.

I considered a union query but I thought he might want the two
results on the same returned row.


Did you try out the function I posted?

What you want is a function that will return the lowest date:

GetLowestDate(Field1, Field2, Field3, Field4, Field5...)

The function I posted will give you that, and it uses a parameter
array so that you can pass it 2 or more arguments and return the
minimum/maximum of all the values you pass it.

Try it -- you'll see that it does exactly what is needed here with
no IIf() and no UNION.

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 12 '05 #6
CDB
Why use an added-on code manager in a Relational Database when "native" SQL
will do the job?

Clive

"David W. Fenton" <dX********@bway.net.invalid> wrote in message
news:Xn**********************************@24.168.1 28.74...
Salad <oi*@vinegar.com> wrote in
news:RL*****************@newsread2.news.pas.earthl ink.net:
David W. Fenton wrote:
Salad <oi*@vinegar.com> wrote in
news:N4****************@newsread2.news.pas.earthli nk.net:
Bill wrote:

>Hello out there;
>
>This may be a challenge but I'm certain it's possible but I
>can't seem to figure out how.
>
>I have a table that has several date fields, e.g., Date1, Date2,
>Date3, Date4 ... etc. I wish to find out either of two things
>for each row.
>
>1) What is the maximun date in any of the date fields in the
>row.
>
>
>OR
>
>2) Which field in the row contains the most recent date.

You could write a function. Ex:
Expr1 : GetEarliestDate([Date1],[Date2],[Date3],[Date4])
Expr2 : GetRecentDateDate([Date1],[Date2],[Date3],[Date4])

Private Function GetEarliesDate( d1 As Date....)
If d1 < d2 And D1 < d3 And d1 < d4 then
GetEarliesDate = D1
Else if d2 < d1 And d2 < d3 and d2 < d4 then
...
End Function

Or you could right a hell-atious IIF() statement.

If you really wanted the function to handle many dates, you could
use a parameter array, and then walk the array to check which one
was the max. Check the Help for PARAMETER ARRAY to figure it out.


I understood the problem to be there are many roww of data. Each
row has 4 dates. Out of those 4 dates he wanted the min and max
dates for each row.

I'm not sure how a parameter array would assist in this case.
When calling a function in a query, you need to pass the arguments
of that row withing the query or perhaps the key so the function
can waht to process.

I considered a union query but I thought he might want the two
results on the same returned row.


Did you try out the function I posted?

What you want is a function that will return the lowest date:

GetLowestDate(Field1, Field2, Field3, Field4, Field5...)

The function I posted will give you that, and it uses a parameter
array so that you can pass it 2 or more arguments and return the
minimum/maximum of all the values you pass it.

Try it -- you'll see that it does exactly what is needed here with
no IIf() and no UNION.

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc

Nov 12 '05 #7
"CDB" <al***@delete.wave.co.nz> wrote in
news:c7**********@news.wave.co.nz:
"David W. Fenton" <dX********@bway.net.invalid> wrote in message
news:Xn**********************************@24.168.1 28.74...
Salad <oi*@vinegar.com> wrote in
news:RL*****************@newsread2.news.pas.earthl ink.net:
> David W. Fenton wrote:
>
>> Salad <oi*@vinegar.com> wrote in
>> news:N4****************@newsread2.news.pas.earthli nk.net:
>>
>>
>>>Bill wrote:
>>>
>>>>Hello out there;
>>>>
>>>>This may be a challenge but I'm certain it's possible but I
>>>>can't seem to figure out how.
>>>>
>>>>I have a table that has several date fields, e.g., Date1,
>>>>Date2, Date3, Date4 ... etc. I wish to find out either of
>>>>two things for each row.
>>>>
>>>>1) What is the maximun date in any of the date fields in the
>>>>row.
>>>>
>>>>
>>>>OR
>>>>
>>>>2) Which field in the row contains the most recent date.
>>>
>>>You could write a function. Ex:
>>> Expr1 : GetEarliestDate([Date1],[Date2],[Date3],[Date4])
>>> Expr2 :
>>> GetRecentDateDate([Date1],[Date2],[Date3],[Date4])
>>>
>>>Private Function GetEarliesDate( d1 As Date....)
>>> If d1 < d2 And D1 < d3 And d1 < d4 then
>>> GetEarliesDate = D1
>>> Else if d2 < d1 And d2 < d3 and d2 < d4 then
>>> ...
>>>End Function
>>>
>>>Or you could right a hell-atious IIF() statement.
>>
>> If you really wanted the function to handle many dates, you
>> could use a parameter array, and then walk the array to check
>> which one was the max. Check the Help for PARAMETER ARRAY to
>> figure it out.
>
> I understood the problem to be there are many roww of data.
> Each row has 4 dates. Out of those 4 dates he wanted the min
> and max dates for each row.
>
> I'm not sure how a parameter array would assist in this case.
> When calling a function in a query, you need to pass the
> arguments of that row withing the query or perhaps the key so
> the function can waht to process.
>
> I considered a union query but I thought he might want the two
> results on the same returned row.


Did you try out the function I posted?

What you want is a function that will return the lowest date:

GetLowestDate(Field1, Field2, Field3, Field4, Field5...)

The function I posted will give you that, and it uses a parameter
array so that you can pass it 2 or more arguments and return the
minimum/maximum of all the values you pass it.

Try it -- you'll see that it does exactly what is needed here
with no IIf() and no UNION.


Why use an added-on code manager in a Relational Database when
"native" SQL will do the job?


Because it's faster?

Now, that might depend on circumstances. If you write your UNION
correctly, it will be a UNION ALL, and those can be very fast, but
they still don't use indexes the same way as non-UNIONs do.

Also, if the result needs to be editable, the UNION won't do the job
(unless you use the UNION as the basis for an elaborate subquery).

Which approach is best depends entirely on what you're trying to
accomplish.

I would definitely say that the use of a UNION suggests to me that
the data is badly denormalized, since the UNIONing your fields into
a single column is a renormalization of multiple columns into a
single one against which you can apply criteria/transformations.

But if you're just wanting the lesser or greater of a handful of
fields for a form display or for a check in a loaded record, a UNION
makes no sense to me.

Let me explain that. I can think of an app of mine where there were
three dates that were independent pieces of information that were
put in a single row (they were truly independent attributes of the
entity being modelled, not three instances of a child entity). When
you were adding child records to the main record, you had to check
that the date of the child record was not greater than the Max() of
the three fields in that record. A UNION on a single record would be
incredible overkill, but the parameter array iMax() function would
make a great deal of sense.

The more records you're processing to get the answer, the more
likely it is that the UNION will be preferrable.

But if you're constantly processing large numbers of records to find
this out, then I'd suggest this vastly increases the likelihood that
your schema is denormalized.

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 12 '05 #8
"CDB" <al***@delete.wave.co.nz> wrote in message news:<c7**********@news.wave.co.nz>...
First thought - any time such a problem arises, check the data schema for
improved normalisation.

Second, if that is not appropriate, or possible, create a temporary
normalised table (vertical slices) using a Union subquery:

SELECT A.RowID, Max(A.Date1) AS Latest
FROM [SELECT R.RowID, R.Date1 FROM RowTable AS R
UNION ALL SELECT R.RowID, R.Date2 FROM RowTable AS R
UNION ALL SELECT R.RowID, R.Date3 FROM RowTable AS R
]. AS A
GROUP BY A.RowID;

Clive
"Bill" <va****@rac.ca> wrote in message
news:68**************************@posting.google.c om...
Hello out there;

This may be a challenge but I'm certain it's possible but I can't seem
to figure out how.

I have a table that has several date fields, e.g., Date1, Date2,
Date3, Date4 ... etc. I wish to find out either of two things for
each row.

1) What is the maximun date in any of the date fields in the row.

OR

2) Which field in the row contains the most recent date.

TIA

Bill


Thanks to those who helped.

I'll take the three approaches to the office tomorrow and see if I can
implement any of the approaches. Just a beginner.

A word of explanation wrt normalization the table deals with a
tracking progress of a document that goes through a number of steps,
not necessarily in order but usually and there is a limited number of
possible events, six in this case. I would have been possible to
track them in a separate table with three columns document, event, and
date. However I chose the easier route with six columns one per date.

There are not a great many rows perhaps several hundred. About the
development environment. The application is a web-based interface for
both input update and review of the records. I'm working in with HTML
and ASP and MS Access database as well as bits of vbscript &
JavaScript. So implementing some of the more advance approaches such
as the parameter array will be pushing my envelope. But after all
that's were the fun is.

Thanks once again for your assistance. I certainly appreciate the
ideas and effort it takes to provide the assistance.

Thank you.

Bill
Nov 12 '05 #9

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

Similar topics

10
by: Mark | last post by:
I have a table about people containing 25 fields. The table contains the usual fields - first, last, address, city, state and zip. There is no primary key. These fields all have data with the...
3
by: google | last post by:
I have a database with four table. In one of the tables, I use about five lookup fields to get populate their dropdown list. I have read that lookup fields are really bad and may cause problems...
2
by: Dixie | last post by:
I have a table of records, many of which are repeated or differ in 4 of the 6 fields only. I want to show only those records on a continuous form that are unique in the first 2 fields. That is as...
3
by: MLH | last post by:
I have a query, qryAppend30DayOld260ies that attempts to append records to tblCorrespondence. When run, it can result in any of the following: appending no records, appending 1 record or appending...
2
by: NigelMThomas | last post by:
I have an especially challenging problem. I know there are a few geniuses in this group so perhaps; you can advise me whether or not this can be done as an update query in Access. Thanks. I am...
4
by: Tom | last post by:
An order may be rescheduled to ship multiple times. The tables look like: TblOrder OrderID <order fields> TblOrderShip OrderShipID OrderID ShipDate ReasonNotShipped
8
by: No bother | last post by:
I have a table with two columns, one named master, the other slave. Each column has a set of numbers. A number in one column can appear in the other. I am trying to see if there is any infinite...
5
by: talktozee | last post by:
Hello, everyone! Here's are the basics: 1. The query looks at all positions that are active and haven't been filled. 2. It then has to look at every single position and determine three...
1
by: Donald Calloway | last post by:
I have created a database application that uses a macro, activated by a button click event, that runs a query which returns a recordset consisting of a subset of the matching database record, and...
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.