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;
Related
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 :)
I'm a beginner at SQL, how do I get a query which returns the most prevalent column value? Probably there is an answer somewhere but I don't know how to google it.
For example in the user_id column the query should return the value 1 because this is the most prevalent number.
One approach is to do a GROUP BY aggregation and then apply a LIMIT trick:
SELECT user_id, COUNT(*) AS cnt
FROM yourTable
GROUP BY user_id
ORDER BY COUNT(*) DESC
LIMIT 1;
If you want something more complex, then you would be getting into the realm of rank functionality. MySQL (at least as of the current release) does not support built-in rank support, so it can be tricky to perform such queries.
SELECT top 1 user_id, COUNT(*) AS cnt
FROM yourTable
GROUP BY user_id
ORDER BY COUNT(*) DESC
Have a common table expression that counts each user_id. Select user_id where the count is the max count. Will return both user_id's in case of a tie.
with cte as
(
SELECT user_id, COUNT(*) AS cnt
FROM yourTable
GROUP BY user_id
)
select user_id
from cte
where cnt = (select max(cnt) from cte)
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;
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;
please forgive me if this has been answered, but could not find it using the search tool or a basic google query.
I am trying to return a value that indicates the maximum number of rows any distinct value in a column in SQL.
For example, I'd like to use something like
SELECT MAX(COUNT(DISTINCT person_id) AS MAX_NUM_PERS_ROW
FROM mytable
and if the person with most rows in the table had 5 rows, the value returned would be 5...
Any and all help is appreciated!
You can do this with nested aggregation:
select max(cnt)
from (select person_id, count(*) as cnt
from mytable
group by person_id
) p;
If you actually want the person, you can also do:
select person_id, count(*) as cnt
from mytable
group by person_id
order by count(*) desc
limit 1;