MySQL: Proper date range between start and end date as two fields - mysql

I have the following table called seasons
+----+--------+------------+------------+
| id | cost | start | end |
+----+--------+------------+------------+
| 33 | 255 | 2014-01-05 | 2014-04-16 |
| 17 | 357 | 2014-04-17 | 2014-04-19 |
| 65 | 191.25 | 2014-04-20 | 2014-07-10 |
| 49 | 255 | 2014-07-11 | 2014-08-23 |
| 81 | 191.25 | 2014-08-24 | 2014-12-18 |
+----+--------+------------+------------+
I am trying to get a date range between start and end using the following query.
SELECT
*
FROM
seasons
WHERE
(start BETWEEN '2014-01-05' AND '2014-01-05' OR end BETWEEN '2014-01-05' AND '2014-01-05');
I was able to get a result set if the start date started exactly on the value on the field.
+----+------+------------+------------+
| id | cost | start | end |
+----+------+------------+------------+
| 33 | 255 | 2014-01-05 | 2014-04-16 |
+----+------+------------+------------+
Now the problem is when I advance the date to
2014-01-06
SELECT
*
FROM
seasons
WHERE
(start BETWEEN '2014-01-06' AND '2014-01-06' OR end BETWEEN '2014-01-06' AND '2014-01-06');
Empty set (0.00 sec)
There are NO RESULT. How can I get the date range in between to different fields on SQL?
Any help is appreciated.

You have your logic backward. If you want seasons where a given date is in that season then your where clause should look like:
WHERE '2014-01-06' BETWEEN start AND end;

Since you are not really looking for a range and only needing a specific date, you could also just use (SQL Fiddle):
SELECT *
FROM seasons
WHERE start = '2014-01-05' OR end = '2014-01-05'

When querying if a certain date is in a given range, the WHERE clause typically is:
WHERE 'specified_date' BETWEEN 'start_date' AND 'end_date'
Otherwise it is an illogical date span to MySQL and will return an empty result set.
The BETWEEN Comparison Operator is equivalent to:
min <= expr AND expr <= max
Therefore, it's meant to be used like this:
expr BETWEEN min AND max
Here is documentation from MySQL on the BETWEEN Comparison Operator.

Related

Something curious is happening subtracting dates in a query - 70 days appears from nowhere :-/ [duplicate]

the leetcode question 197.Rising Temperature
Given a Weather table, write a SQL query to find all dates' Ids with higher temperature compared to its previous (yesterday's) dates.
+---------+------------------+------------------+
| Id(INT) | RecordDate(DATE) | Temperature(INT) |
+---------+------------------+------------------+
| 1 | 2015-01-01 | 10 |
| 2 | 2015-01-02 | 25 |
| 3 | 2015-01-03 | 20 |
| 4 | 2015-01-04 | 30 |
+---------+------------------+------------------+
For example, return the following Ids for the above Weather table:
+----+
| Id |
+----+
| 2 |
| 4 |
+----+
i didn't know the function DATEDIFF(),so i wrote my sql solution:
select w1.Id
from Weather w1,Weather w2
where w1.RecordDate - w2.RecordDate = 1
and w1.Temperature > w2.Temperature
and i went through the testcase but got a wrong submit,the right solution is use funtion DATEDIFF()
select w1.Id
from Weather w1,Weather w2
where DATEDIFF(w1.RecordDate,w2.RecordDate)=1
and w1.Temperature > w2.Temperature
so my question is what's the difference between
DATEDIFF(w1.RecordDate,w2.RecordDate)=1and w1.RecordDate - w2.RecordDate = 1
thank you for your help
If the datatype of RecordDate is DATETIME rather than DATE, subtracting them returns a large value that contains the difference between the times as well as the dates. E.g.
mysql> select cast('2019-03-21 10:20:30' as datetime) - cast('2019-03-11 9:15:20' as datetime) as difference;
+-----------------+
| difference |
+-----------------+
| 10010510.000000 |
+-----------------+
But if they're DATE then subtraction should be the same as DATEDIFF():
mysql> select cast('2019-03-21' as date) - cast('2019-03-11' as date) as difference;
+------------+
| difference |
+------------+
| 10 |
+------------+
When we do simple addition and subtraction on the date or datetime, it will convert it into a number and after that the actual operation will execute. After the operation execution, the output will not be the date datatype.
For example,
For addition (+) : -
'2020-07-31' + 1 => 20200732
For subtraction (-) : -
'2020-08-01' - '2020-07-31' => 70
It should return 1 but as they consider it as a number so they return 70 instead of 1.
This is the reason we cannot apply direct addition or subtraction on date.
To get the actual difference between two dates one should must use the DateDiff().
Datediff(cast('2020-08-01' as Date),Cast('2020-07-31' as Date)) => 1

