473,795 Members | 2,968 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7294
On Sat, 10 Apr 2004 20:33:59 GMT, Salad <oi*@vinegar.co m> 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.co m> 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.co m> wrote in
news:2e******** *************** *********@4ax.c om:
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(NumberOfThi ngys) as MaxNumThingys, Date
FROM tbl_Thingys
WHERE Date = '4/1/2004'
GROUP BY Date

versus this:

SELECT Max(NumberOfThi ngys) 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.co m> wrote in message
news:2e******** *************** *********@4ax.c om...

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
15752
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. Presently we get a "Page has expired" message. How can we avoid this? Full details: Having searched for postings on how to avoid the "Page has Expired" they are
6
5271
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 all querying is done locally. One of the reports we run allows the user to list all invoices within a period. They are also allowed to select a customer code and a product set on
5
1036
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 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
4
2441
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 example: field1 field2 field3 field4 one 1 0 0 two 2 0 3 three 0 0 0 my query should only get entries one and two. can somebody show me
5
10882
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 new database. For instance, her old database has a table with Band Info in it. Her new database also has a table with Band Info in it but slightly different. I was wondering if there was an easy way to compare the fields from similar tables in...
3
1809
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 of which are: TableMembers: (containg fields Refs, DateCreated, MembershipNo, OfficeLocation ...NB: Refs has a Primary Key - No Duplicates) TablePeople: (containing fields: Refs, Name, Addr, TelHome, TelWork, TelMobile & TelFax)...
1
2288
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 that have null and non null fields (see below). CustNo Funding ClaimAmt Prod 123 R 100 PPO 123 R 200 Null Value
2
1723
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 populated just fine. However, when I add a new record using the new record control at the bottom of the subform, it gets added with NULL for the two values that were in the original WHERE clause. For example, if my query was "SELECT batch, style,...
3
2106
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 when I pass a class as a parameter to a Windows Form. Basically, I have a class that has several fields, two of these fields are an instance of an inner class, the rest are basic value types (bool's in this case). I have a windows form, the...
3
4710
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 are found.
0
9672
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9519
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10439
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10215
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10001
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5437
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4113
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
2
3727
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.