mysql next date by day of one field - mysql

i have a mysql customers table:
customer_id | customer_name | creation_date
1 | john | 2013-09-12 18:34:00
2 | banjo | 2013-01-11 14:34:00
what i would to achieve is to know the closest DAY in the current ot next month that match the creation_date field.
I.E if the current date is 2014-01-20, i would like to have the following result
customer_id | customer_name | creation_date | next_date
1 | john | 2013-09-12 18:34:00 | 2014-02-12
2 | banjo | 2013-01-11 14:34:00 | 2014-02-11

The following seems to work but not tested for edge cases:
SELECT
CURRENT_DATE AS cutoff_date,
date_column AS creation_date,
CASE
WHEN STR_TO_DATE(CONCAT_WS('-', YEAR(CURRENT_DATE), MONTH(CURRENT_DATE), DAY(date_column)), '%Y-%c-%e') >= CURRENT_DATE
THEN STR_TO_DATE(CONCAT_WS('-', YEAR(CURRENT_DATE), MONTH(CURRENT_DATE), DAY(date_column)), '%Y-%c-%e')
ELSE STR_TO_DATE(CONCAT_WS('-', YEAR(CURRENT_DATE), MONTH(CURRENT_DATE), DAY(date_column)), '%Y-%c-%e') + INTERVAL 1 MONTH
END AS next_date
FROM dates2
Results:
cutoff_date creation_date next_date
------------------- ------------------- -------------------
2014-01-20 00:00:00 2010-01-01 00:41:00 2014-02-01 00:00:00
2014-01-20 00:00:00 2010-01-10 00:06:00 2014-02-10 00:00:00
2014-01-20 00:00:00 2010-01-19 22:34:00 2014-02-19 00:00:00
2014-01-20 00:00:00 2010-01-19 23:13:00 2014-02-19 00:00:00
2014-01-20 00:00:00 2010-01-20 00:36:00 2014-01-20 00:00:00
2014-01-20 00:00:00 2010-01-20 00:43:00 2014-01-20 00:00:00
2014-01-20 00:00:00 2010-02-15 08:05:00 2014-02-15 00:00:00
2014-01-20 00:00:00 2010-02-25 22:50:00 2014-01-25 00:00:00

First calculate the wanted date, than use min/max to get the closest one. Maybe something like that:
-- If your stored date is always in year 2013
select CASE WHEN DATE_ADD(DATE_ADD(creation_date, INTERVAL 1 YEAR), INTERVAL 1 MONTH) < SYSDATE
THEN DATE_ADD(creation_date, INTERVAL 1 YEAR)
ELSE DATE_ADD(DATE_ADD(creation_date, INTERVAL 1 YEAR), INTERVAL 1 MONTH)
END AS next_date
from customers;
This will make your creation_date 2013-09-12 to 2014-10-12. But if you just want the day maybe this is useful?:
-- If your stored date is always in year 2013
select str_to_date(concat(date_format(curdate(),'%Y-%m'), date_format(creation_date,'%d')),'%Y-%m-%d') as next_date
from customers;
This should use the current year and month, but change just the day. You can use a CASE to check if the difference between the current or the next month is closer.

You can try this, in mysql you can use interval for perticuler increament date like:
select customer_id,customer_name,creation_date,(creation_date+interval 1 year) as next_date from customer_info;
if you want,
next year then use- (creation_date + interval 1 year)
next month then use- (creation_date + interval 1 month)
for perticular day use- (creation_date + interval 5 day)

Related

How to select count today and tomorrow data less than specific time group by day?