How to sum alias fields with date range

I need to show SUM value of alias table that has value of the result from multiplying 2 fields.
Here is asset table:
+----------+-----+------------+
| price | qty | tanggal |
+----------+-----+------------+
| 6775000 | 1 | 2019-03-30 |
| 4760000 | 2 | 2019-03-25 |
| 7800000 | 2 | 2019-04-01 |
| 13599000 | 1 | 2019-03-30 |
+----------+-----+------------+
I've already tried:
SELECT
SUM((price*qty))
AS worth
FROM asset
WHERE DATE(tanggal) >= 2019-03-25 AND DATE(tanggal) <= 2019-03-30
Also using BETWEEN clause like this:
SELECT
SUM((price*qty))
AS worth
FROM asset
WHERE DATE(tanggal) BETWEEN 2019-03-25 AND 2019-03-30
It keeps giving me NULL value, but if I remove the WHERE clause it
works fine and give me value of 45494000. Any ideas?
What about adding single quotes, right know you are only doing a subtraction
SELECT SUM((price*qty)) AS worth
FROM asset
WHERE tanggal >= '2019-03-25' AND tanggal <= '2019-03-30'

mysql string to date conversion

Need help to suggest sql query where I have a varchar column called Timing like below
+---------------------+
| Timing | MO |
+---------------------+
|7/11/2016 20:45 |ABC |
|7/12/2016 20:45 |ABC |
|2016-07-11 00:00|ABC |
|2016-07-12 00:00|ABC |
+---------------------+
I need to extract date from the Timing column but tricky part is the date is not always in same format. I need to output timing column as below
+-------------+
| Timing | MO|
+-------------+
|7/11/2016|ABC|
|7/12/2016|ABC|
|7/11/2016|ABC|
|7/12/2016|ABC|
+-------------+
It's simple enough using substring and replace functions to cast varchar as data and recast them to you required output.
for example
DROP TABLE T;
CREATE TABLE T (TIMING VARCHAR(20),MO VARCHAR(2));
INSERT INTO T VALUES
('7/11/2016 20:45' ,'AB'),
('7/12/2016 20:45' ,'AB'),
('2016-07-11 00:00','AB'),
('2016-07-12 00:00','AB'),
('27/06/2016 20:45' ,'AB'),
('29/05/2016 20:45' ,'AB')
*/
SELECT *,
CASE
WHEN INSTR(TIMING,'/') = 0 THEN
CASE WHEN INSTR(TIMING,'-') = 5 THEN CAST(TIMING AS DATE)
end
WHEN INSTR(TIMING,'/') = 3 THEN
CAST(
CONCAT(
SUBSTRING(CONCAT('0',TIMING),8,4)
,'-'
,SUBSTRING(CONCAT('0',TIMING),5,2)
,'-'
,SUBSTRING(CONCAT('0',TIMING),1,2)
)
AS DATE
)
WHEN INSTR(TIMING,'/') = 2 THEN
CAST(
CONCAT(
SUBSTRING(CONCAT('0',TIMING),7,4)
,'-'
,SUBSTRING(CONCAT('0',TIMING),4,2)
,'-'
,SUBSTRING(CONCAT('0',TIMING),1,2)
)
AS DATE
)
END
FROM T
results in
-----------------------------------------------------------------------------+
| 7/11/2016 20:45 | AB | 2016-11-07 |
| 7/12/2016 20:45 | AB | 2016-12-07 |
| 2016-07-11 00:00 | AB | 2016-07-11 |
| 2016-07-12 00:00 | AB | 2016-07-12 |
| 27/06/2016 20:45 | AB | 2016-06-02 |
| 29/05/2016 20:45 | AB | 2016-05-02 |
+------------------+------+-----------------------------------------------------
You do have to code for every known variant - in this case I have identified that 7/11/2016 needs to left padded with zero and slashes replaced with dashs before date conversion. Hopefully you have a limited number of date variations in your source to code for and you will end up with a large query - but that's life. God luck,

