Re: Using Max and Criteria on the same field in Access Query
- From: Bob Quintal <rquintal@xxxxxxxxxxxxx>
- Date: Sun, 27 Apr 2008 07:31:57 -0400
dsdevonsomer@xxxxxxxxx wrote in
news:1fd2ab04-b1cf-44f9-b570-092aef9bb7d1
@t63g2000hsf.googlegroups.co
m:
On Apr 26, 10:32 pm, dsdevonso...@xxxxxxxxx wrote:
Hello Everyone,
I faced strange situation. To explain, let's take the following
table with 2 rows.
ID | Order # | Order Date | State | Count
1 | 201 | 10/1/2007 | CA | 1
2 | 201 | 11/15/2007 | CA | 1
3 | 201 | 3/1/2008 | CA | 1
4 | 201 | 4/10/2008 | CA | 1
Now, I want to find the max of order date and then total count of
orders. So if I use the following query, it gives me total of 4
for entire life of this order.
SELECT ORDER_DETAIL.[ORDER #], Max(ORDER_DETAIL.[ORDER DATE]) AS
[MaxOfORDER DATE], ORDER_DETAIL.STATE, Sum(ORDER_DETAIL.COUNT) AS
SumOfCOUNT
FROM ORDER_DETAIL
GROUP BY ORDER_DETAIL.[ORDER #], ORDER_DETAIL.STATE;
But, if I want to only find out how many orders were placed in
this year and what's the max date on that order, I tried to use
this query and it gave me still 4 count instead of 2.
SELECT ORDER_DETAIL.[ORDER #], Max(ORDER_DETAIL.[ORDER DATE]) AS
[MaxOfORDER DATE], ORDER_DETAIL.STATE, Sum(ORDER_DETAIL.COUNT) AS
SumOfCOUNT
FROM ORDER_DETAIL
GROUP BY ORDER_DETAIL.[ORDER #], ORDER_DETAIL.STATE
HAVING (((Max(ORDER_DETAIL.[ORDER DATE]))>#1/1/2008#));
I was confused... How would I get 2 order count ?
What's going wrong?
JB
To check something else, I did the following
SELECT ORDER_DETAIL.[ORDER #], Max(ORDER_DETAIL.[ORDER DATE]) AS
[MaxOfORDER DATE], ORDER_DETAIL.STATE, Sum(ORDER_DETAIL.COUNT) AS
SumOfCOUNT
FROM ORDER_DETAIL
WHERE (((ORDER_DETAIL.[ORDER DATE])>#1/1/2008#))
GROUP BY ORDER_DETAIL.[ORDER #], ORDER_DETAIL.STATE
HAVING (((ORDER_DETAIL.STATE)="CA"));
but for this I had to add one more time "Order Date" in columns
and apply my conditions. This is strange. I couldn't use >
1/1/2008 on the same field where I have used MAX. There must be
something inherently different in the way MS Access runs query on
MAX and WHERE. Does anyone know ?
JB.
Access applies the max(), sum(), etc functions only to the column
not to the the entire row. and does this consistently. the Where
clause is applied to the entire row before the functions. the Having
clause applies criteria after the functions.
To get the results you wish, you need to use one query to filter the
records and a second query based on the first to do the totals.
Once this is working as separate queries you can (sometimes) rewrite
the SQL into a single query, using the SQL editor.
Query1=
SELECT ORDER_DETAIL.[ORDER #], ORDER_DETAIL.[ORDER DATE]),
ORDER_DETAIL.STATE, ORDER_DETAIL.COUNT FROM ORDER_DETAIL
WHERE year(ORDER_DETAIL.[ORDER DATE]) = 2008;
Query2
SELECT Query1.[ORDER #], Max(Query1.[ORDER DATE]) AS
[MaxOfORDER DATE], Query1.STATE, Sum(Query1.COUNT) AS
SumOfCOUNT
FROM Query1
GROUP BY Query1.[ORDER #], Query1.STATE;
--
Bob Quintal
PA is y I've altered my email address.
** Posted from http://www.teranews.com **
.
- References:
- Using Max and Criteria on the same field in Access Query
- From: dsdevonsomer
- Re: Using Max and Criteria on the same field in Access Query
- From: dsdevonsomer
- Using Max and Criteria on the same field in Access Query
- Prev by Date: Re: immediate filtering
- Next by Date: Re: Report formatting Disappears on Some Items when Output to PDF in Code
- Previous by thread: Re: Using Max and Criteria on the same field in Access Query
- Next by thread: Re: Using Max and Criteria on the same field in Access Query
- Index(es):
Relevant Pages
|
|