473,385 Members | 1,834 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,385 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 17202
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...

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.