Solved

Where/Having Question

Posted on 2001-06-05
13
187 Views
Last Modified: 2006-11-17
I want to limit a query to a maximum date entered..

I would like to say .. where myDateField = max(mydatefield)

How can I use a function like MAX in a where clause or having clause .. I have tried different sytaxes with no luck.  Im sure its easy..I am actually using it in Access but figured it was more of an SQL question
0
Comment
Question by:kiprimshot
  • 5
  • 4
  • 3
  • +1
13 Comments
 
LVL 32

Expert Comment

by:bhess1
Comment Utility
Try:

... WHERE myDateField = (Select Max(myDateField) FROM MyFile)
0
 
LVL 2

Author Comment

by:kiprimshot
Comment Utility
The problem with that is that it only returns the max date of one record..
right now without the where clause I am getting the max date for each individual...

I want to continue with that ...

I hope that makes sense
0
 
LVL 8

Expert Comment

by:dovholuk
Comment Utility
so how about a little subquery then?

select * from MyTable WHERE MyDateField < (SELECT MAX(MyOtherDateField) FROM SomeOtherTable)

would that work???

i'll check the syntax and get back to you...

dovholuk
0
 
LVL 8

Expert Comment

by:dovholuk
Comment Utility
yeah, that'll work.

use a subquery as above

need more help?

dovholuk
0
 
LVL 32

Accepted Solution

by:
bhess1 earned 50 total points
Comment Utility
You can modify the Subquery to refer to the ID for each individual, e.g.:

... WHERE myDateField = (Select Max(X.myDateField) FROM MyFile AS X WHERE X.ID = MyFile.ID)

In this case, MyFile.ID refers to the ID of the main recordset.  
0
 
LVL 2

Author Comment

by:kiprimshot
Comment Utility
isnt that the same as what bhess suggested?  Maybe Im misunderstanding..
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 8

Expert Comment

by:dovholuk
Comment Utility
kiprimshot,

yes, pretty much. i read your response without reading bhess' and i jumped the gun a bit. but i also changed the = sign to a < sign, which will give you all the records up to that date.

it's still the same basic idea though, but i thought you were looking for the records up to/ including that Max date.

my apologies if i recycled a response.

dovholuk
0
 
LVL 54

Expert Comment

by:nico5038
Comment Utility
Just a little remark, is the Time included in the date field.... ?

Nic;o)
0
 
LVL 2

Author Comment

by:kiprimshot
Comment Utility
no time
0
 
LVL 32

Expert Comment

by:bhess1
Comment Utility
Question:  Did you try the second query example I provided above?  Does it do what you need?
0
 
LVL 8

Expert Comment

by:dovholuk
Comment Utility
kiprimshot,

either my query or bhess' should do what you're looking for.  have you tried them?

if you didn't get what you needed, please let us know.

dovholuk
0
 
LVL 2

Author Comment

by:kiprimshot
Comment Utility
I will try it sometime this afternoon
0
 
LVL 2

Author Comment

by:kiprimshot
Comment Utility
I should have been able to figure this out..
Thanks to both of you for your help.
0

Featured Post

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now