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

Max of each count

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
Sep 8 '07 #1
7 2177
mwasif
802 Expert 512MB
This will work for you
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM (
  2.     SELECT type, COUNT(*) AS cnt, item FROM table_name
  3.     GROUP BY item
  4.     ORDER BY cnt DESC
  5.     ) tmp 
  6. GROUP BY type
Sep 9 '07 #2
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
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM (
  2.     SELECT type, COUNT(*) AS cnt, item FROM table_name
  3.     GROUP BY item
  4.     ORDER BY cnt DESC
  5.     ) tmp 
  6. GROUP BY type
Sep 9 '07 #3
mwasif
802 Expert 512MB
Did you try this query? What do you get when you execute this query?
Sep 9 '07 #4
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

Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM (
  2.     SELECT type, COUNT(*) AS cnt, item FROM table_name
  3.     GROUP BY item
  4.     ORDER BY cnt DESC
  5.     ) tmp 
  6. GROUP BY type
Sep 9 '07 #5
mwasif
802 Expert 512MB
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.
Sep 9 '07 #6
amitpatel66
2,367 Expert 2GB
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
Expand|Select|Wrap|Line Numbers
  1. SELECT E.type,MAX(E.cnt_item) FROM
  2. (SELECT item,COUNT(item) cnt_item,type from <table_name> GROUP by item,type) E
  3. GROUP BY E.type
  4.  
Sep 10 '07 #7
Wow perfect, that did the job!
looks surprisingly simple once worked out...very elegant!
Many thanks guys :)

Josep M

Expand|Select|Wrap|Line Numbers
  1. SELECT E.type,MAX(E.cnt_item) FROM
  2. (SELECT item,COUNT(item) cnt_item,type from <table_name> GROUP by item,type) E
  3. GROUP BY E.type
  4.  
Sep 10 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

3
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...
5
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...
4
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" />...
2
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...
11
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...
6
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...
16
by: sophie_newbie | last post by:
I have a list a little something like this: StringA StringC StringB StringA StringC StringD StringA ....
1
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...
17
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...
1
sammyboy78
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...
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: 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...
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
tracyyun
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...

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.