Hi,
I spent the whole day trying to figure out a valid mysql query which includes MAX and COUNT and the same time: suppose I have the following table recording some bought items
ITEM TYPE
banana fruit
banana fruit
apple fruit
carrot vegetable
tomato vegetable
tomato vegetable
tomato vegetable
I need to retrieve the maximum number of hits for each TYPE: something like
fruit 2 banana
vegetable 3 tomato
Still I can't imagine a single query to do all the job, any hints?
Thanks in advance should someone be inspired enough ;)
Josep
7 2177
This will work for you - SELECT * FROM (
-
SELECT type, COUNT(*) AS cnt, item FROM table_name
-
GROUP BY item
-
ORDER BY cnt DESC
-
) tmp
-
GROUP BY type
Hey many thanks!
I get the idea, not sure if it would be too hard just to display the max item count from each type, as this query makes the count for item, orders it and groups it by type, but displays all the items...the problem is just to display the item with the highest count from each type...
This will work for you - SELECT * FROM (
-
SELECT type, COUNT(*) AS cnt, item FROM table_name
-
GROUP BY item
-
ORDER BY cnt DESC
-
) tmp
-
GROUP BY type
Did you try this query? What do you get when you execute this query?
Well I get a
mysql_fetch_array(): supplied argument is not a valid MySQL result resource.
Maybe I should group by type in the inner query too?
Did you try this query? What do you get when you execute this query?
Originally Posted by mwasif
This will work for you - SELECT * FROM (
-
SELECT type, COUNT(*) AS cnt, item FROM table_name
-
GROUP BY item
-
ORDER BY cnt DESC
-
) tmp
-
GROUP BY type
Before using this query in your PHP code, execute this query in MySQL and see if it gives you the required results. And replace table_name with your actual table name and column names too.
Hi,
I spent the whole day trying to figure out a valid mysql query which includes MAX and COUNT and the same time: suppose I have the following table recording some bought items
ITEM TYPE
banana fruit
banana fruit
apple fruit
carrot vegetable
tomato vegetable
tomato vegetable
tomato vegetable
I need to retrieve the maximum number of hits for each TYPE: something like
fruit 2 banana
vegetable 3 tomato
Still I can't imagine a single query to do all the job, any hints?
Thanks in advance should someone be inspired enough ;)
Josep
-
SELECT E.type,MAX(E.cnt_item) FROM
-
(SELECT item,COUNT(item) cnt_item,type from <table_name> GROUP by item,type) E
-
GROUP BY E.type
-
Wow perfect, that did the job!
looks surprisingly simple once worked out...very elegant!
Many thanks guys :)
Josep M -
SELECT E.type,MAX(E.cnt_item) FROM
-
(SELECT item,COUNT(item) cnt_item,type from <table_name> GROUP by item,type) E
-
GROUP BY E.type
-
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Rich Protzel |
last post by:
Hello,
So my table contains say 100,000 records, and I need to group the
categories in fld1 by the highest count of subcategories. Say fld1
contains categories A, B, C, D, E.
All of these...
|
by: Ross Presser |
last post by:
The purpose, as you can probably guess, is to produce a set of sample
documents from a large document run. The data row has a CLUB column and an
IFC column; I want a set of samples that contains...
|
by: Jean-Christophe Michel |
last post by:
Hi,
I have a stylesheet with an accumulator like this
<xsl:param name="new-x">
<xsl:for-each select="anode">
<mynode>
<xsl:attribute name="attr">
<xsl:value select="anode/atag" />...
|
by: Ken |
last post by:
The fact that you can not reassign a variable in XSL is an endless
source of frustration, causing you to jump through all sorts of
non-intuitive hoops.
In this case, however, the lack of...
|
by: Karl Irvin |
last post by:
My program looks like this:
Dim db As DAO.Database, rst As DAO.Recordset
Set db = CurrentDb
Set rst = db.OpenRecordset("SELECT * FROM tUnpaidInvoiceIDs WHERE TxnType =
'Invoice'")
Do some...
|
by: geronimo_me |
last post by:
I have 20 queries that compare fields in one table with fields in
another table - the query results are the records that do not match in
Table1 and Table2. ie
Table1
DOB 28/02/78
Table2
DOB...
|
by: sophie_newbie |
last post by:
I have a list a little something like this:
StringA
StringC
StringB
StringA
StringC
StringD
StringA
....
|
by: heckstein |
last post by:
I am working in Access 2002 and trying to create a report from our company's learming management system. I am not a DBA and most of my SQL knowledge has been self taught through trial and error. I...
|
by: The Frog |
last post by:
Hello everyone,
I am working on an application that can build database objects in MS
Access from text files. I suppose you could call it a backup and
restore type routine.
Accessing the...
|
by: sammyboy78 |
last post by:
I need to create a method for sorting my CD objects by the CD Title. I have no idea where to begin. I haven't yet covered any sorting algorithms so I need to know how I can sort this thing. I'm not...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |