I'm trying to group by Date(). I've got 3 records with the created_at column:
2011-12-03 08:00:24, 2011-12-03 08:12:10, 2011-12-04 09:00:00
I'd like to only group by year, month and day, regardless of time. So for the example above. It should only return two rows:
2011-12-03 and 2011-12-04
How should I go about this?
... group by date(date_time_column)
This should allow you to group by year month and day
SELECT group_by_column
, DATE_FORMAT(created_at, '%Y')
, DATE_FORMAT(created_at, '%m')
, DATE_FORMAT(created_at, '%d')
FROM my_table
GROUP BY group_by_column
or if you want to do them all together.
SELECT group_by_column
, DATE_FORMAT(created_at, '%Y%m%d')
FROM my_table
GROUP BY group_by_column
Did you try the following?
SELECT
DATE(created_at) AS created_date
FROM
my_table
GROUP BY
created_date
MySQL permits GROUP BY DATE(created_at). So that would translate in ActiveRecord to .group(DATE(created_at))
In fact, that exact example is available in the Rails Guides on ActiveRecord querying.
You can use the Date() function.
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date
mysql> SELECT DATE('2003-12-31 01:02:03');
-> '2003-12-31'
TRY
GROUP BY DATE(`date_column`)
Reference
Related
So I have a database table which contains id, action, timestamp for example. I want to get the unique days with corresponding hits/views.
Something like this:
Date ------ Hits
2018-01-10 5
2018-01-11 542
2018-01-12 74
My approach was this:
SELECT DISTINCT FROM_UNIXTIME (timecreated, '%d.%m.%y') AS 'date'
FROM `mdl_logstore_standard_log`
WHERE action = 'viewed'
That lists me all unique days but if I'll add count(action) AS 'hits' to the SELECT statement it will just show me the first day with the sum of all hits.
How can I solve this query? I'm grateful for any hints
You need to group results using GROUP BY
SELECT FROM_UNIXTIME (timecreated, '%d.%m.%y') AS `date`, COUNT(action) AS hits
FROM `mdl_logstore_standard_log`
WHERE action = 'viewed'
GROUP BY FROM_UNIXTIME (timecreated, '%d.%m.%y')
ORDER BY `date`
For your requested output, you should really do:
SELECT FROM_UNIXTIME(timecreated, '%Y-%m-%d') AS `date`, COUNT(*) AS hits
FROM `mdl_logstore_standard_log`
WHERE action = 'viewed'
GROUP BY FROM_UNIXTIME (timecreated, '%Y-%m-%d')
ORDER BY MIN(timecreated);
The order by uses MIN(timecreated) so it will work even if you change the format.
i made a sql statement and looks fine i first wanted to show only weekly dates like this one
WHERE WEEK(date_add) = WEEK(UTC_TIMESTAMP())
this works fine outcome = '2017-02-27 12:08:24'
For MONTHS i change WEEK to MONTH or YEAR works great. But then someone asked me to show only the date like outcome = 'april' or 'juli' and then i was like how do i do that? so i searched on google and found out that i could use %M Month name (January..December)
I know i need
'%M'
but how can i have it work in my case.
I also did try
DATE_FORMAT(NOW(),'%M')
But the outome was 1
I did search other post before posting this one they did not help me.
SELECT
sum(totalExcl) AS total, saleType, date_add
FROM
ex.ps_ox_quo
WHERE
WEEK(date_add) = WEEK(UTC_TIMESTAMP())
AND saleType IN ('IEW')
GROUP BY date_add
ORDER BY date_add DESC
I think you just want date_format():
SELECT sum(totalExcl) AS total, saleType, date_add,
date_format(date_add, '%M')
FROM ex.ps_ox_quo
WHERE WEEK(date_add) = WEEK(UTC_TIMESTAMP()) AND
saleType IN ('IEW')
GROUP BY date_add, saleType
ORDER BY date_add DESC;
Note: You should probably include the year in the date comparison.
You have a datetime type for column date_add you can use MONTHNAME
SELECT
sum(totalExcl) AS total, saleType, MONTHNAME(date_add)
FROM
ex.ps_ox_quo
WHERE
WEEK(date_add) = WEEK(UTC_TIMESTAMP())
AND saleType IN ('IEW')
GROUP BY MONTHNAME(date_add)
ORDER BY MONTHNAME(date_add) DESC
try this
SELECT
sum(totalExcl) AS total, saleType, monthname(date_add)
FROM
ex.ps_ox_quo
WHERE
month(date_add) = month(UTC_TIMESTAMP())
AND saleType IN ('IEW')
GROUP BY month(date_add)
ORDER BY month(date_add) DESC
select YEAR_MONTH(now()) AS current_month
I have the following structure
ID DATE(DATETIME) TID
1 2012-04-01 23:23:23 8882
I'm trying to count the amount of rows and group them by each day of the month that matches TID = 8882
Thanks
You can group using the DAY function:
SELECT DAY(Date), COUNT(*)
FROM table
WHERE TID = 8882
GROUP BY DAY(Date)
Not sure exactly what you mean by day of the month -- do you want to group the 1st of Feb with the 1st of March? Or do you just mean date? Assuming the latter, how about this:
SELECT DATE(date) as d,count(ID) from TABLENAME where TID=8882 GROUP by d;
Try this query:
SELECT COUNT(id), DAY(dat), MONTH(dat), YEAR(dat)
FROM table
WHERE TID=8882
GROUP BY YEAR(dat), MONTH(dat), DAY(dat);
Try this:
SELECT DAY(date) AS `DAY`, COUNT(1) AS `COUNT` FROM
table1
WHERE TID = 8882
GROUP BY DAY(date)
What about MySQL Query GROUP BY day / month / year
count for every date:
SELECT date(created_at), COUNT(*)
FROM message_requests
GROUP BY date(created_at)
For example I have a table with fields:
id date
1 2001-01-01
2 2001-01-05
.................
N 2011-12-31
How get i get all months last days from this table?
example:
if i have dates like 2001-05-31 and 2001-06-01
i need only 2001-05-31 not both
You can do SELECT LAST_DAY for example the below returns Oct. 31st. 2010
SELECT LAST_DAY('2010-10-10');
select max(date_format(date, '%d')) as last_day_of_the_month
from table
group by date_format(date, '%Y%m')
maybe this would work better?
select DISTINCT(LAST_DAY(date)) from table GROUP BY date_format(date, '%Y%m')
SELECT id, date
FROM `table`
WHERE DATE( date ) = LAST_DAY( date )
The DATE function over field is for filter the date without time, use only if you have a datetime column.
The query get all rows with date = last day of month.
select subdate(adddate(subdate(`date`, day(`date`) - 1), interval 1 month), 1)
Note: This is the "hard way". See #harper89's answer - it's better :)
I found a solution. but this query is very slow on large tables. so I am still looking for a better solution
select DISTINCT(LAST_DAY(date)) from table;
select max(date)
from table
group by year(date), month(date)
Consider the following table which has the fields - id (int) and date_created (datetime):
id date_created
1 2010-02-25 12:25:32
2 2010-02-26 13:40:37
3 2010-03-01 12:02:22
4 2010-03-01 12:10:23
5 2010-03-02 10:10:09
6 2010-03-03 12:45:03
I want to know the busiest/most popular hour of the day for this set of data. In this example, the result I'm looking for would be 12.
Ideas?
To get just the most popular hour, use this query
select date_format( date_created, '%H' ) as `hour`
from [Table]
group by date_format( date_created, '%H' )
order by count(*) desc
limit 1;
If you want to look at all the data, go with this one
select count(*) as num_records
, date_created
, date_format( date_created, '%H' ) as `hour`
from [Table]
group by `hour`
order by num_records desc;
If you want something a little more flexible, perhaps to the half hour, or quarter hour, you can do the following:
SELECT floor(time_to_sec(date_created)/3600),count(*) AS period
FROM table GROUP BY period ORDER BY c DESC
If you want the most popular 2 hour interval, use 7200. The most popular 15 minute interval, use 900. You just need to remember you are dealing with seconds (3600 seconds in an hour).
Use the hour() function to extract the hour, then do the usual aggregation:
SELECT count(hour(date_created)) AS c, hour(date_created) AS h FROM table GROUP BY h ORDER BY c DESC;
I like both Simon and Peter's answers, but I can't select both as accepted. I combined the 2 to make a cleaner query that only returned the popular hour (I don't need the counts).
SELECT hour(date_created) AS h
FROM my_table
GROUP BY h
ORDER BY count(*) DESC
LIMIT 1
You could try this:
SELECT
DATE_FORMAT(date,'%H') as hours,
count(*) as count
FROM
myTable
GROUP BY
hours
ORDER BY
count DESC