get all months last days in mysql - mysql

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)

Related

SQL - I need help How to COUNT all sales by month within a year

I can't figure out a way to COUNT al transactions by month each YEAR on a single query. I figured out a way but for one month at a time, there's a lot of data (time consuming)
this is the table
i want to have something like this
thank you
here is how you can do it , using extract to extract month /year from your date column and group by it:
select
extract(year_month from Date) year_month
, count(*) total_sales
from your table
group by extract(year_month from Date)
order by extract(year_month from Date)

How to select rows with the latest time for each date within the last 30 days from now

Imagine a table with field 'datetime'. Example rows:
2017-01-27 13:06:02
2017-01-27 05:13:14
2017-01-23 22:13:56
2017-01-26 14:02:09
2017-01-23 13:26:12
...
I need to get * from the bold lines, BUT WITHIN the last 30 days from now...
In other words, rows with the max date in the last 30 days.
30 rows in total in each case, assuming every day has at least one row...
You can group by the date part of datetime and get the max for each day.
select max(`datetime`)
from tablename
where `datetime` >= date(now())-interval '30' day
group by date(`datetime`)
To get all the fields from the table for such rows, use
select * from tablename where `datetime` in (
select max(`datetime`)
from tablename
where `datetime` >= date(now())-interval '30' day
group by date(`datetime`)
)
vkp's answer is basically correct, although there's no need for subquery to select the final result from - you can just put other columns straight into your query, up to something like this:
select *, max(datetime)
from tablename
where datetime >= date(now())-interval '30' day
group by date(datetime);
Ah, and that works for joins too.
The other thing I'd change to address the goal more precise, is:
max(time(datetime))
select * from your_table
where datetime between sysdate and sysdate-30

How to group by date regardless of time?

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

SQL Count Totals Within Date Ranges

I have a CHANGES table with fields VALUE(integer) and CREATED_AT(timestamp). I want to know the total of the VALUE column grouped by each of the past 30 days (without making 30 queries).
So if yesterday there were records created with VALUEs of 10, -7, and 12; I would want a record returned with CREATED_AT = yesterday and TOTAL = 15.
Any help?
SELECT date(created_at) as CREATED_AT, sum(value) as TOTAL
FROM changes
WHERE created_at >= curdate() - interval 30 day
GROUP BY date(created_at);
Well, it slightly depends on what kind the timestamp is formatted in (SQL/ Unix/ etc). But this type of query might help you along:
SELECT
DATE_FORMAT(CREATED_AT, '%Y-%m-%d') ym,
COUNT(VALUE)
FROM foo
GROUP BY ym

Selecting all records from one year ago till now

I have a table filled with a lot of rows and I need to select all the rows that are less than a year old till now.
The table (called orders) has a DateTime column named order_date, that's the field that determines when the order was placed.
How can I select all the records that have an order_date between now and a full year ago?
select *
from orders
where order_date >= DATE_SUB(NOW(),INTERVAL 1 YEAR);
SELECT * FROM order WHERE order_date >= curdate() - interval 1 year;
To first of month a year ago
SELECT DATE_SUB(DATE_FORMAT(CURRENT_DATE,'%Y-%m-01'),INTERVAL 1 YEAR);
I hope it helps you:
select *
from table
where (order_date BETWEEN '2/15/2011 3:36:18 PM' AND '2/17/2011 9:00:00 PM')