Calculate the fullness of an apartment using SQL expression

I have a database which looks like this:
Reservations Table:
-------------------------------------------------
id | room_id | start | end |
1 | 1 | 2015-05-13 | 2015-05-16 |
2 | 1 | 2015-05-18 | 2015-05-20 |
3 | 1 | 2015-05-21 | 2015-05-24 |
-------------------------------------------------
Apartment Table:
---------------------------------------
id | room_id | name |
1 | 1 | test apartment |
---------------------------------------
Meaning that in the month 05 (May) there is 31 days in the database we have 3 events giving us 8 days of usage 31 - 8 = 23 / 31 = 0.741 * 100 = %74.1 is the percentage of the emptiness and %25.9 is the percentage of usage. how can i do all of that in SQL? (mySQL).
This is my proposal:
SELECT SUM(DAY(`end`)-DAY(`start`))/EXTRACT(DAY FROM LAST_DAY(`start`)) FROM `apt`;
LAST_DAY function gives as output the date of last day of the month.
Check this
http://sqlfiddle.com/#!9/7c53b/2/0
Not the most efficient query but will get the job done.
select
sum(a.days)*100/(SELECT DAY(LAST_DAY(min(start))) from test1)
as usePercent,
100-(sum(a.days)*100/(SELECT DAY(LAST_DAY(min(start))) from test1))
as emptyPercent
FROM
(select DATEDIFF(end,start) as days from test1) a
What I did is first get the date difference and count them. Then in a nested query use the day(last_day()) function to get the last day of month. Then calculated by using your logic.

mysql split a string in a where clause

