0

I was looking for a way to accommodate there being more than one result for finding a max and min. I found this link to a previous question: max Counts

One of the answers given was:

SELECT  color_id, COUNT(color_id) totalCount
FROM    products 
WHERE   item_id = 1234 
GROUP   BY color_id 
HAVING  COUNT(color_id) =
(
  SELECT  COUNT(color_id) totalCount
  FROM    products 
  WHERE   item_id = 1234 
  GROUP   BY color_id 
  ORDER BY totalCount DESC
  LIMIT 1  
)

Is this accepted practice especially for large databases? Isn't the above query basically running itself within itself if that makes sense?

I have a more complicated query also requiring to find the ma and min. I'd like to optimize it:

Edit:

SELECT `system_users`.`first`, `system_users`.`last`,  COUNT(`quotes`.`created_by`) as most_quotes
FROM `quotes` 
INNER JOIN `system_users`
ON `quotes`.`created_by` = `system_users`.`id`
where `system_users`.`store_id` = '$createdID' 
and `quotes`.`date_created` between '$startDate' and '$endDate' group by(`created_by`)
HAVING count(`quotes`.`created_by`) = 
(
SELECT COUNT(`quotes`.`created_by`)
FROM `quotes` 
INNER JOIN `system_users`
ON `quotes`.`created_by` = `system_users`.`id`
where `system_users`.`store_id` = '$createdID' 
and `quotes`.`date_created` between '$startDate' and '$endDate' group by(`created_by`) ORDER BY count(`created_by`) DESC limit 1
)
OR
(
SELECT COUNT(`quotes`.`created_by`)
FROM `quotes` 
INNER JOIN `system_users`
ON `quotes`.`created_by` = `system_users`.`id`
where `system_users`.`store_id` = '$createdID'
and `quotes`.`date_created` between '$startDate' and '$endDate' group by(`created_by`) ORDER BY count(`created_by`) ASC limit 1
)
ORDER BY most_quotes ASC

I'm trying to think of different ways to find the max and min with no luck so far. Any more help on this would be greatly appreciated Thanks mc

0

2 Answers 2

1

That is a bad idea - to use HAVING on large databases. And, besides, your problem can be solved this way (I have MySQL 5.5 version):

SELECT  
  color_id, 
  COUNT(color_id) AS totalCount
FROM    
  products 
WHERE   
  item_id = 1234 
GROUP BY 
  color_id 
ORDER BY 
  totalCount DESC 
LIMIT 1

The problem with HAVING is that it is executed after whole query was done, i.e. storage-engine already worked, so no indexes or other optimizations could be done for HAVING conditions - and, therefore, it can be treated as a full result set scan.

Thanks to @GordonLinoff I've found that it is not exactly the thing you wanted. If you're trying to find all corresponding rows, you'll better act like Gordon suggested.

While I have found another way to solve this, it may be only a couple better than original variant with HAVING (and - better because storage-engine will be involved both times)

SELECT
  first.color_id, 
  first.rows_count 
FROM 
  (SELECT color_id, COUNT(1) AS rows_count FROM products WHERE item_id=1234 GROUP BY color_id) AS first 
  LEFT JOIN
  (SELECT color_id, COUNT(1) AS rows_count FROM products WHERE item_id=1234 GROUP BY color_id ORDER BY rows_count DESC LIMIT 1) AS second 
    ON first.rows_count=second.rows_count 
WHERE second.rows_count IS NOT NULL;

I've also got variant with variables (similar to Gordon's one). So you can choose between these options.

5
  • This may be a more efficient query, but it does not answer the question. The question specifically states that more than one "color_id" may meet the max condition. This returns only one value. Aug 20, 2013 at 13:24
  • Hm.. @GordonLinoff, you're correct in that - since the query above will return only one row. I'll correct.
    – Alma Do
    Aug 20, 2013 at 13:26
  • Thanks for the responses. although my query is a bit more complicated than that. As it stands this is what I have, for finding a max and min:
    – danw
    Aug 20, 2013 at 15:19
  • please see my edit above, I couldn't add my query as a comment. I would like to accomodate for more than one max and min, but have no idea how
    – danw
    Aug 20, 2013 at 15:31
  • @GordonLinoff could you suggest a solution for my appended quote that finds the max and min? Thanks
    – danw
    Aug 22, 2013 at 9:40
0

The answer to your question is that there are multiple ways to approach this in MySQL using one query.

One way is the method in your question. Another would be to use variables:

select color_id, totalcount
from (SELECT color_id, COUNT(color_id) as totalCount,
             @maxcount = if(@maxcount < count(color_id), count(color_id), @maxcount)
      FROM products cross join
           (select @maxcount := 0) const
     WHERE item_id = 1234 
     GROUP BY color_id 
    ) t
where totalCount = @maxcount;

I am not a big fan of using variables in MySQL, but oftentimes they are the only way to express a query efficiently. I believe this is the most efficient way to write what you want as a single query.

A third way would be to use a temporary table. And a fourth way would be to move the subquery in the having condition to another join.

In most other databases, you would simply use window functions. Something like:

select color_id, totalcount
from (SELECT color_id, COUNT(color_id) as totalCount,
             max(count(color_id)) over () as maxTotalCount
      FROM products
     WHERE item_id = 1234 
     GROUP BY color_id 
    ) t
where totalCount = maxTotalCount;

However, MySQL does not support these.

EDIT:

If you want the max and the min using variables:

select color_id, totalcount
from (SELECT color_id, COUNT(color_id) as totalCount,
             @maxcount = if(@maxcount < count(color_id), count(color_id), @maxcount),
             @mincount = if(@mincount > count(color_id) or @mincount = -1, count(color_id), @mincount)
      FROM products cross join
           (select @maxcount := -1, @mincount := -1) const
     WHERE item_id = 1234 
     GROUP BY color_id 
    ) t
where totalCount in (@mincount, @maxcount);
2
  • could you suggest a work around with my edited query above? Thanks
    – danw
    Aug 21, 2013 at 11:05
  • I ran this query and it returned empty columns
    – danw
    Aug 23, 2013 at 19:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.