max and count query - mysql

I'm trying to make a query with max and count like this one: (taken from http://www.w3resource.com/sql/aggregate-functions/max-count.php)
SELECT MAX (mycount)
FROM (SELECT agent_code,COUNT(agent_code) mycount
FROM orders
GROUP BY agent_code);
this query returns a column with the name 'MAX(MYCOUNT)' with the max value: '7',the simple change I want is that I would like to get the agent code of the one who got the maximum, instead of the max records of agent code.
tried to do this in some ways but no luck so far,
hope you can help me to do this right.

If you don't have to worry about a tie for two agents having the max number of orders, then you can try the following:
SELECT agent_code, COUNT(*) AS mycount
FROM orders
GROUP BY agent_code
ORDER BY COUNT(*) DESC
LIMIT 1
If you do have to worry about a tie for the max number of orders, and you want all ties, then you can use a subquery:
SELECT agent_code, COUNT(*) AS mycount
FROM orders
GROUP BY agent_code
HAVING COUNT(*) = (SELECT MAX(t.mycount) FROM
(SELECT COUNT(*) AS mycount FROM orders GROUP BY agent_code) t)

You can use order by and limit:
SELECT agent_code, COUNT(agent_code) as mycount
FROM orders
GROUP BY agent_code
ORDER BY mycount DESC
LIMIT 1;

Related

How to sum when they have same color with sql?

I have this table
i want to ignore productNo and sum all product count accordingly.
select sum(count), max(productNo)
from Table
where date between 117 and 118
group by product
this one gives wrong result...
I want to have sum of counts for each Product-ProductNo combination
try like below
select product,productno,sum(count) as result
from table_name
where productno='X1'
group by product,productno
seems you need the firts rows order by result
select product,productno,sum(count) as result
from table
group by product,productno
order by result
limit 1
Since you haven't tagged any DBMS so, i would use row_number():
select t.*
from (select product, productno, sum(count) as cnt,
row_number() over (partition by product order by sum(count) desc) as seq
from table t
group by product, productno
) t
where seq = 1;
You can also use LIMIT clause (but not for each product) :
select product, productno, sum(count) as cnt
from table t
group by product, productno
order by cnt desc
limit 1;
Some other DBMS requires TOP clause instead of LIMIT clause so, you can change accordingly but the idea would be same.
select sum(count), max(productNo)
from Table
where date between 117 and 118
group by product, productNo
with this it works :)

How to display only value that occurs second highest number of times?

Write a query to display the customer name who visited the second highest number of times
select customer_id,count(*) from booking group by customer_id ;
using this query i got the count of number of visits for each customer as shown below
CUSTOMER_ID,COUNT(*)
C001,6
C002,1
C003,1
C004,1
C005,4
but i want to display only c005 since he has visited the second maximum time
SELECT customer_id, COUNT(*)
FROM booking
GROUP BY customer_id
HAVING COUNT(*) <> (SELECT MAX(t.custCount)
FROM (SELECT COUNT(*) AS custCount
FROM booking
GROUP BY customer_id) t )
ORDER BY COUNT(*) DESC
LIMIT 1
As a side note, this won't work if there are ties for second place. In this case, you use the above query as a condition in the WHERE clause, e.g.
SELECT customer_id
FROM booking
GROUP BY customer_id
HAVING COUNT(*) = (query given above)
You can use a outer query and filter the same like
select customer_id from (
select customer_id,
count(*) as datacount
from booking
group by customer_id ) xxx
order by datacount desc
limit 1;

What is the best way to select rows with maximum value?

I have come across a task, I managed to complete the objective but the solution I got is not optimum, I need more optimum solution. I have used normal Sub Queries May be Correlated Sub Query can solve this better.
This is the table i made
SELECT custid,
count(DISTINCT bid) AS Total
FROM loan
GROUP BY custid;
The output of this is like:-
What I want is the custid having maximum Total.
One way to do it is using Order by Total DESC LIMIT 1 but this will give only 1 result.
What I did is
SELECT custid
FROM (SELECT custid,
count(DISTINCT bid) AS Total
FROM loan
GROUP BY custid) c1
WHERE total = (SELECT max(Total)
FROM (SELECT custid,
count(DISTINCT bid) AS Total
FROM loan
GROUP BY custid) c2)
This gives me correct result that is
What I want to do is reduce the code, because here I am writing the same thing again. I know there must be a simpler way to do it. Maybe a correlated query.
Looking for some good answers. This is basically to clear my concepts only
Sorry, if it is noob question. I am a noob to SQL.
After understand what OP want with #Ravinder 's tip,
I guess build in mysql function GROUP_CONCAT is what you need, sql is:
select custid_count.Total, GROUP_CONCAT(custid_count.custid order by custid_count.custid asc SEPARATOR ',') as custids from
(select custid, count(distinct bid) as Total from loan group by custid order by Total desc) as custid_count
group by custid_count.Total
order by custid_count.Total desc
limit 1;
the result column custids is the max ids concated by ',' ,after the query, you need to split custids by ',' and convert each substring to number type you need,
Here is another way:
select * from loan
where custid =
(
select custid_count.custid from
(select custid, count(distinct bid) as Total from loan group by custid order by Total desc) as custid_count
order by custid_count.Total desc
limit 1
);
First find the custid with max count, then query all rows which match the custid,
I haven't tried this in mysql, but in the sql language I'm using it is fine to use a aggregation function without a group by so something like this
select custid, total, max(total) as maxtotal
from (select custid, count(distinct bid) as total
from loan
group by custid) c1;
would tag on every line both the individual customer total and the table wide max total, and you'd just have to filter on the ones that where the total was equal to the max total. That would give you a final answer of something like this:
select custid
from (select custid, count(distinct bid) as total
from loan
group by custid) c1
where total = max(total);

SQL statement Max(Count(*))

Basically I have a review table for product. The attributes are reviewID, reviewCustName, reviewText, productID. So I wonder is there any ways to count the product with most reviews? Here is my SQL statement:
SELECT productID, count(*) AS mostReviews, MAX(mostReviews) FROM sm_review GROUP BY productID;
I wonder is it possible to write such SQL statement? Or i there any better way?
Thanks in advance.
You can use the following to get the result. This gets the total count for each product but when you order the count in a descending order and apply LIMIT 1 it returns only the product with the most reviews:
select count(*) total
from sm_review
group by productId
order by total desc
limit 1
It should just be;
SELECT count(*) AS num_reviews FROM sm_review
GROUP BY productID ORDER BY num_reviews DESC LIMIT 1;
Note the ORDER BY num_reviews and the LIMIT 1 which limits the number of results.

MySql sum query

I need a MySql query to do the following:
Select all the items in a table, group them by a type, order them by count type and also sum all the count.
This is what I've done so far:
select type, count(*) as cnt from $tbl_name group by type order by count(*) desc;
This only gives me the count for each group. What should I add so that this code will also show the total count (sum the counts from every group).
select * from (
select type as type,
count(*) as cnt
from $tbl_name
group by type WITH ROLLUP) as inner_table
order by cnt desc;
Note that the first row will be the rolled up total sum.
ROLLUP reference
Try this:
select type, SUM(count(*)) as cnt from $tbl_name group by type order by count(*) desc;