SQL statement Max(Count(*)) - mysql

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.

Related

How to find Maximum of average in group by clause in SQL

Hi I have a query like this
select avg(TotalSale) as value from Sales group by SalesPerson;
which gives me the output as
value
50.0000
250.0000
62.5000
I want to just get the max out of the values. What should i do?
I tried
select max(avg(TotalSale)) as value from Sales group by SalesPerson; which is wrong.
select avg(TotalSale) as value from Sales group by SalesPerson order by value desc limit 1; which works partially.
What would be better approach to do this?
Wrap your query....
select max(avgvalue)
from
(
select avg(totalsale) as avgvalue
from sales
group by salesperson
)z
Use order by and fetch one row:
select avg(TotalSale)
from Sales
group by SalesPerson
order by avg(TotalSale) desc
limit 1;
You can also get the SalePerson involved with this as well.
I guess it can be done in a single query .Try this :
SELECT
Max(Totalsale) as Value
FROM
Sales
ORDER BY avg(Totalsale)
LIMIT 1;

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 :)

max and count query

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;

Return Customer Number with highest number of order

Question,
You are given the table "ORDERS"
Return the customerNumber to the customer that has placed the highest number of order(it is guaranteed that there is only one customer with the most orders).
My guess was to return highest repeated customer number but couldn't get the exact syntax.
My Answer (Doesn't Work)
SELECT 'customerNumber'
FROM 'ORDERS'
GROUP BY 'customerNumber'
ORDER BY COUNT(*)
DESC LIMIT 1;
SELECT customerNumber
FROM ORDERS
GROUP BY customerNumber
ORDER BY COUNT(*)
DESC LIMIT 1;
Write your MySQL query statement below
SELECT customer_number FROM orders
GROUP BY customer_number
ORDER BY count(order_number) DESC
LIMIT 1;

MySQL query to find top items

I have a table called order_list with the following fields
order_id
user_id
item_id
count
date
order_status
every time users place order, this table used to save order details. so how can I write my SQL query to find top 10 items based on the sum of ordered count?
SELECT item_id, SUM(count) FROM order_list GROUP BY item_id ORDER BY SUM(count) DESC LIMIT 0,10
SELECT *
FROM table_name
GROUP BY item_id
ORDER BY count DESC
LIMIT 10
select item_id, sum(count) as total
from order_list
group by item_id
order by total desc
limit 10