I have a table - tblCorresponden ce. It houses records
of correspondence initiated here in our office. Records
are appended whenever it is recognized that some type
of outbound letter needs to be created and sent. The
table contains [OutDate] and [OutProcessor] fields. The
2 fields are updated when the letter is printed, permanently
marking it as having been printed & presumably sent.
At any given moment, there may be a number of unprinted
letters. Whenever its convenient, the clerk clicks a button
to print them all. Their [OutDate] and [OutProcessor] fields
are updated to Now() and CurrentUser() respectively.
What's the best way to write a query that'll extract only the
last record(s) marked as printed? Could be none, one or a
dozen - dunno. Also - dunno the exact timestamp that goes
into the [OutDate] when it is written. The records are NOT
all grouped together at the end of the table.
Recommendation? 6 1481
Select c.* From tblCorresponden ce As c where c.OutDate = (Select
Max(OutDate) From tblCorresponden ce))
MLH <CR**@NorthStat e.net> wrote in
news:b4******** *************** *********@4ax.c om: I have a table - tblCorresponden ce. It houses records of correspondence initiated here in our office. Records are appended whenever it is recognized that some type of outbound letter needs to be created and sent. The table contains [OutDate] and [OutProcessor] fields. The 2 fields are updated when the letter is printed, permanently marking it as having been printed & presumably sent.
At any given moment, there may be a number of unprinted letters. Whenever its convenient, the clerk clicks a button to print them all. Their [OutDate] and [OutProcessor] fields are updated to Now() and CurrentUser() respectively.
What's the best way to write a query that'll extract only the last record(s) marked as printed? Could be none, one or a dozen - dunno. Also - dunno the exact timestamp that goes into the [OutDate] when it is written. The records are NOT all grouped together at the end of the table.
Recommendation?
SELECT * FROM tblCorresponden ce where datediff("h",
[outdate],dmax("[outdate]","tblCorrespon dence")) < 9
ORDER BY [outdate] DESC;
This will return all records flagged on the last day that any
correspondence was printed, assuming a 9 to 5 workday.
--
Bob Quintal
PA is y I've altered my email address.
YES BABY!!!!
U R A genius man!
>Select c.* From tblCorresponden ce As c where c.OutDate = (Select Max(OutDate) From tblCorresponden ce))
When I look at the above syntax, a natural question comes to mind.
Why the "As c" thing? I mean, why the above rather than the following?
SELECT *
FROM tblCorresponden ce
WHERE (((OutDate)=(Se lect Max(OutDate) From tblCorresponden ce)));
I'm guessing there's some added flexibility in using the syntax (that
perhaps is not illustrated in this example) and people tend to favor
the syntax for that reason. But I am curious and wonder if you might
elaborate - it time permits.
Thx again for the solution.
Sorry. I should have said something about that.
The "As c" you probably know by now is an alias. It is a device used to
provide a convenient reference to a long (or confusing) table name. One
benefit: It can make you SQL shorter and easier to read. Compare
Select [some stupid old table name].LastName, [some stupid old table
name].FirstName, [some stupid old table name].Title
From [some stupid old table name]
Where [some stupid old table name].Category = 'mycategory'
with
Select t.LastName, t.FirstName, t.Title
From [some stupid old table name] as t
Where t.Category = 'mycategory'
So, readability all by itself is a plus. I like aliases so much just on
this ground that I regularly let my fingers to crazy and give my tables
really clear names, regardless of length. Then, when referring to them in
the application I apply aliases everywhere.
There is another reason more related to function than to convenience. In
some queries, you will need to use a subquery. That will have a form like
this:
Select field1, field2, field3... From mytable Where field1 =
(Select(Max) field1 from mytable and mytable.field2 = mytable.field2)
An example would be wehre field1 is a date field, and you want all the
records from mytable where field1 is equal to the latest date contained in
field1, and, in the example above, field2 in the main query is equal to
field2 in the subquery. In the SQL above, there is a problem. The intent
is that field2 in the subquery be equal to field2 in the main query. There
is no way for you (or any SQL parser) to understand this intend. Both
references are to mytable.field2, Whicy mytable.field2?
Aliases solve this problem.
Select field1, field2, field3... From mytable As mUpper Where field1 =
(Select(Max) mSub.field1 from mytable as mSub and mSub.field2 =
mUpper.field2)
Note how the use of aliases makes it possible in the subquery to specify
that mSub.field2 should be compared to mUpper.field2.
Enlightening.
Thx for the explanation. Seems I agree
with you on the first count. And I understand
the implications in the second count.
Many thx. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Peter Foti |
last post by:
Simple question... which is better to use for defining font sizes and why?
px and em seem to be the leading candidates. I know what the general answer
is going to be, but I'm hoping to ultimately get some good real world
examples.
Fire away! :)
Regards,
Peter Foti
|
by: Matt Kruse |
last post by:
http://www.JavascriptToolbox.com/bestpractices/
I started writing this up as a guide for some people who were looking for
general tips on how to do things the 'right way' with Javascript. Their code
was littered with document.all and eval, for example, and I wanted to create
a practical list of best practices that they could easily put to use.
The above URL is version 1.0 (draft) that resulted. IMO, it is not a
replacement for the FAQ,...
|
by: Andrew S. Giles |
last post by:
I thought I would post here, as I am sure someone, somewhere has run into
this problem, and might have a good solution for me.
I am writing an applicaiton in C# that will accept data and then put it into
an Excel spreadsheet. Easy, right? Well it is, until you have to get the
data from another application that is written in Borland C++ PowerBuilder 5.
The situation is that the Borland Code isnt going to get re-written (too
expensive,...
|
by: fox |
last post by:
Hi,
Lacking javascript knowledge, I just realized why my project has a bug.
I am using ASP to loop through a set of records while it creates URLs
with a querystring that has a single value pair. This URL needs to open
in a floating window if clicked. (this is for an administrator and so
opening a small floater gives them more efficient access to the data that
will be displayed). I now understand that because the ASP executes first,
that...
|
by: 2005 |
last post by:
Hi
In C++, are the following considered best practices or not?
- passing aguments to functions (ie functions do not take any arguments
)
- returning values using return statement
Anything else?
The reason for this question is that I had an assignment in which I was
| |
by: gs |
last post by:
let say I have to deal with various date format and I am give format string
from one of the following
dd/mm/yyyy
mm/dd/yyyy
dd/mmm/yyyy
mmm/dd/yyyy
dd/mm/yy
mm/dd/yy
dd/mmm/yy
mmm/dd/yy
|
by: darthghandi |
last post by:
I've created a class to listen to all interfaces and do a
BeginAccept(). Once it gets a connection, it passes the connected
socket off and stores it in a List. Next, it continues to listen for
more incoming connections and does the BeginAccpet() again. It does
an infinite loop this way. My question is: What is the best way to
stop this? I thought about putting a boolean in there, but then what
if it's still waiting for an incoming...
|
by: Guy |
last post by:
Is there a better way to search identical elements in a sorted array
list than the following:
iIndex = Array.BinarySearch( m_Array, 0, m_Array.Count,
aSearchedObject );
aFoundObject= m_Array;
m_ResultArray.Add ( aFoundObject);
|
by: GS |
last post by:
I need to extract sections out of a long string of about 5 to 10 KB, change
any date format of dd Mmm yyyy to yyyy-mm-dd, then further from each section
extract columns of tables.
what is the best approach in using regex for this? I can see match and
replace the dates, extract section with regex, and then for each section
extract again with the right regex the tables....
|
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...
|
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,...
| |
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...
|
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...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |