Mysql query - GROUP BY and show the highest COUNT(*) - mysql

I'm doing a small project and I'm trying to get the restaurant with the most votes each week to be displayed once.
This is my query:
SELECT votedRestaurant,
week,
COUNT(*)
FROM mylunch.votes
GROUP BY votedRestaurant,
week
ORDER BY week DESC;
This gets me the following result:
I would only like to have the one with the highest COUNT(*) displayed per week.
Thanks for any help.

you can try use LIMIT and ordering by count, for example
SELECT votedRestaurant,
week,
COUNT(*) AS tcount
FROM mylunch.votes
GROUP BY votedRestaurant,
week
ORDER BY tcount DESC
LIMIT 1;
Also, you can use subquery, so says the documentation.

Using Mysql 8 you can make use of window functions
WITH
cte AS (SELECT votedRestaurant, WEEK, COUNT(*) total
FROM votes
GROUP BY votedRestaurant,WEEK
ORDER BY WEEK DESC, votedRestaurant)
SELECT *
FROM (
SELECT *,
row_number() over (PARTITION BY WEEK ORDER BY total DESC) AS rn
FROM
cte
) t
WHERE rn = 1
Demo
Another way would be by using string fuctions
SELECT t.week, SUBSTRING_INDEX(GROUP_CONCAT(t.votedRestaurant ORDER BY t.total DESC),',',1) votedRestaurant, MAX(t.total)
FROM(
SELECT votedRestaurant, WEEK, COUNT(*) total
FROM votes
GROUP BY votedRestaurant,WEEK
ORDER BY WEEK DESC
) t
GROUP BY t.week
ORDER BY t.week DESC
Demo

Related

mysql select row before max value

My Requirement is to get the row before the max(create_date).
SELECT servicecalls_servicecall_id, max(created_at) FROM
service_followup_details where servicecalls_servicecall_id IN (SELECT
service_call_id from service_calls where status=2) group by
servicecalls_servicecall_id
How can I do it by MySQL query?
Get the max(create_at), then pick the one which is less than that max value.
select servicecalls_servicecall_id,created_at
from `service_followup_details`
where created_at<(
SELECT max(created_at)
FROM `service_followup_details`
where servicecalls_servicecall_id IN
(SELECT service_call_id from service_calls
where status=2)
group by servicecalls_servicecall_id
)
order by created_at desc
limit 1
;
Well you can achieve this with the help of ROW_NUMBER() function.
Check my implementation below. What I've done is, I'm sorting the records by created_at in a DESC order. Off course, this is partitioned by the servicecalls_servicecall_id so the sequence will restart for each new servicecalls_servicecall_id. Then you can simply pick the records that have row_number=2 to get the closest record to the max created_date.
SELECT servicecalls_servicecall_id,
ROW_NUMBER() OVER (partition by servicecalls_servicecall_id ORDER BY created_at DESC) AS row_number
FROM service_followup_details
WHERE servicecalls_servicecall_id IN (
SELECT service_call_id FROM service_calls WHERE status=2
)
AND row_number = 2;

Getting a win streak from a database

I have followed this tutorial on trying to get a win streak from my database of bets.
The data has a result (Win/Loss/Pending) and a date (Amongst other values)
Here is the SQL command I'm using...
SELECT *
FROM (SELECT Result,
MIN(date) as StartDate,
MAX(date) as EndDate,
COUNT(*) as Games
FROM (SELECT *,
(SELECT COUNT(*)
FROM bets G
WHERE G.result <> GR.result
AND G.date <= GR.date) as RunGroup
FROM bets GR WHERE user = 4 ORDER BY date DESC) A
GROUP BY result, RunGroup
ORDER BY Min(date)) A
WHERE result = 'Win'
ORDER BY Games DESC
The only difference with mine is I'm trying to filter a single users bets and not everyones bets but...I can see in my DB that there are 3 Win's in a row but my output is 2. Can anyone spot where I have gone wrong?
I want to get one row with the largest streak, which all I think I would need to do at this point is add LIMIT 1
Thanks
If you want all sequential wins summarized in one row, then I would suggest handling this has a gaps-and-islands problem. A simple method is to count the cumulative number of non-wins. This then assigns a group to each group of wins that can be used for aggregation:
select user, grp, count(*) as num_in_sequence,
min(date), max(date)
from (select b.*,
sum(result <> 'Win') over (partition by user order by date) as grp
from bets b
) b
where result = 'Win'
group by user, grp;
This is how I did it:
SELECT * FROM (SELECT result, MIN(date) as StartDate, MAX(date) as EndDate, COUNT(*) as Games FROM (SELECT date, result, (SELECT COUNT(*) FROM bets G WHERE G.result <> GR.result AND G.date <= GR.date AND user = ${uid}) as RunGroup FROM bets GR WHERE user = ${uid} ORDER BY date) A Where result = 'Win' GROUP BY result, RunGroup ORDER BY Min(date)) A ORDER BY Games DESC LIMIT 1

