PC Review


Reply
Thread Tools Rate Thread

Counting the maximum value

 
 
John
Guest
Posts: n/a
 
      6th Jan 2008
Hi

I have a column of amounts. I need to find the count of the maximum amount.
For example if I have the below records

$10
$9
$8
$9
$10
$9

then the maximum is $10 and the count is 2. How can I do it via a query?

Thanks

Regards



 
Reply With Quote
 
 
 
 
giorgio rancati
Guest
Posts: n/a
 
      6th Jan 2008
Hi,
try these queries

#1 Derived table
----
SELECT tab1.amount, Count(tab1.amount) AS CountOfAmount
FROM tab1,
(Select Max(amount) AS MaxOfAmount From tab1) AS DrvTbl
WHERE tab1.amount=DrvTbl.MaxOfAmount
GROUP BY tab1.amount;
----

#2 SubQuery
----
SELECT tab1.amount, Count(tab1.amount) AS CountOfAmount
FROM tab1
WHERE tab1.amount=(Select Max(amount) From tab1)
GROUP BY tab1.amount;
----

#3 Domain Aggregate function
----
SELECT tab1.amount, Count(tab1.amount) AS CountOfAmount
FROM tab1
WHERE tab1.amount=Dmax("amount","tab1")
GROUP BY tab1.amount;
----

bye
--
Giorgio Rancati
[Office Access MVP

"John" <(E-Mail Removed)> ha scritto nel messaggio
news:(E-Mail Removed)...
> Hi
>
> I have a column of amounts. I need to find the count of the maximum
> amount. For example if I have the below records
>
> $10
> $9
> $8
> $9
> $10
> $9
>
> then the maximum is $10 and the count is 2. How can I do it via a query?
>
> Thanks
>
> Regards
>
>
>



 
Reply With Quote
 
John W. Vinson
Guest
Posts: n/a
 
      6th Jan 2008
On Sun, 6 Jan 2008 08:51:22 -0000, "John" <(E-Mail Removed)> wrote:

>Hi
>
>I have a column of amounts. I need to find the count of the maximum amount.
>For example if I have the below records
>
>$10
>$9
>$8
>$9
>$10
>$9
>
>then the maximum is $10 and the count is 2. How can I do it via a query?
>
>Thanks
>
>Regards
>
>


You can do it with one subquery:

SELECT Amount, Count(*)
FROM tablename
HAVING Amount = (SELECT Max(X.[amount]) FROM tablename AS X)
GROUP BY Amount;

If you want to do this on a subset of the records rather than on the whole
table, you'll need WHERE clauses is both the main and subqueries.

John W. Vinson [MVP]
 
Reply With Quote
 
Michel Walsh
Guest
Posts: n/a
 
      8th Jan 2008
In addition to the answers you already got, you can use a TOP 1 on a group
by query, something that may possibly looks like:


SELECT TOP 1 amount
FROM tableName
GROUP BY amount
ORDER BY COUNT(*) DESC



which will return the amount having the maximum number of occurrence (if
there are two amounts occurring to the same max number of occurrence, they
will be both listed).


Note that the syntax can be a little bit deceptive. It does NOT take the
TOP 1 ***amount*** , no, it takes the top 1 record (here, group of records)
and from it, select the field amount. Which is the top 1 record, you can
ass? well, after all records are grouped and order by their number of
occurrence, the order being descending, the top one on the list is the
(group of) record you will got.



Vanderghast, Access MVP


"John" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi
>
> I have a column of amounts. I need to find the count of the maximum
> amount. For example if I have the below records
>
> $10
> $9
> $8
> $9
> $10
> $9
>
> then the maximum is $10 and the count is 2. How can I do it via a query?
>
> Thanks
>
> Regards
>
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Counting cells before/after a maximum value Paul Hyett Microsoft Excel Discussion 7 24th Feb 2008 06:15 PM
Re: Counting cells before/after a maximum value T. Valko Microsoft Excel Discussion 0 21st Feb 2008 07:41 PM
Re: Counting cells before/after a maximum value Pete_UK Microsoft Excel Discussion 0 21st Feb 2008 06:51 PM
Counting the maximum value John Microsoft Access Queries 3 8th Jan 2008 06:46 PM
Maximum Tables? Maximum Fields/Columns Per Record? =?Utf-8?B?c2hhcnJpcw==?= Microsoft Access Getting Started 7 17th Oct 2005 05:23 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:11 PM.