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

Can I use Max and Count Together?

Always finding answers here so finally posting a question!
I have a MySQL query and I am trying to get a max count to help replicate an Access Pivot table.
Query:
Expand|Select|Wrap|Line Numbers
  1. SELECT DISTINCT oTech, Count(sysID) as totRows
  2. FROM view_schedule where SchedDate > '2007-08-01'
  3. GROUP BY oTech, SchedDate
  4. ORDER BY oTech ASC, totRows DESC;
Returns a table like this:
oTech.................totRows
4..............................7
4..............................3
4..............................1
6..............................2
6..............................1
7..............................1
9..............................2

Problem:
I only need the top row for each different oTech. I can skip other rows in my PHP code but was curious if there was a way to just get the maximum count for each oTech...
Aug 8 '07 #1
5 6830
pbmods
5,821 Expert 4TB
Heya, Andrew. Welcome to TSDN!

Please use CODE tags when posting source code. See the REPLY GUIDELINES on the right side of the page next time you post.

Try adding a LIMIT clause:
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.     DISTINCT
  3.         `oTech`,
  4.         COUNT(`sysID`)
  5.             AS `totRows`
  6.     FROM
  7.         `view_schedule`
  8.     WHERE
  9.         `SchedDate` > '2007-08-01'
  10.     GROUP BY
  11.         `oTech`,
  12.         `SchedDate`
  13.     ORDER BY
  14.         `oTech` ASC,
  15.         `totRows` DESC
  16.     LIMIT 1;
  17.  
Aug 8 '07 #2
Thanks, I appreciate the tip and help. However, I want the first data row each time the oTech is different so I want this data so LIMIT 1 does not work. I do know how to go through it in PHP to skip data I don't need but was looking for a way to do it in MySQL.

Data I Want
oTech.................totRows
4..............................7
6..............................2
7..............................1
9..............................2
Aug 8 '07 #3
pbmods
5,821 Expert 4TB
Heya, Andrew.

Oh I see.

Maybe this will work:
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.     DISTINCT
  3.         `oTech`,
  4.         MAX(COUNT(`sysID`)) -- <--
  5.             AS `totRows`
  6.     FROM
  7.         `view_schedule`
  8.     WHERE
  9.         `SchedDate` > '2007-08-01'
  10.     GROUP BY
  11.         `oTech`,
  12.         `SchedDate`
  13.     ORDER BY
  14.         `oTech` ASC,
  15.         `totRows` DESC
  16.  
Otherwise, I'm thinking that you'd need to use a subquery to do this.

Maybe something like:
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.         `oTech`,
  3.         MAX(`totRows`)
  4.             AS `totRows`
  5.     FROM
  6.     (
  7.         SELECT
  8.             DISTINCT
  9.                 `oTech`,
  10.                 COUNT(`sysID`)
  11.                     AS `totRows`
  12.             FROM
  13.                 `view_schedule`
  14.             WHERE
  15.                 `SchedDate` > '2007-08-01'
  16.             GROUP BY
  17.                 `oTech`,
  18.                 `SchedDate`
  19.             ORDER BY
  20.                 `oTech` ASC,
  21.                 `totRows` DESC
  22.     )
  23.     GROUP BY
  24.         `oTech`
  25.  
EDIT: Is the DISTINCT qualifier necessary here? Not sure.
Aug 8 '07 #4
Thanks, I added "AS VS" alias after the sub-query end parenthesis and before the main query group by and that works great. The DISTINCT was not needed and actually got in the way of other data oddly enough so I removed it.

Thanks for your help!
Andrew
Aug 9 '07 #5
pbmods
5,821 Expert 4TB
Heya, Andrew.

Glad to hear you got it working! Good luck with your project, and if you ever need anything, post back anytime :)
Aug 9 '07 #6

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

Similar topics

3
by: rob.guitar.rob | last post by:
Hello, My last few posts have been revolving aroung the same problem, and I still cant solve it and I would be really appreciate if anyone could spot a problem. a section of my XML goes like...
22
by: Joseph Shraibman | last post by:
On a 7.3.4 database: explain analyse select count(*) from elog; Aggregate (cost=223764.05..223764.05 rows=1 width=0) (actual time=81372.11..81372.11 rows=1 loops=1) -> Seq Scan on elog ...
2
by: pierrelap | last post by:
Hello, I need to code a query that: 1-counts the number of time two companies have been in a deal together 2-in the five years that preceded the deal Lead Participant DealDate AAA BBB ...
3
by: amanda | last post by:
Hope someone can help me with this - I've been staring at it stupidly for hours now, convinced there must be an easy way to achieve the results I want: I have a very large table recording every...
3
by: Kuups | last post by:
Hi! I have a question regarding the count if character within a string like for example I have a string of e.g. 123#123# I would like to determine what is the code? of getting the # sign
5
by: Genalube | last post by:
I am trying to count the number of owners that show up in a query (conveyQuery). The query will produce a column OwnName that will contain names like John Smith, Mike Jones, Frank Vaugn. Each of...
4
by: gab | last post by:
Hi :-) I have a table with X and Y (which are strings), I would like to get an output in the form of one colum with X and Y together, and a colum with the number of times the strings is...
2
by: jadeverell | last post by:
Hi, I am trying to perform a calculation that adds together two 'Counts' (on seperate tables) to give a total value. The code for the two counts is: Count(Tbl_Contract_Maintenance.) AS ,...
2
by: hall.jeff | last post by:
Before the inevitable response comes, let me assure you I've read through the posts from Guido about this. 7 years ago Guido clearly expressed a displeasure with allowing these methods for tuple....
5
by: stateemk | last post by:
I am trying to run two queries. One query is doing a count to figure out how many total sales there were each year based on the assessor. That query is working fine. On the other query, I'm trying...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.