To find the maximum number of order count that occur in any 1 hour of the day from the database?

I have a food selling website in which there is order table which record the order of every user.It column for user id ,user name,orderid ,timestamp of order.I want to know the maximum number of order that has been made in any one hour span through out the day.Give me any formula for this,or any algorithm or any sql queries for these.
SQL server:
with CTE as
(
select cast(t1.timestamp as date) as o_date, datepart(hh, t1.timestamp) as o_hour, count(*) as orders
from MyTable t1
group by cast(t1.timestamp as date), datepart(hh, t1.timestamp)
)
select o_date, o_hour, orders
from CTE
where orders = (select max(orders) from CTE)
Oracle
with CTE as
(
select to_char(t1.timestamp, 'YYYYMMDD') as o_date, to_char(t1.timestamp, 'HH24') as o_hour, count(*)
from MyTable t1
group by to_char(t1.timestamp, 'YYYYMMDD'), to_char(t1.timestamp, 'HH24')
)
select o_date, o_hour, orders
from CTE
where orders = (select max(orders) from CTE)
You can get count by day and hour like this
For SQL
SELECT TOP 1
COUNT(*)
FROM myTable
GROUP BY DATEPART(day, [column_date]), DATEPART(hour, [column_date])
ORDER BY COUNT(*) DESC;
For MySQL
SELECT
COUNT(*)
FROM myTable
GROUP BY HOUR(column_date), DAY(column_date)
ORDER BY COUNT(*) DESC
LIMIT 1;

Most common hour query?

I have this table:
ID(INT) DATE(DATETIME)
Under the DATE column there are a lot of different dates, and I want to figure out the most common hour between all the rows of the table, regardless of the day.
How can I do that with a MySQL query?
SELECT HOUR(date) AS hr, COUNT(*) AS cnt
FROM yourtable
GROUP BY hr
ORDER BY cnt DESC
LIMIT 1
relevant docs: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_hour
Try this -
SELECT HOUR(`DATE`) AS `hour`, COUNT(*)
FROM `table`
GROUP BY `hour`
You could do a query like:
SELECT COUNT(daterow) AS occurrences FROM table GROUP BY daterow ORDER BY occurrences DESC LIMIT 1;
SELECT COUNT( id ) , HOUR( date )
FROM test
GROUP BY HOUR( date )
ORDER BY COUNT( id ) DESC
LIMIT 1

Most common week days query

Have this query:
SELECT HOUR( DATE ) AS hr, COUNT( * ) AS cnt
FROM users
GROUP BY hr
ORDER BY cnt DESC
DATE is a DATETIME field and the above query shows me the most common hours in the date field.
I'm trying to improve it but not sure how to do it, I want to break it down by week days and within each week day to most common hours.
Try this query -
SELECT
DAYOFWEEK(DATE) AS wd,
HOUR(DATE) AS hr,
COUNT(*) AS cnt
FROM
users
GROUP BY
wd, hr
ORDER BY
cnt DESC
SELECT
DATE_FORMAT(DATE, '%a') AS wd, -- or DATE_FORMAT(DATE, '%W')
HOUR(DATE) AS hr,
COUNT(*) AS cnt
FROM
users
GROUP BY
wd, hr
ORDER BY
cnt DESC
Let me know if below works:
SELECT dayname(DATE) as week_day,HOUR( DATE ) AS hr, COUNT( * ) AS cnt
FROM users
GROUP BY week_day, hr ORDER BY cnt DESC