I have a table like a table below.
I want to select count and group by day.
But the data in 1 day will start counts at 7:00:00 until tomorrow at 6:59:59 (24hr.).
For example
Day 1 data between '2019/06/01 7:00:00' and '2019/06/02 06:59:59'
Day 2 data between '2019/06/02 7:00:00' and '2019/06/03 06:59:59'
How can I code the where condition?
id | create_date | judge |
-----+---------------------+---------+
1 | 2019-06-02 8:00:00 | ok |
2 | 2019-06-02 9:00:00 | ok |
3 | 2019-06-02 10:00:00 | ok |
4 | 2019-06-02 11:00:00 | ok |
5 | 2019-06-02 15:00:00 | ok |
6 | 2019-06-03 4:00:00 | ok |
7 | 2019-06-03 5:00:00 | ok |
8 | 2019-06-03 8:00:00 | ok |
9 | 2019-06-03 9:00:00 | ok |
10 | 2019-06-03 9:00:00 | fail |
I've tried below but the result is not as expected.
SELECT COUNT(*),DAY(create_date)
FROM mytable
WHERE judge = 'ok' and MONTH(create_date) = '6' and YEAR(create_date) = '2019' and TIME(create_date) > '07:00:00'
Group by DAY(create_date) order by DAY(create_date) ASC
Expected results
COUNT(*) | DAY(create_date) |
-----------+---------------------+
7 | 2 | (from id 1 to 7)
2 | 3 | (from id 8 and 9)
You could subtract seven hours from each date, truncate them to show the date only and then group them:
SELECT DATE(DATE_SUB(create_date, INTERVAL 7 HOUR)), COUNT(*)
FROM mytable
-- Where clause if you need it...
GROUP BY DATE(DATE_SUB(create_date, INTERVAL 7 HOUR))
Just subtract 7 hours for the aggregation and the date/time comparisons:
SELECT DATE(create_date - interval 7 hour) as dte, COUNT(*)
FROM mytable
WHERE judge = 'ok' and
create_date >= '2019-06-01 07:00:00' AND
create_date < '2019-07-01 07:00:00'
GROUP BY DATE(create_date - interval 7 hour)
ORDER BY dte;
Try this-
SELECT
CAST(DATE_SUB(create_date, INTERVAL 7 HOUR) AS DATE),
COUNT(*)
FROM YOUR_TABLE
GROUP BY CAST(DATE_SUB(create_date, INTERVAL 7 HOUR) AS DATE)

MySQL - Select rows where 1 hour before datetime column

I have these rows
id | start_time |
1 | 2018-06-15 02:00:00 |
2 | 2018-06-15 02:45:00 |
3 | 2018-06-15 03:45:00 |
I want to select rows that are 1 hour before the start_time. So if the time is 2018-06-15 01:00:00 then the first row should be returned.
How do i do this? I've tried below but i don't know how to subtract 1 hour from start_time.
SELECT *
FROM table1
WHERE DATE_FORMAT(start_time, '%Y-%m-%d %H') <= DATE_FORMAT(NOW(), '%Y-%m-%d %H');
To subtract hours ,use date_sub function
In your case
SELECT DATE_SUB(DATE_FORMAT(start_time, '%Y-%m-%d %H'), INTERVAL 1 HOUR)

DateTime query searching

