Most common week days query - mysql

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

Related

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

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

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;

MySQL - max( ) row values of subquery

I have a table in which the rainfall is recorded every 15 minutes, so the total rainfall for a day is the sum of 'rain' in 95 records. The following query correctly returns a list of dates and the rainfall for that day.
select thedate as tdate,sum(rain) as dailyrain
from weatherdata
group by year(thedate), month(thedate), day(thedate)
(thedate stored as datetime, rain stored as integer )
I now want to make this a sub query and return the date on which there was maximum rainfall.
My query below correctly returns the maximum rain amount, but not the correct date linked with that rainfall.
select maxdate,max(dailyrain) from (select thedate as maxdate,sum(rain) as dailyrain
from weatherdata
group by year(thedate), month(thedate), day(thedate)) as maxrain
I think I probably have to use a JOIN. I know many similar questions have been answered here but I can't quite get the syntax right to make this work. Any help would be much appreciated.
SELECT DATE(thedate) as tdate, sum(rain) as dailyrain
FROM weatherdata
GROUP BY DATE(thedate)
ORDER BY sum(rain) DESC, DATE(thedate) DESC
LIMIT 1 ;
or:
SELECT DATE(thedate) as tdate, sum(rain) as dailyrain
FROM weatherdata
GROUP BY DATE(thedate)
ORDER BY dailyrain DESC, tdate DESC
LIMIT 1 ;
To get all dates having the maximal rainfall:
SELECT DATE(thedate), SUM(rain)
FROM weatherdata
GROUP BY DATE(thedate)
HAVING SUM(rain) = (
SELECT SUM(rain)
FROM weatherdata
GROUP BY DATE(thedate)
ORDER BY SUM(rain) DESC
LIMIT 1
)
please try this
select tdate,dailyrain from
(select date(thedate) as tdate,sum(rain) as dailyrain
from weatherdata
group by date(thedate))r
WHERE dailyrain =
(select max(dailyrain) as maxrain from
(select date(thedate) as tdate,sum(rain) as dailyrain
from weatherdata
group by date(thedate)
)m
)
sqlFiddle

Using grouped value instead of table value in MySQL query

I need to group some data by date, but I have a very special case where the name of the final field should be the same of the original field and I can't use an expression in the GROUP BY
I have created this sqlfiddle with some example data:
http://sqlfiddle.com/#!2/8771a/1
I need this result:
DATE PAGEVIEWS
2013-12 69
2013-11 70
Note 1: I can't change the group by, if I do this I get the result, but I need to group by date and date should be the formatted date, and not the real date in the table:
SELECT DATE_FORMAT(`date`, "%Y-%m") AS `date`, SUM(pageviews) AS pageviews
FROM `domains_data`
GROUP BY DATE_FORMAT(`date`, "%Y-%m")
ORDER BY `date` DESC
Note 2: I can't rename the field, it should have the name "date" at the end, this isn't possible for me:
SELECT DATE_FORMAT(`date`, "%Y-%m") AS `date2`, SUM(pageviews) AS pageviews
FROM `domains_data`
GROUP BY `date2`
ORDER BY `date` DESC
There is some way to do it with MySQL?
Is the use of nested query allowed?
SELECT `date`, SUM(pageviews) AS pageviews FROM
(SELECT DATE_FORMAT(`date`, "%Y-%m") AS `date`, SUM(pageviews) AS pageviews
FROM `domains_data`
GROUP BY `date`) AS Ref
GROUP BY `date`
ORDER BY `date` DESC
If you can change the from clause:
SELECT `date`, SUM(pageviews) AS pageviews
FROM (select DATE_FORMAT(`date`, "%Y-%m") AS `date`, pageviews
from `domains_data` dd
) dd
GROUP BY `date`
ORDER BY `date` DESC;
However, given the constraint that you cannot change the group by, you probably cannot change the from either. Can you explain why your query has these limitations?

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