How to convert date range into whole number (integer) in SQL - mysql

Let's say I have a date 2013-03-01 and date 2013-04-02. How do I get a integer value between that date, for example in this case 2 days. In SQL kindly advise how to construct the query
day_date
---------------------
2005-07-29 00:00:00
2013-03-01 00:00:00
2013-04-02 00:00:00
2013-06-01 00:00:00
2013-10-19 00:00:00
2013-10-23 00:00:00
2013-12-31 00:00:00

The DATEDIFF() function returns the time between two dates.
SELECT DATEDIFF('2014-11-30','2014-11-29') AS DiffDate

Related

how do i get the correct id with the query results

I want to create a stored procedure in MySQL, but first, I want to get the query right. However, I keep getting the problem that I can't seem to get the correct id back from my query that correspond with the DateTime stamps that I get back.
this is the table I am trying to get the result from:
id EventId start end
1 1 2019-04-05 00:00:00 2019-04-07 00:00:00
2 2 2020-04-03 00:00:00 2020-04-03 00:00:00
3 3 2020-04-02 00:00:00 2020-04-02 00:00:00
7 1 2020-06-11 00:00:00 2020-06-11 00:00:00
9 2 2020-06-18 00:00:00 2020-06-18 00:00:00
10 3 2020-06-11 00:00:00 2020-06-11 00:00:00
11 3 2020-06-07 00:00:00 2020-06-07 00:00:00
query:
SELECT DISTINCT Eventid, MIN(start), id
from date_planning
WHERE `start` >= NOW()
GROUP BY Eventid
this gives me the following result
EventId Min(start) id
1 2020-06-11 00:00:00 3
2 2020-06-18 00:00:00 9
3 2020-06-07 00:00:00 10
but these are the correct ids that belong to those DateTimes:
EventId Min(start) id
1 2020-06-11 00:00:00 7
2 2020-06-18 00:00:00 9
3 2020-06-07 00:00:00 11
You want the row with the minimum "future" date for each eventId. To solve this greatest-n-per-group problem, you need to filter rather than aggregate. Here is one option using a correlated subquery:
select dt.*
from date_planning dt
where dt.start = (
select min(dt1.start)
from date_planning dt1
where dt1.eventId = dt.eventId and dt1.start >= now()
)
For performance, you need an index on (eventId, start).

Data type and sum time duration

What I have:
A MySQL table consisting of a "duration" column.
05:00:00
00:30:00
00:30:00
00:15:00
01:45:00
00:30:00
01:30:00
02:30:00
01:00:00
01:30:00
Data Type:
I first used TIME but from what I've researched, TIME is for storing a point in time rather than a duration of time. TIME also represents a limitation in that it can't exceed 23 hours.
I've instead opted for the DATETIME data type. My column now looks as follows:
0000-00-00 05:00:00
0000-00-00 00:30:00
0000-00-00 00:30:00
0000-00-00 00:15:00
0000-00-00 01:45:00
0000-00-00 00:30:00
0000-00-00 01:30:00
0000-00-00 02:30:00
0000-00-00 01:00:00
0000-00-00 01:30:00
Sum:
I need the total duration. I.e. SUM of the "duration" column in HH:MM:SS.
Summing the column produces "134000".
SELECT sum(`duration`) FROM `my_table`
My question:
How do I sum the duration column in a HH:MM:SS format?
SELECT SEC_TO_TIME(SUM(TIME_TO_SEC(`duration`)));
TIME_TO_SEC - will convert datetime to seconds. Then the SUM in your case will be 54000
SEC_TO_TIME - convert it back to datetime format
Output
15:00:00

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

Mysql order date by the m/h/s

How to order date, like this - 2012-02-01 00:00:00 by the hour,minutes,and the seconds, not by the year/moth/day.
If i have..
2012-02-01 02:00:00
2012-03-01 20:00:00
2012-04-01 12:00:00
2012-05-01 07:00:00
I wan't to get this output.
Column tipe is timestamp.
2012-02-01 02:00:00
2012-05-01 07:00:00
2012-04-01 12:00:00
2012-03-01 20:00:00
ORDER BY TIME(date_column)
This will, however, slow down your queries, as it isn't possible to index the on-the-fly calculation. If you have a lot of records, or if this query runs frequently, you should break the time portion of the date into its own column so you can index it for faster sorting.
Use the TIME() function to extract the time portion of the expression passed, e.g.
mysql> SELECT TIME('2012-02-01 02:00:00');
-> '02:00:00'
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_time

finding date intervals start and end dates

I have a table like this (plus 10 more columns) containing more than 1 million of frequently updated records:
id pid start_date end_date
1 761 2011-07-25 00:00:00 2011-08-01 00:00:00
2 761 2011-08-01 00:00:00 2011-08-22 00:00:00
3 761 2011-08-22 00:00:00 2011-09-19 00:00:00
4 802 2011-08-22 00:00:00 2011-09-19 00:00:00
5 761 2011-06-05 00:00:00 2011-07-05 00:00:00
and would like to get result for a particular pid (761 in the example below) with all consecutive intervals combined:
id pid start_date end_date
1 761 2011-07-25 00:00:00 2011-09-19 00:00:00
5 761 2011-06-05 00:00:00 2011-07-05 00:00:00
Currently I am doing this in the code, but would like to move this functionality entirely to the db side.
Any ideas how to do this?
edit: start_date and end_date columns are of DATETIME type.
This is really much better done in code. Loop over the rows, when it's for the same product, update the end date, otherwise create a new array entry.
For an idea of how complex this is in SQL, see my attempt at solving this in SQL Server :)