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

Create Query to evaluate Max Date recognizing Null as High Value

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:

Expand|Select|Wrap|Line Numbers
  1. SELECT tblKids.KidID, Min(tblSchedules.StartDate) AS OriginalStart, Max(tblSchedules.EndDate) AS FinalEnd
  2. FROM tblKids INNER JOIN tblSchedules ON (tblKids.KidID = tblSchedules.KidID) AND (tblKids.KidID = tblSchedules.KidID) AND (tblKids.KidID = tblSchedules.KidID)
  3. GROUP BY tblKids.KidID;
  4.  
Thanks,
Bradley
Oct 7 '07 #1
5 6291
Scott Price
1,384 Expert 1GB
Try ORDER BY tblKids.EndDate...

Welcome to the Scripts!

Regards,
Scott
Oct 7 '07 #2
nico5038
3,080 Expert 2GB
When you see Null as the highest enddate, you should use the NZ() function e.g. like:
Expand|Select|Wrap|Line Numbers
  1. Max(Nz(EndDate,#01-01-2999#)) 
  2.  
Getting the idea ?

Nic;o)
Oct 7 '07 #3
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?

Expand|Select|Wrap|Line Numbers
  1. 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
  2. FROM tblKids INNER JOIN tblSchedules ON (tblKids.KidID = tblSchedules.KidID) AND (tblKids.KidID = tblSchedules.KidID) AND (tblKids.KidID = tblSchedules.KidID)
  3. GROUP BY tblKids.KidID, IIf([FakeEnd]=#12/31/9999#,Null,[FakeEnd]);
  4.  
Thanks,
Bradley
Oct 7 '07 #4
nico5038
3,080 Expert 2GB
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)
Oct 8 '07 #5
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.

Expand|Select|Wrap|Line Numbers
  1. SELECT tblKids.KidID, IIf([FakeEnd]=#12/31/9999#,Null,[FakeEnd]) AS FinalEnd
  2. FROM [SELECT tblKids.KidID, Max(IIf(IsNull([tblSchedules].[EndDate]),#12/31/9999#,[tblSchedules].[EndDate])) AS FakeEnd
  3. FROM tblKids INNER JOIN tblSchedules ON (tblKids.KidID = tblSchedules.KidID) AND (tblKids.KidID = tblSchedules.KidID) AND (tblKids.KidID = tblSchedules.KidID)
  4. GROUP BY tblKids.KidID]. AS [%$##@_Alias] INNER JOIN tblKids ON [%$##@_Alias].KidID = tblKids.KidID
  5. GROUP BY tblKids.KidID, IIf([FakeEnd]=#12/31/9999#,Null,[FakeEnd]);
Thank you again,
Bradley
Oct 8 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

8
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...
8
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...
4
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,...
24
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...
23
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...
3
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...
0
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...
0
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...
24
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...
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
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
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...
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.