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

SELECT clause and LAST function causing problems

Hello all,
I have the following data, that was queried and sorted to columns
PROBLEM_ID and then by STATUSDATE (ascending):

STATUS_ID STATUSDATE PROBLEM_ID
--------- ---------- ----------
10 12/04/2005 1234
40 15/05/2005 1234
10 11/11/2004 1235
50 15/12/2004 1235
70 20/01/2005 1235

I now want another query which returns the row which is always the
latest STATUSDATE. This is what I have done so far:

SELECT qryFirst.PROBLEM_ID, Last(qryFirst.STATUS_ID) AS LAST_STATUS
FROM qryFirst
GROUP BY qryFirst.PROBLEM_ID;

However, when I want to add qryFirst.STATUSDATE to the SELECT clause, I
get an error ,,You tried to execute a query that does not include the
specified expression <name> as part of an aggregate function. (Error
3122)"

I need to see what the STATUSDATE is for each of the records returned
in the second query. How do I do this? Please!!!!

Kind Regards,

Jean

Nov 13 '05 #1
8 10290
On 19 Jul 2005 04:26:29 -0700, "Jean" <je**********@hotmail.com>
wrote:

Classic problem. I always solve it by using two queries. The first one
(let's call it Q2) gets the Max(StatusDate) group by ProblemID.
The second one joins the original query Q1 and Q2 by both the
StatusDate and ProblemID fields, and then you can additionally select
StatusID and any other column Q1 might have.

Btw, note the use of Max rather than Last. I find it more predictable,
and it is more universally part of the SQL language.

-Tom.

Hello all,
I have the following data, that was queried and sorted to columns
PROBLEM_ID and then by STATUSDATE (ascending):

STATUS_ID STATUSDATE PROBLEM_ID
--------- ---------- ----------
10 12/04/2005 1234
40 15/05/2005 1234
10 11/11/2004 1235
50 15/12/2004 1235
70 20/01/2005 1235

I now want another query which returns the row which is always the
latest STATUSDATE. This is what I have done so far:

SELECT qryFirst.PROBLEM_ID, Last(qryFirst.STATUS_ID) AS LAST_STATUS
FROM qryFirst
GROUP BY qryFirst.PROBLEM_ID;

However, when I want to add qryFirst.STATUSDATE to the SELECT clause, I
get an error ,,You tried to execute a query that does not include the
specified expression <name> as part of an aggregate function. (Error
3122)"

I need to see what the STATUSDATE is for each of the records returned
in the second query. How do I do this? Please!!!!

Kind Regards,

Jean


Nov 13 '05 #2
On Tue, 19 Jul 2005 06:35:51 -0700, Tom van Stiphout <no*************@cox.net>
wrote:
On 19 Jul 2005 04:26:29 -0700, "Jean" <je**********@hotmail.com>
wrote:

Classic problem. I always solve it by using two queries. The first one
(let's call it Q2) gets the Max(StatusDate) group by ProblemID.
The second one joins the original query Q1 and Q2 by both the
StatusDate and ProblemID fields, and then you can additionally select
StatusID and any other column Q1 might have.

Btw, note the use of Max rather than Last. I find it more predictable,
and it is more universally part of the SQL language.


To add to what Tom said, the First() and Last() functions are unfortunately
named. The thing either of them does is to return an arbitrary value from the
group, with nothing particularly "first" or "last" about it. I presume these
functions actually do something like what their names imply if you are
querying a table linked to an ISAM data source in which physical order is
considered meaningful, such as Paradox.
Nov 13 '05 #3
Steve Jorgensen <no****@nospam.nospam> wrote in
news:qk********************************@4ax.com:
On Tue, 19 Jul 2005 06:35:51 -0700, Tom van Stiphout
<no*************@cox.net> wrote:
On 19 Jul 2005 04:26:29 -0700, "Jean" <je**********@hotmail.com>
wrote:

Classic problem. I always solve it by using two queries. The first
one (let's call it Q2) gets the Max(StatusDate) group by
ProblemID. The second one joins the original query Q1 and Q2 by
both the StatusDate and ProblemID fields, and then you can
additionally select StatusID and any other column Q1 might have.

Btw, note the use of Max rather than Last. I find it more
predictable, and it is more universally part of the SQL language.


To add to what Tom said, the First() and Last() functions are
unfortunately named. The thing either of them does is to return
an arbitrary value from the group, with nothing particularly
"first" or "last" about it. I presume these functions actually do
something like what their names imply if you are querying a table
linked to an ISAM data source in which physical order is
considered meaningful, such as Paradox.


Well, First() means the first value for that column in the recordset
in its present order, Last() means the last value.

It's no different than the fact that TOP 10 on a query sorted in
descending data order will return a different set of records than
TOP 10 on the same query sorted by CompanyName ascending.

It should be self-evident that the terms First() and Last() are
dependent on the ordering of the domain to which they are applied.
Why this confuses people into saying that they return
non-predictable results, I can't say. It's only non-predictable if
you're ignoring the sort order.

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 13 '05 #4
On Tue, 19 Jul 2005 14:16:52 -0500, "David W. Fenton"
<dX********@bway.net.invalid> wrote:
Steve Jorgensen <no****@nospam.nospam> wrote in
news:qk********************************@4ax.com :
On Tue, 19 Jul 2005 06:35:51 -0700, Tom van Stiphout
<no*************@cox.net> wrote:
On 19 Jul 2005 04:26:29 -0700, "Jean" <je**********@hotmail.com>
wrote:

Classic problem. I always solve it by using two queries. The first
one (let's call it Q2) gets the Max(StatusDate) group by
ProblemID. The second one joins the original query Q1 and Q2 by
both the StatusDate and ProblemID fields, and then you can
additionally select StatusID and any other column Q1 might have.

Btw, note the use of Max rather than Last. I find it more
predictable, and it is more universally part of the SQL language.


To add to what Tom said, the First() and Last() functions are
unfortunately named. The thing either of them does is to return
an arbitrary value from the group, with nothing particularly
"first" or "last" about it. I presume these functions actually do
something like what their names imply if you are querying a table
linked to an ISAM data source in which physical order is
considered meaningful, such as Paradox.


Well, First() means the first value for that column in the recordset
in its present order, Last() means the last value.

It's no different than the fact that TOP 10 on a query sorted in
descending data order will return a different set of records than
TOP 10 on the same query sorted by CompanyName ascending.

It should be self-evident that the terms First() and Last() are
dependent on the ordering of the domain to which they are applied.
Why this confuses people into saying that they return
non-predictable results, I can't say. It's only non-predictable if
you're ignoring the sort order.


It is, in fact self-evident until you realize there's no way to specify the
sort that you're talking about. Aggregate functions, including First() and
Last() return an item from within the group, but you can only sort a query on
fields on the group by clause or on aggregate results.

For example, let's say I have the following...

tblPerson
PersonID
FullName
StreetAddress
City
State
ZipCode

Now, let's say I want the first full name in alphabetical order for each Zip
code...

SELECT ZipCode,
First(FullName) As FirstFullName
FROM tblPerson
GROUP BY ZipCode
ORDER BY ZipCode

So - what does First() mean? If you say that it should be in the sort order,
well, the sort order only sorts ZipCode groups. If I try to add FullName to
the sort, that's an error because FullName is not part of the GROUP BY. If I
try to solve that by adding FullName to the GROUP BY, that of course, defeats
the whole purpose because I no longer have a single group for each ZipCode.
There is simply no way in SQL to specify a sort order by which First or Last
within a group should be identified.
Nov 13 '05 #5
Thanks for the input guys, and for clearing that up with the FIRST and
LAST functions.

I actually tried FIRST/LAST in some way and got it right, but then I
had to use the FIRST function instead of the LAST. Having got it right
I thought cool, but WHY FIRST???

So I tried MAX as you suggested, and it still works.

Tom, I tried your suggestion, and compared it to mine, and it gives the
same results. Thanks.
This is what I did:

SELECT qryFirst.PROBLEM_ID, MAX(qryFirst.STATUS_ID) AS LAST_STATUS
FROM qryFirst
GROUP BY qryFirst.PROBLEM_ID;

I will use your second query suggestion now to get the STATUSDATE.

Regards,

Jean

Nov 13 '05 #6
Bri


Steve Jorgensen wrote:
On Tue, 19 Jul 2005 06:35:51 -0700, Tom van Stiphout <no*************@cox.net>
wrote:
To add to what Tom said, the First() and Last() functions are unfortunately
named. The thing either of them does is to return an arbitrary value from the
group, with nothing particularly "first" or "last" about it. I presume these
functions actually do something like what their names imply if you are
querying a table linked to an ISAM data source in which physical order is
considered meaningful, such as Paradox.


I thought that if the source was sorted then you got the Min or Max?

ie, these are equivalent:
SELECT Max(Value) AS A FROM MTable
SELECT Last(Value) AS A FROM [SELECT * FROM MTable ORDER BY Value]. AS S

From this it is obviously easier to use the Max as without the Sort,
Last is not goin to work predictably.

My understanding was that in a newly compacted DB that the tables were
physically sorted by the PK, so there is an order. Of course, as soon as
you start adding/deleting/editing that the order is no longer guaranteed.

Personally, I only use First or Last when I need to include a field in
an Aggregate/Transform query that isn't part of the
Grouping/Count/Sum/etc.

--
Bri

Nov 13 '05 #7
On Tue, 19 Jul 2005 16:23:21 GMT, Bri <no*@here.com> wrote:


Steve Jorgensen wrote:
On Tue, 19 Jul 2005 06:35:51 -0700, Tom van Stiphout <no*************@cox.net>
wrote:
To add to what Tom said, the First() and Last() functions are unfortunately
named. The thing either of them does is to return an arbitrary value from the
group, with nothing particularly "first" or "last" about it. I presume these
functions actually do something like what their names imply if you are
querying a table linked to an ISAM data source in which physical order is
considered meaningful, such as Paradox.
I thought that if the source was sorted then you got the Min or Max?

ie, these are equivalent:
SELECT Max(Value) AS A FROM MTable
SELECT Last(Value) AS A FROM [SELECT * FROM MTable ORDER BY Value]. AS S


That was not my impression, but I have never tested it. Supposedly, the
sorting of any source is ignored when it is used in the FROM clause of another
query. In fact, you can see this at work if you just save one query with a
sort, then run a query of that query with no sort. The output generally looks
the same as doing a single query with no sort. Also, what would the order of
a FROM source mean if the FROM clause is a join of multiple sources? Which
sort would be definitive?

From this it is obviously easier to use the Max as without the Sort,
Last is not goin to work predictably.

My understanding was that in a newly compacted DB that the tables were
physically sorted by the PK, so there is an order. Of course, as soon as
you start adding/deleting/editing that the order is no longer guaranteed.

Personally, I only use First or Last when I need to include a field in
an Aggregate/Transform query that isn't part of the
Grouping/Count/Sum/etc.


Nov 13 '05 #8
Bri
Steve Jorgensen wrote:
I thought that if the source was sorted then you got the Min or Max?

ie, these are equivalent:
SELECT Max(Value) AS A FROM MTable
SELECT Last(Value) AS A FROM [SELECT * FROM MTable ORDER BY Value]. AS S

That was not my impression, but I have never tested it. Supposedly, the
sorting of any source is ignored when it is used in the FROM clause of another
query. In fact, you can see this at work if you just save one query with a
sort, then run a query of that query with no sort. The output generally looks
the same as doing a single query with no sort. Also, what would the order of
a FROM source mean if the FROM clause is a join of multiple sources? Which
sort would be definitive?


I see where you are going with this and it does make sense. But if the
sort is of no importance then what possible use could First/Last ever
have? Hmm, thought I should see what Help has to say about it and it
does seem to say that the sort is relevant:

"Because records are usually returned in no particular order (unless the
query includes an ORDER BY clause), the records returned by these
functions will be arbitrary."

Min/Max are much easier and predictable to use, so I'm not likely to
switch over. It just seems odd that First/Last exist if they don't
actually do anything meaningful.

--
Bri

Nov 13 '05 #9

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

Similar topics

5
by: malcolm | last post by:
Example, suppose you have these 2 tables (NOTE: My example is totally different, but I'm simply trying to setup the a simpler version, so excuse the bad design; not the point here) CarsSold {...
9
by: Christian Wetzig | last post by:
hi, i have a table with test_field of type "TEXT". when i do: select test_field from test where id=1; the connection (with psql) hangs and no output is received, so i have to kill psql. ...
3
by: Tcs | last post by:
My backend is DB2 on our AS/400. While I do HAVE DB2 PE for my PC, I haven't loaded it yet. I'm still using MS Access. And no, I don't believe this is an Access question. (But who knows? I...
19
by: William Wisnieski | last post by:
Hello Everyone, I have a main form with a datasheet subform that I use to query by form. After the user selects two criteria on the main form and clicks the cmdShowResults button on the main...
26
by: GreatAlterEgo | last post by:
Hi, This is my query which is embedded in a COBOL program. EXEC SQL SELECT DATE, AGE, DURATION, AMT INTO :LDATE, :L.AGE, :L.DURATION, :L.AMT FROM TAB1 WHERE CODE = :KEY.CODE AND...
5
by: Henning M | last post by:
Hi all, I having some problems with Access and selecting records between dates.. When I try this in access, it works fine!! "Select * from Bilag Where Mdates Between #1/1/2006# And...
48
by: Jimmy | last post by:
thanks to everyone that helped, unfortunately the code samples people gave me don't work. here is what i have so far: <% Dim oConn, oRS, randNum Randomize() randNum = (CInt(1000 * Rnd) + 1) *...
22
by: MP | last post by:
vb6,ado,mdb,win2k i pass the sql string to the .Execute method on the open connection to Table_Name(const) db table fwiw (the connection opened via class wrapper:) msConnString = "Data Source="...
2
by: AWasilenko | last post by:
I can't figure out this problem Im having, I just can't understand why it is ignoring the call I put in. First the code (This is a cherrypy website): import sys, cherrypy, html class Root:...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.