Hi Experts and Fellow Duffers:
I am trying to create a query which will identify the most recent EndDate in tblSchedules associated with tblKids.KidID where a Null value (indicating a current schedule) is the most recent.
Max(EndDate) doesn't recognize Null as the highest value. Last(EndDate) returns the most recently entered schedule but not the most recent EndDate.
Any suggestions? My query so far: - SELECT tblKids.KidID, Min(tblSchedules.StartDate) AS OriginalStart, Max(tblSchedules.EndDate) AS FinalEnd
-
FROM tblKids INNER JOIN tblSchedules ON (tblKids.KidID = tblSchedules.KidID) AND (tblKids.KidID = tblSchedules.KidID) AND (tblKids.KidID = tblSchedules.KidID)
-
GROUP BY tblKids.KidID;
-
Thanks,
Bradley
5 6291
Try ORDER BY tblKids.EndDate...
Welcome to the Scripts!
Regards,
Scott
When you see Null as the highest enddate, you should use the NZ() function e.g. like: -
Max(Nz(EndDate,#01-01-2999#))
-
Getting the idea ?
Nic;o)
Thank you both you both for you replies. Both the ORDER BY and Nz() suggestions were good but occurred after the query evaluated Max() so I was still left with the max non-null date (which disregards current schedules).
After playing, I got to the following partial solution which uses iif (instead on Nz) to create a "FakeEnd" for Null rows and an additional field to put them back to Null (which would be the one field I would use.) Technically it works but prompts the user for a "FakeEnd" parameter before figuring it out which really isn't going to work practically.
I have a thought that a subquery (to make FakeEnd) might be appropriate here but I've reached the limit of my expertise with regard to queries, let alone subqueries. If either/any of you think a subquery might work, what would it look like? - SELECT tblKids.KidID, Min(tblSchedules.StartDate) AS OriginalStart, Max(IIf(IsNull([tblSchedules].[EndDate]),#12/31/9999#,[tblSchedules].[EndDate])) AS FakeEnd, IIf([FakeEnd]=#12/31/9999#,Null,[FakeEnd]) AS FinalEnd
-
FROM tblKids INNER JOIN tblSchedules ON (tblKids.KidID = tblSchedules.KidID) AND (tblKids.KidID = tblSchedules.KidID) AND (tblKids.KidID = tblSchedules.KidID)
-
GROUP BY tblKids.KidID, IIf([FakeEnd]=#12/31/9999#,Null,[FakeEnd]);
-
Thanks,
Bradley
I posted:
Max(Nz(EndDate,#01-01-2999#))
and NOT
NZ(Max(EndDate,#01-01-2999#))
The problem described about the Max can't happen with the proposed Max(NZ()) statement!
Nic;o)
Thanks to you both for your help. It was Max(Nz(... that I tried but with the same result. Subquerying did work, though. Hooray! In the end, this is the SQL that worked. - SELECT tblKids.KidID, IIf([FakeEnd]=#12/31/9999#,Null,[FakeEnd]) AS FinalEnd
-
FROM [SELECT tblKids.KidID, Max(IIf(IsNull([tblSchedules].[EndDate]),#12/31/9999#,[tblSchedules].[EndDate])) AS FakeEnd
-
FROM tblKids INNER JOIN tblSchedules ON (tblKids.KidID = tblSchedules.KidID) AND (tblKids.KidID = tblSchedules.KidID) AND (tblKids.KidID = tblSchedules.KidID)
-
GROUP BY tblKids.KidID]. AS [%$##@_Alias] INNER JOIN tblKids ON [%$##@_Alias].KidID = tblKids.KidID
-
GROUP BY tblKids.KidID, IIf([FakeEnd]=#12/31/9999#,Null,[FakeEnd]);
Thank you again,
Bradley
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Rigga |
last post by:
Hi,
I am new to mysql and need help on how to join tables. I have a database
which contains 4 tables, the main table contains information by date order
and the other 3 contain data also in date...
|
by: MacDermott |
last post by:
I have a query, which gathers up information, which is subsequently dumped
into an instance of Excel using recordsetcopy.
For one of the query fields, I have written what should be a pretty...
|
by: Apple |
last post by:
1. I want to create an autonumber, my requirement is : 2005/0001
(Year/autonumber), which year & autonumber no. both can auto run.
2. I had create a query by making relation to a table & query,...
|
by: clare at snyder.on.ca |
last post by:
I have a SQL query I need to design to select name and email addresses
for policies that are due and not renewed in a given time period. The
problem is, the database keeps the information for every...
|
by: sandy |
last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory',
within my class Directory (Can you already smell disaster?)
Each Directory can have subdirectories so I thought to put these...
|
by: Sheldon |
last post by:
I have the following query expression -
Like Format((!!)) & "/*/" & (!
!)
which would translate to e.g. 04/*/2007 if someone is running a report
for last month. The above expression is part of a...
|
by: RCapps |
last post by:
When running the below SQL Query I keep getting the following error:
Server: Msg 4924, Level 16, State 1, Line 1
ALTER TABLE DROP COLUMN failed because column 'ContractDef' does not exist in table...
|
by: KevLe |
last post by:
I'm building a log search function in c# for a certain management app and would like some help on the design how to solve
this, here is my solution (on paper) so far:
The log files are saved to...
|
by: MU |
last post by:
Hello
I have some code that sets a dropdownlist control with a parameter
from the querystring. However, when the querystring is empty, I get
an error.
Here is my code:
Protected Sub...
|
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...
|
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
|
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...
|
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...
|
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,...
|
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...
|
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,...
|
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: 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...
| |