473,467 Members | 1,680 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to limit MIN(DateValue) with GROUP BY in query?

How do I limit the results of this query to return only the NextAppt and the
corresponding Subject?

SELECT [Subject], MIN([ApptDate]) As NextAppt
FROM tblAppointments
WHERE ((tblAppointments.Entity_ID=QryPrm("frm0", "Entity_ID")) AND
(tblAppointments.ApptDate>=Date()))
GROUP BY [Subject];

The problem with this is that it returns more than one value. If
tblAppointments has:

[ApptDate] [Subject]
2/10/2005 meetingA
2/11/2005 meetingB
2/12/2005 meetingC

I am getting is every record that satisfies the WHERE clause.

What I want is:

2/10/2005 meetingA

Thanks in advance.
Nov 13 '05 #1
2 2907
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

What's 'QryPrm("frm0", "Entity_ID")'? is it a call to a function? Does
the function read the Control "Entity_ID" on the form "frm0"? If so,
use the query PARAMETERS instead:

PARAMETERS Forms!frm0!Entity_ID Long;
SELECT [Subject], MIN([ApptDate]) As NextAppt
FROM tblAppointments
WHERE ((tblAppointments.Entity_ID=Forms!frm0!Entity_ID ) AND
(tblAppointments.ApptDate>=Date()))
GROUP BY [Subject];

If that Entity_ID is a different data type than Long, change it
accordingly.

If that Entity_ID on the form has more than one Subject, there will be
as many rows returned as there are subjects.

Do you want the first NextAppt or the last NextAppt. For the first:
use MIN(NextAppt). For the last: Max(NextAppt).

--
MGFoster:::mgf00 <at> earthlink <decimal-point> net
Oakland, CA (USA)

-----BEGIN PGP SIGNATURE-----
Version: PGP for Personal Privacy 5.0
Charset: noconv

iQA/AwUBQeiIxIechKqOuFEgEQJi+gCgs8VnS6gSEd0DVjgDwAp5tx DF2JEAn0mp
uy4oefXoz7cHZFIN6ig9xf3j
=dJkJ
-----END PGP SIGNATURE-----
deko wrote:
How do I limit the results of this query to return only the NextAppt and the
corresponding Subject?

SELECT [Subject], MIN([ApptDate]) As NextAppt
FROM tblAppointments
WHERE ((tblAppointments.Entity_ID=QryPrm("frm0", "Entity_ID")) AND
(tblAppointments.ApptDate>=Date()))
GROUP BY [Subject];

The problem with this is that it returns more than one value. If
tblAppointments has:

[ApptDate] [Subject]
2/10/2005 meetingA
2/11/2005 meetingB
2/12/2005 meetingC

I am getting is every record that satisfies the WHERE clause.

What I want is:

2/10/2005 meetingA

Nov 13 '05 #2
Hi and thanks for the reply.
What's 'QryPrm("frm0", "Entity_ID")'? is it a call to a function? Does
the function read the Control "Entity_ID" on the form "frm0"?
Yes and yes. Here is the function:

Public Function QryPrm(ByVal strFrm As String, ByVal strCtl As String,
Optional ByVal strSubFrm As String) As Variant
If Len(strSubFrm) = 0 Then
QryPrm = Forms(strFrm).Controls(strCtl)
Else
QryPrm = Forms(strFrm).Controls(strCtl).Form.Controls(strSu bFrm)
End If
End Function
If so, use the query PARAMETERS instead:

PARAMETERS Forms!frm0!Entity_ID Long;
SELECT [Subject], MIN([ApptDate]) As NextAppt
FROM tblAppointments
WHERE ((tblAppointments.Entity_ID=Forms!frm0!Entity_ID ) AND
(tblAppointments.ApptDate>=Date()))
GROUP BY [Subject];


I thought it was better to supply query parameters with a function as I am
doing. One advantage of using a function to supply parameters is that I can
build a recordset from my queries like this:

Set rst = db.OpenRecordset("qryMyQuery")

which I cannot do if I write the query with a reference to a control on a
form.

I looked up Parameter in Help: "Parameter objects allow you to change the
arguments in a frequently run QueryDef object without having to recompile
the query."

Since my query is a stored query, I'm not sure PARAMETER is what I want. In
any case, regardless of how I supply the parameter, I still don't get the
one, first NextAppt I am looking for. Am I missing something?
Nov 13 '05 #3

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

Similar topics

2
by: Dom | last post by:
I need to run a query based on a query in Access. The second query has a number of conditions which all work well, but there is one more contition I need to set to make it run properly. the...
2
by: someone | last post by:
How can I limit the number of records a Query retrieves or at least limit the number of records returned on a report. I only want to print the top 5 or 10 returns of a query into a weekly reort....
5
by: - | last post by:
I have only 1m records in my database running on a laptop of speed 1.6GHz, memory 512MB ram, and Toshiba MK8032GAX hard disk. I use 'LIMIT x,10' for the query to utilise record paging. When the...
3
by: rcamarda | last post by:
Hello, I ran a query that I thought would take an hour, but instead took 14 hours to run. The consequence was it bogged down our data warehouse and the overnight build was adversely impacted. Is...
2
by: milo1955 | last post by:
I have a form that passes a single variable via the GET function to a mysql databse query with the results paginated. The first page of the results work fine, but the link to the second page yields...
1
by: DAHMB | last post by:
I have a column in a report that I want to return a sum of certain criteria from a query between two dates. I set the dates in a form that I launch the report from. My query contains a field called...
3
by: DAHMB | last post by:
I have a report called rptCarSheet based on a Query called qryCombinedCarAbsentee. In the report I have the following fields , , , , I would like to limit the fields to only show the value if the ...
11
by: Mysterydave | last post by:
Hi, I have a table which will have records appended to it weekly. In my append query I have a field to identify when it was appended using Now(). Now what I want to do is delete the identical...
0
by: mjames777 | last post by:
any one can help me to get correct with min query to get All matching experience in all the category mentioned in condition. If my table structure like... Field Type...
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
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,...
1
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...
0
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...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.