473,394 Members | 1,750 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,394 software developers and data experts.

Warning. Min/Max use on fields with criteria

This may occur to you in the future. You want to get the min or max of
a field that you set criteria on...maybe for a combo box. Let's say you
wanted to get the minimum of all date fields where the date field is in
the future. If you use the query builder and don't modify the results
you end up with zero records. Ex.

SELECT Min(DateField) AS MinDate
FROM Table
HAVING Min(DateField) > Date()+1

The above is code generated, minus all the ()'s, from the query builder.

If you really have the min or max records beyond today, you need to
manually modify the query to change the word Having to Where

SELECT Min(DateField) AS MinDate
FROM Table
Where DateField > Date() + 1
Nov 12 '05 #1
5 7260
On Sat, 10 Apr 2004 20:33:59 GMT, Salad <oi*@vinegar.com> wrote:

You can achieve the same by adding another DateField column in the
query designer, rather than adding the criteria to the first DateField
field.

-Tom.

This may occur to you in the future. You want to get the min or max of
a field that you set criteria on...maybe for a combo box. Let's say you
wanted to get the minimum of all date fields where the date field is in
the future. If you use the query builder and don't modify the results
you end up with zero records. Ex.

SELECT Min(DateField) AS MinDate
FROM Table
HAVING Min(DateField) > Date()+1

The above is code generated, minus all the ()'s, from the query builder.

If you really have the min or max records beyond today, you need to
manually modify the query to change the word Having to Where

SELECT Min(DateField) AS MinDate
FROM Table
Where DateField > Date() + 1


Nov 12 '05 #2
A little additional info that might help someone out there:

In a query that has no aggregation (GROUP BY, MIN, MAX, etc), the
criteria in the WHERE clause are applied to the records at the time of
selection.

But in a query *with* aggregation, there are two options for how you
apply criteria: at the time of selection (WHERE clause) or after
grouping (HAVING clause).

As in Salad's example, the incorrect application of criteria (WHERE vs
HAVING) can have an effect on the result set of the query.

In other cases, the result set could be the same, but the
performance can be significantly affected. For instance, if you want
the Max of some value on a specific date, applying the date criterion
in the WHERE clause will weed out all but the date in which you're
interested prior to the application of the Max function. But if you
apply the date criterion in the HAVING clause, your query will select
*all* dates and calculate the Max value for each date, before
applying the date criterion and showing you only the date you're
interested in. (Unless Access/Jet has some kind of SQL optimization
built in of which I'm not aware.)

This is, IMHO, one of the places in Access where the GUI is a little
too user-friendly, and hides something significant from the user.

-Matt

On Sat, 10 Apr 2004 20:33:59 GMT, Salad <oi*@vinegar.com> wrote:
If you really have the min or max records beyond today, you need to
manually modify the query to change the word Having to Where


Nov 12 '05 #3
Matthew Sullivan <Ma**@NoSpam.com> wrote in
news:2e********************************@4ax.com:
A little additional info that might help someone out there:

In a query that has no aggregation (GROUP BY, MIN, MAX, etc), the
criteria in the WHERE clause are applied to the records at the time of
selection.

But in a query *with* aggregation, there are two options for how you
apply criteria: at the time of selection (WHERE clause) or after
grouping (HAVING clause).

As in Salad's example, the incorrect application of criteria (WHERE vs
HAVING) can have an effect on the result set of the query.

In other cases, the result set could be the same, but the
performance can be significantly affected. For instance, if you want
the Max of some value on a specific date, applying the date criterion
in the WHERE clause will weed out all but the date in which you're
interested prior to the application of the Max function. But if you
apply the date criterion in the HAVING clause, your query will select
*all* dates and calculate the Max value for each date, before
applying the date criterion and showing you only the date you're
interested in. (Unless Access/Jet has some kind of SQL optimization
built in of which I'm not aware.)

This is, IMHO, one of the places in Access where the GUI is a little
too user-friendly, and hides something significant from the user.


The original poster specified two entirely separate and distinct criteria in
his cirterion clause.

HAVING Min(DateField) > Date()+1

and

Where DateField > Date() + 1

Of course "having" scans the aggregations in this case
(and "where" does not).

What is you reason for thinking
"having" will scan the aggregations in

HAVING DateField > Date()+1

?????

--
Lyle
(for e-mail refer to http://ffdba.com/contacts.htm)
Nov 12 '05 #4
Lyle:

I wasn't intending to say anything about that syntax in particular
(offhand, I don't even know whether it's valid).

My apologies if I wrote poorly.

When I was talking about applying the same criterion in either the
WHERE or HAVING and getting the same result set, I meant an example
something like this (note: SQL Server sytnax):

SELECT Max(NumberOfThingys) as MaxNumThingys, Date
FROM tbl_Thingys
WHERE Date = '4/1/2004'
GROUP BY Date

versus this:

SELECT Max(NumberOfThingys) as MaxNumThingys, Date
FROM tbl_Thingys
GROUP BY Date
HAVING Date = '4/1/2004'

-Matt

On 11 Apr 2004 22:40:04 GMT, Lyle Fairfield
<Mi************@Invalid.Com> wrote:
What is you reason for thinking
"having" will scan the aggregations in

HAVING DateField > Date()+1

?????


Nov 12 '05 #5
"Matthew Sullivan" <Ma**@NoSpam.com> wrote in message
news:2e********************************@4ax.com...

This is, IMHO, one of the places in Access where the GUI is a little
too user-friendly, and hides something significant from the user.


Saying these sorts of things round here is generally considered to be
promoting heresy <g>

Nov 12 '05 #6

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

Similar topics

16
by: Dave Smithz | last post by:
Hi, In summary: I want to a form to submit information via a HTTP POST, however, when using Internet Explorer I want to be able to use the back button and all the information retained....
6
by: AAVF | last post by:
Hi We have a problem with a query. An Access database links via ODBC to a UNIX server. To speed things, we use the ODBC to load the relevant tables to the local PC that runs Access so that...
5
by: Salad | last post by:
This may occur to you in the future. You want to get the min or max of a field that you set criteria on...maybe for a combo box. Let's say you wanted to get the minimum of all date fields where...
4
by: nick_faye | last post by:
hi, hope someone can help me. i am a newbie in creating queries and i want to create a query wherein i only get entries from my table where values of fields 2, 3 and 4 are not zeros. for...
5
by: Megan | last post by:
Hi everybody- I'm helping a friend with a music database. She has an old one and is creating a new one. She wants to compare records and fields in the old database with records and fields in the...
3
by: Jan Szymczuk | last post by:
I am trying to create a query that will show me who is phoning who in an organisation from available Telephone Billing information. I am creating a MSAccess 2000 database with a few few tables, two...
1
by: RiesbeckP | last post by:
Hi All, I have a DB where there are customer numbers and a few other fields. I want to be able to pull all of the null records for a particular field as well as all the other customer numbers...
2
by: JM | last post by:
Hello, I've created a Querydef in a Form_Load() sub. The form is a subform that no longer has linked child fields. The form is bound to this querydef. When I open the form, the fields are...
3
by: Ross McLean | last post by:
Hi all, I've been teaching myself C# for a new project at work. I have a bit of a background in c++ and java but never been what you could call a guru. I'm having some strange things happening...
3
by: Simon | last post by:
Dear reader, I found out a strange behaviour in a query of the type Total (summation query). In case of a normal select query with a criteria setting Is Null for field-A, four (4) records...
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: 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...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.