I have an event system and for my repeat events I am using a cron like system.
Repeat Event:
+----+----------+--------------+
| id | event_id | repeat_value |
+----+----------+--------------+
| 1 | 11 | *_*_* |
| 2 | 12 | *_*_2 |
| 3 | 13 | *_*_4/2 |
| 4 | 14 | 23_*_* |
| 5 | 15 | 30_05_* |
+----+----------+--------------+
NOTE: The cron value is day_month_day of week
Event:
+----+------------------------+---------------------+---------------------+
| id | name | start_date_time | end_date_time |
+----+------------------------+---------------------+---------------------+
| 11 | Repeat daily | 2014-04-30 12:00:00 | 2014-04-30 12:15:00 |
| 12 | Repeat weekly | 2014-05-06 12:00:00 | 2014-05-06 13:00:00 |
| 13 | Repeat every two weeks | 2014-05-08 12:45:00 | 2014-05-08 13:45:00 |
| 14 | Repeat monthly | 2014-05-23 15:15:00 | 2014-05-23 16:00:00 |
| 15 | Repeat yearly | 2014-05-30 07:30:00 | 2014-05-30 10:15:00 |
+----+------------------------+---------------------+---------------------+
Anyway I have a query to select the events:
SELECT *
FROM RepeatEvent
JOIN `Event`
ON `Event`.`id` = `RepeatEvent`.`event_id`
That produces:
+----+----------+--------------+----+------------------------+---------------------+---------------------+
| id | event_id | repeat_value | id | name | start_date_time | end_date_time |
+----+----------+--------------+----+------------------------+---------------------+---------------------+
| 1 | 11 | *_*_* | 11 | Repeat daily | 2014-04-30 12:00:00 | 2014-04-30 12:15:00 |
| 2 | 12 | *_*_2 | 12 | Repeat weekly | 2014-05-06 12:00:00 | 2014-05-06 13:00:00 |
| 3 | 13 | *_*_4/2 | 13 | Repeat every two weeks | 2014-05-08 12:45:00 | 2014-05-08 13:45:00 |
| 4 | 14 | 23_*_* | 14 | Repeat monthly | 2014-05-23 15:15:00 | 2014-05-23 16:00:00 |
| 5 | 15 | 30_05_* | 15 | Repeat yearly | 2014-05-30 07:30:00 | 2014-05-30 10:15:00 |
+----+----------+--------------+----+------------------------+---------------------+---------------------+
However, I want to select events within a month. I will only have certain conditions: daily, weekly, every two weeks, month and yearly.
I want to put in my where clause a way to divide the string of the repeat value and if it fits any of the following conditions to show it as a result (repeatEvent is row that is being interrogated, search is the date being looked for):
array(3) = string_divide(repeat_value, '_')
daily = array(0)
monthy = array(1)
dayOfWeek = array(2)
if(daily == '*' && month == '*' && dayOfWeek == '*') //returns all the daily events as they will happen
return repeatEvent
if(if(daily == '*' && month == '*' && dayOfWeek == search.dayOfWeek) //returns all the events on specific day
return repeatEvent
if(daily == search.date && month == '*' && dayOfWeek == '*') //returns all the daily events as they will happen
return repeatEvent
if (contains(dayOfWeek, '/'))
array(2) = string_divide(dayOfWeek,'/')
specificDayOfWeek = array(0);
if(specificDayOfWeek == repeatEvent.start_date.dayNumber)
if(timestampOf(search.timestamp)-timestampOf(repeatEvent.start_date)/604800 == (0 OR EVEN)
return repeatEvent
if(daily == search.date && month == search.month && dayOfWeek == '*') //returns a single yearly event (shouldn't often crop up)
return repeatEvent
//everything else is either an unknown format of repeat_value or not an event on this day
To summarise I want to run a query in which the repeat value is split in the where clause and I can interrogate the split items. I have looked at cursors but the internet seems to advise against them.
I could process the results of selecting all the repeat events in PHP, however, I imagine this being very slow.
Here is what I would like to see if looking at the month of April:
+----------+--------------+----+------------------------+---------------------+---------------------+
| event_id | repeat_value | id | name | start_date_time | end_date_time |
+----------+--------------+----+------------------------+---------------------+---------------------+
| 11 | *_*_* | 11 | Repeat daily | 2014-04-30 12:00:00 | 2014-04-30 12:15:00 |
+----------+--------------+----+------------------------+---------------------+---------------------+
Here is what I would like to see if looking at the month of May
+----------+--------------+----+------------------------+---------------------+---------------------+
| event_id | repeat_value | id | name | start_date_time | end_date_time |
+----------+--------------+----+------------------------+---------------------+---------------------+
| 11 | *_*_* | 11 | Repeat daily | 2014-04-30 12:00:00 | 2014-04-30 12:15:00 |
| 12 | *_*_2 | 12 | Repeat weekly | 2014-05-06 12:00:00 | 2014-05-06 13:00:00 |
| 13 | *_*_4/2 | 13 | Repeat every two weeks | 2014-05-08 12:45:00 | 2014-05-08 13:45:00 |
| 14 | 23_*_* | 14 | Repeat monthly | 2014-05-23 15:15:00 | 2014-05-23 16:00:00 |
| 15 | 30_05_* | 15 | Repeat yearly | 2014-05-30 07:30:00 | 2014-05-30 10:15:00 |
+----------+--------------+----+------------------------+---------------------+---------------------+
Here is what I would like to see if looking at the month of June
+----------+--------------+----+------------------------+---------------------+---------------------+
| event_id | repeat_value | id | name | start_date_time | end_date_time |
+----------+--------------+----+------------------------+---------------------+---------------------+
| 11 | *_*_* | 11 | Repeat daily | 2014-04-30 12:00:00 | 2014-04-30 12:15:00 |
| 12 | *_*_2 | 12 | Repeat weekly | 2014-05-06 12:00:00 | 2014-05-06 13:00:00 |
| 13 | *_*_4/2 | 13 | Repeat every two weeks | 2014-05-08 12:45:00 | 2014-05-08 13:45:00 |
| 14 | 23_*_* | 14 | Repeat monthly | 2014-05-23 15:15:00 | 2014-05-23 16:00:00 |
+----------+--------------+----+------------------------+---------------------+---------------------+
You could put a bandaid on this, but no one would be doing you any favors to tell you that that is the answer.
If your MySQL database can be changed I would strongly advise you to split your current column with underscores day_month_day of year to three separate columns, day, month, and day_of_year. I would also advise you to change your format to be INT rather than VARCHAR. This will make it faster and MUCH easier to search and parse, because it is designed in a way that doesn't need to be translated into computer language through complicated programs... It is most of the way there already.
Here's why:
Reason 1: Your Table is not Optimized
Your table is not optimized and will be slowed regardless of what you choose to do at this stage. SQL is not built to have multiple values in one column. The entire point of an SQL database is to split values into different columns and rows.
The advantage to normalizing this table is that it will be far quicker to search it, and you will be able to build queries in MySQL. Take a look at Normalization. It is a complicated concept, but once you get it you will avoid creating messy and complicated programs.
Reason 2: Your Table could be tweaked slightly to harness the power of computer date/time functions.
Computers follow time based on Unix Epoch Time. It counts seconds and is always running in your computer. In fact, computers have been counting this since, as the name implies, the first Unix computer was ever switched on. Further, each computer and computer based program/system, has built in, quick date and time functions. MySQL is no different.
I would also recommend also storing all of these as integers. repeat_doy (day of year) can easily be a smallint or at least a standard int, and instead of putting a month and day, you can put the actual 1-365 day of the year. You can use DAY_OF_YEAR(NOW()) to input this into MySQL. To pull it back out as a date you can use MAKEDATE(YEAR(NOW),repeat_doy). Instead of an asterisk to signify all, you can either use 0's or NULL.
With a cron like system you probably will not need to do that sort of calculation anyway.
Instead, it will probably be easier to just measure the day of year elsewhere (every computer and language can do this. In Unix it is just date "%j").
Solution
Split your one repeat_value into three separate values and turn them all into integers based on UNIX time values. Day is 1-7 (or 0-6 for Sunday to Saturday), Month is 1-12, and day of year is 1-365 (remember, we are not including 366 because we are basing our year on an arbitrary non-leap year).
If you want to pull information in your SELECT query in your original format, it is much easier to use concat to merge the three columns than it is to try to search and split on one column. You can also easily harness built in MySQL functions to quickly turn what you pull into real, current, days, without a bunch of effort on your part.
To implement it in your SQL database:
+----+----------+--------------+--------------+------------+
| id | event_id | repeat_day | repeat_month | repeat_doy |
+----+----------+--------------+--------------+------------+
| 1 | 11 | * | * | * |
| 2 | 12 | * | * | 2 |
| 3 | 13 | * | * | 4/2 |
| 4 | 14 | 23 | * | * |
| 5 | 15 | 30 | 5 | * |
+----+----------+--------------+--------------+------------+
Now you should be able to build one query to get all of this data together regardless of how complicated your query. By normalizing your table, you will be able to fully harness the power of relational databases, without the headaches and hacks.
Edit
Hugo Delsing made a great point in the comments below. In my initial example I provided a fix to leap years for day_of_year in which I chose to ignore Feb 29. A much better solution removes the need for a fix. Split day_of_year to month and day with a compound index. He also has a suggestion about weeks and number of weeks, but I will just recommend you read it for more details.
Try to write where condition using this:
substring_index(repeat_value,'_', 1)
instead of daily
substring_index(substring_index(repeat_value,'_', -2), '_', 1)
instead of monthly
and
substring_index(substring_index(repeat_value,'_', -1), '_', 1)
instead of dayOfWeek
I think you are overthinking the problem if you only want the events per month and not per day. Assuming that you always correctly fill the repeat_value, the query is very basic.
Basically all event occur every month where the repeat_value is either LIKE '%_*_%' or LIKE '%_{month}_%'.
Since you mentions PHP I'm assuming you are building the query in PHP and thus I used the same.
<?php
function buildQuery($searchDate) {
//you could/should do some more checking if the date is valid if the user provides the string
$searchDate = empty($searchDate) ? date("Y-m-d") : $searchDate;
$splitDate = explode('-', $searchDate);
$month = $splitDate[1];
//Select everything that started after the searchdate
//the \_ is because else the _ would match any char.
$query = 'SELECT *
FROM RepeatEvent
JOIN `Event`
ON `Event`.`id` = `RepeatEvent`.`event_id`
WHERE `Event`.`start_date_time` < \''.$searchDate.'\'
AND
(
`RepeatEvent`.`repeat_value` LIKE \'%\_'.$month.'\_%\'
OR `RepeatEvent`.`repeat_value` LIKE \'%\_*\_%\'
)
';
return $query;
}
//show querys for all months on current day/year
for ($month = 1; $month<=12; $month++) {
echo buildQuery(date('Y-'.$month.'-d')) . '<hr>';
}
?>
Now if the repeat_value could be wrong, you could add a simple regex check to make sure the value is always like *_*_* or *_*_*/*
You can use basic regular expressions in MySQL:
http://dev.mysql.com/doc/refman/5.0/en/pattern-matching.html
For a monthly event in May (first day) you can use a pattern like this (not tested):
[0-9\*]+\_[5\*]\_1
You can generate this pattern via PHP