I have a sample database.
Name CheckIn CheckOut
Jake 2017-08-02 00:00:00 2017-08-05 00:00:00
Rowan 2017-08-07 00:00:00 2017-08-11 00:00:00
Xander 2017-08-08 00:00:00 2017-08-10 00:00:00
Anna 2017-08-09 00:00:00 2017-08-15 00:00:00
Nat 2017-08-11 00:00:00 2017-08-14 00:00:00
For example user search the date of 2017-08-08 to 2017-08-10. User want to search the all data that who has the date of 8, 9 and 10. Ex. Rowan choose the date of checkin in 7 and checkout in 10, So rowan had the the date of 7, 8, 9 10 and 11. This is the output that i want.
Name CheckIn CheckOut
Rowan 2017-08-07 00:00:00 2017-08-11 00:00:00
Anna 2017-08-09 00:00:00 2017-08-15 00:00:00
Xander 2017-08-08 00:00:00 2017-08-10 00:00:00
I really dont know the logic of reservation searching help me. Thanks guys.
If you want to check for overlapping intervals, then this is the way:
SELECT Name, CheckIn, CheckOut
FROM mytable
WHERE CheckIn <= '2017-08-10' AND CheckOut >= '2017-08-08';
Demo here
To better understand this you can draw a sketch depicting the search interval [2017-08-08, 2017-08-10] against the reservation interval [CheckIn, CheckOut]:
If:
CheckOut < '2017-08-08' then the reservation interval comes before the search interval
CheckIn > '2017-08-10` then the reservation interval comes after the search interval
Hence none of the above conditions must hold if we want the intervals to overlap. So we end up with the following predicates:
`CheckOut` >= '2017-08-08' AND `CheckIn` <= '2017-08-10`
try this:
SELECT * FROM test.TableName WHERE
(DATE(CheckIn) BETWEEN DATE('2017-08-08') AND DATE('2017-08-10') )
OR
(DATE(CheckOut) BETWEEN DATE('2017-08-08') AND DATE('2017-08-10') )
OR
(DATE('2017-08-08') BETWEEN DATE(CheckIn) AND DATE(CheckOut))
OR
(DATE('2017-08-10') BETWEEN DATE(CheckIn) AND DATE(CheckOut));
You need a between condition for start and end date in where clause for check_in and check_out columns as below.
SELECT *
FROM table1
WHERE check_in BETWEEN '2017-08-07' AND '2017-08-10'
OR check_out BETWEEN '2017-08-07' AND '2017-08-10'
Result
name check_in check_out
-------------------------------------------------
Rowan 07.08.2017 00:00:00 11.08.2017 00:00:00
Xander 08.08.2017 00:00:00 10.08.2017 00:00:00
Anna 09.08.2017 00:00:00 15.08.2017 00:00:00
You can check the demo here

How to find sales per hour between two date

