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
Related
How can i find the employee id, date and minimum_time and maximum_time order by id,date,time from this table?
SELECT emp_id,
date,
MIN(time) AS in_time,
MAX(time) AS out_time
FROM yourTable
GROUP BY emp_id,
date
ORDER BY emp_id, date
The MIN() and MAX() functions will find the earliest and latest time of each employee, on each date.
It's simple your query syntax would be like
SELECT atr1, atr2,.. FROM table_name ORDER BY atr1, atr2,..
Try:
SELECT `emp_id` , `date` , MIN( `time` ) as in_time , MAX( `time` ) as out_time
FROM `Table`
WHERE 1
GROUP BY `date`
ORDER BY `time` DESC
LIMIT 0 , 30
As i can see there are same emp_id for many dates, so group by date will get all data for each date.
You can just use the SELECT Query
SELECT employee.id, employee.date, employee.minimum_time, employee.maximum_time
From table_name
ORDER BY employee.id
I need to select first value for every hour from my db. But I don't know how to reverse order on GROUP BY statement.
How can i rewrite my query (now it selects last value in hour)?
SELECT HOUR(`time`) as hour, mytable.*
FROM mytable
WHERE DATE(`time`) ="2015-09-12" GROUP BY HOUR(`time`) ORDER BY `time` ASC;
This query gave me expected result:
SELECT HOUR(`time`) as hour, sortedTable.* FROM
(SELECT electrolysis.* FROM electrolysis
WHERE DATE(`time`)='2015-09-12' ORDER BY `time`) as sortedTable
GROUP BY HOUR(`time`);
You can just select the MIN HOUR in sub query , try using the query:
SELECT * from mytable WHERE `time` IN (
SELECT MIN(HOUR(`time`)) as `hour`
FROM mytable
WHERE DATE(`time`) ="2015-09-12"
GROUP BY HOUR(`time`) ) ORDER BY `time` ASC;
You can do something like this:-
SELECT sub0.min_time,
mytable.*
FROM mytable
INNER JOIN
(
SELECT MIN(`time`) AS min_time
FROM mytable
GROUP BY HOUR(`time`)
) sub0
ON mytable.`time` = sub0.min_time
WHERE DATE(`time`) ="2015-09-12"
ORDER BY `time` ASC
This is using a sub query to get the smallest time in each hour. This is then joined back against your main table on this min time to get the record that has this time.
Note that there is a potential problem here if there are multiple records that share the same time as the smallest one for an hour. There are ways around this, but that will depend on your data (eg, if you have a unique id field which is always ascending with time then you could select the min id for each hour and join based on that)
You can use below query, which is more optimized just make sure that time field should be indexed.
SELECT HOUR(m.time), m.*
FROM mytable AS m
JOIN
(
SELECT MIN(`time`) AS tm
FROM mytable
WHERE `time` >= '2015-09-12 00:00:00' AND `time` <= '2015-09-12 23:59:59'
GROUP BY HOUR(`time`)
) AS a ON m.time=a.tm
GROUP BY HOUR(m.time)
ORDER BY m.time;
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?
i want to select the data in a table such that it should group it by userid except one value in that column and order by date time and desc.
The problem i am getting is the grouped items are not ordering by date and time and in desc manner.
I mean the grouped item is showing earlier row.
How can i do that.
This is what i have done.
SELECT * FROM `tbljobs`
GROUP BY user_id
UNION ALL
SELECT * FROM tbljobs
WHERE user_id = '1'
ORDER BY date_time DESC
LIMIT 20"
where '1' is should not be grouped.
Your ORDER BY is only executed on the second statement. You have to use braces to order the whole results:
(SELECT *
FROM `tbljobs`
GROUP BY user_id)
UNION ALL
(SELECT *
FROM tbljobs
WHERE user_id = '1')
ORDER BY date_time DESC
Thank you friends for your suggestions. Finally I created the solution by myself after a lot of effort.
(
SELECT *
FROM (
SELECT *
FROM tbljobs
ORDER BY date_time desc
) AS A
WHERE user_id <> '1' group by user_id
)
UNION ALL
(
SELECT *
FROM tbljobs
WHERE user_id=1
)
ORDER BY date_time DESC
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