Suppose I have 5 records for a sales table.
ID Name datetime_col
1 ABC 2016-09-15 02:07:56
2 HSJ 2016-09-31 11:45:45
3 JSD 2016-11-26 07:09:56
4 JUH 2016-12-31 12:00:00
5 IGY 2017-01-13 14:00:07
I want to find how many records are there in sales table for each hour between 2016-09-15 AND 2017-01-13
Then result should be like
Hour sales_at_this_hour
2016-09-15 01:00:00 0
2016-09-15 02:00:00 1
2016-09-15 03:00:00 0
...
...
2017-01-13 01:00:00 0
2017-01-13 02:00:00 0
2017-01-13 03:00:00 0
....
2017-01-13 14:00:00 1
Then find the average of sales_at_this_hour using MySQL
EDIT: sorry not fully understand the question at first.
Use DATE_FORMAT
select
DATE_FORMAT(datetime_col, '%Y-%m-%d %h:00:00') as date,
count(id) as count
from table_name
group by date;
Get result with hours that has sales_at_this_hour > 1 (not exactly what you ask for)
datetime_col count
2016-02-04 05:00:00 5
2016-02-04 07:00:00 1
2016-02-04 08:00:00 5
2016-02-04 10:00:00 10
2016-02-04 11:00:00 1
Provide start_date and end_date, and then use DATEDIFF to calculate total time interval for the average calculation.
set #start_date = '2016-01-01', #end_date = '2017-01-01';
select
DATE_FORMAT(group_by_date.datetime, '%h:00:00') as hour,
AVG(group_by_date.count) / DATEDIFF(#end_date, #start_date) as average
from (
select
DATE_FORMAT(created_dtm, '%Y-%m-%d %h:00:00') as datetime,
count(id) as count
from table_name
where created_dtm > #start_date
and created_dtm < #end_date
group by datetime
) group_by_date
group by hour;
For each hour,
average sale count per day = total sale count / total days
hour average
01:00:00 0.03841209
02:00:00 0.01653005
03:00:00 0.0306716
04:00:00 0.01147541
05:00:00 0.01179831

How to get week wise record in mysql from start to end date

I have a Table called weight and fields are ID, WEIGHT, CREATED ON. I also have START date and END date in PHP variables. Now I want to get a week wise record from start date to end date.
Result should be like this:
WEEK | WEIGHT | CREATED ON
-----+--------+-----------
1 | 50 | 2012-02-01
1 | 50 | 2012-02-03
1 | 50 | 2012-02-05
1 | 50 | 2012-02-07
2 | 50 | 2012-02-08
2 | 50 | 2012-02-10
2 | 50 | 2012-02-14
3 | 50 | 2012-02-15
3 | 50 | 2012-02-17
3 | 50 | 2012-02-17
How to achieve it?
Hope it helps
SELECT ID, weigth, created_on, WEEK(created_on) week
FROM weight
WHERE created_on BETWEEN start_date AND end_date
ORDER BY week
try this one...
SELECT WEEKDAY(CREATED_ON)AS weekdays,weight,CREATED_ON
FROM weight
WHERE CREATED_on BETWEEN start_date AND end_date
GROUP BY weekdays
Try below
SELECT WEEKDAY(CREATED_ON) AS week, weight, created_on
FROM weight
WHERE CREATED_on BETWEEN start_date AND end_date
ORDER BY week
This should give you what you want...
Update 1
SELECT (SELECT COUNT(*) FROM myTable u2
WHERE
u2.date > u1.date) + 1 AS week, date FROM myTable u1
WHERE date between start_date AND end_date
ORDER BY week
Update 2
In below query, date is the date that I have.
SELECT (WEEK(date, 5) -
WEEK(DATE_SUB(date, INTERVAL DAYOFMONTH(date) - 1 DAY), 5) + 1) as week, date
FROM myTable
WHERE date BETWEEN start_date AND end_date
ORDER BY week
Update 3
Suppose I have
myDate
+++++++++++++++++++++
2012-02-01 12:12:12
2012-02-01 12:12:12
2012-02-01 12:12:12
2012-02-03 12:12:12
2012-02-05 12:12:12
2012-02-07 12:12:12
2012-02-08 12:12:12
2012-02-08 12:12:12
2012-02-10 12:12:12
2012-02-14 12:12:12
2012-02-15 12:12:12
2012-02-17 12:12:12
2012-02-17 12:12:12
2012-03-01 12:12:12
2012-03-03 12:12:12
2012-03-05 12:12:12
2012-03-07 12:12:12
And below is what I want.
week |myDate
+++++++++++++++++++++++++++
1 |2012-03-07 12:12:12
2 |2012-03-05 12:12:12
3 |2012-03-03 12:12:12
4 |2012-03-01 12:12:12
5 |2012-02-17 12:12:12
5 |2012-02-17 12:12:12
6 |2012-02-15 12:12:12
7 |2012-02-14 12:12:12
8 |2012-02-10 12:12:12
9 |2012-02-08 12:12:12
9 |2012-02-08 12:12:12
10 |2012-02-07 12:12:12
11 |2012-02-05 12:12:12
12 |2012-02-03 12:12:12
13 |2012-02-01 12:12:12
13 |2012-02-01 12:12:12
13 |2012-02-01 12:12:12
To get above, see below query...
SELECT (SELECT COUNT(distinct u2.myDate) FROM myTable u2
WHERE
u2.myDate > u1.myDate) + 1 AS week, myDate FROM myTable u1
WHERE 1=1
ordeR BY week
In your case WHERE clause would be WHERE myDate between startDate AND endDate
Hope this is what you want... Finally!!!
Good Luck!!!
If you already have your start and end dates stored in PHP variables, you could use the MySQL BETWEEN key word to perform a SELECT query on the table, thus (PHP code):
$query = "SELECT FROM weight WHERE `created on` BETWEEN '" . $start_date . "' AND '" . $end_date . "'";
I've included the string quotes (') above as I assume you've stored your variables as PHP strings formatted as date-times, i.e. YYYY-MM-DD HH:MM:SS, using the date() format: "Y-m-d H:i:s".
Further reading: http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#operator_between
Compute the difference (in whole days) between the START date and the CREATED ON date, divide by 7, and add 1 -- something like this:
SELECT
DATEDIFF(CREATED_ON, START) / 7 + 1,
WEIGHT,
CREATED_ON
FROM
weight
ORDER BY
CREATED_ON