I am trying to build a query that will load records from and to specific date comparing 2 fields - the start_time and the end_date.
SELECT start_time
,end_time
,DATEDIFF(end_time, start_time) AS DiffDate
FROM my_tbl
WHERE start_time >= '2015-04-27 00:00:00'
AND end_time <= '2015-04-28 00:00:00'
AND end_time >= '2015-04-27 00:00:00'
AND DiffDate < 100
LIMIT 1000;
Unfortunately the DiffDate returns always 0.
The ideal scenario was to calculate the difference between start_time and end_time when inserting the end_time but the I cant make any changes on the database.
What am I doing wrong here? Even if the DiffDate was working will it considered as a good solution?
From the condition in the where clause it appears that you are trying to get data for the same date, however using the datediff for the same day always would result 0
mysql> select datediff('2015-04-27 12:00:00','2015-04-27 00:00:00') as diff ;
+------+
| diff |
+------+
| 0 |
+------+
1 row in set (0.03 sec)
You may need other means of calculation perhaps using the timestampdiff
mysql> select timestampdiff(minute ,'2015-04-27 00:00:00','2015-04-27 12:00:00') as diff ;
+------+
| diff |
+------+
| 720 |
+------+
1 row in set (0.00 sec)
Also you are using alias in the where clause which is not allowed you have to change that to having clause
SELECT start_time
,end_time
,timestampdiff(minute,start_time,end_time) AS DiffDate
FROM my_tbl
WHERE start_time >= '2015-04-27 00:00:00'
AND end_time <= '2015-04-28 00:00:00'
AND end_time >= '2015-04-27 00:00:00'
having DiffDate < 100
LIMIT 1000;
Coming from MS SQL I was using DATEDIFF but the solution is:
SELECT start_time
,end_time
,TIMESTAMPDIFF(SECOND,start_time,end_time) AS DiffDate
FROM my_tbl
WHERE start_time >= '2015-04-27 00:00:00'
AND end_time <= '2015-04-28 00:00:00'
AND end_time >= '2015-04-27 00:00:00'
AND DiffDate < 100
LIMIT 1000;
I would like to know if there is a better solution from that.
Start_time and end_time are datetime column. So use TimeDIFF..
SELECT start_time, end_time, TIMEDIFF(end_time, start_time) AS DiffDate
FROM my_tbl
WHERE start_time >= '2015-04-27 00:00:00'
AND end_time <= '2015-04-28 00:00:00'
AND end_time >= '2015-04-27 00:00:00'
AND DiffDate < 100
LIMIT 1000;
SELECT start_time,end_time,DATEDIFF(end_time, start_time) AS DiffDate
FROM my_tbl
WHERE start_time >= '2015-04-27 00:00:00'
AND end_time between '2015-04-27 00:00:00' AND '2015-04-28 00:00:00'
AND for date diff < 100
LIMIT 1000;
Related
I have a problem retrieving data from a table between two dates.
Everything works as it should when two dates differ from each other, but when I want to search for all records from today, it searche nothing:
SELECT * from exampletable where created_date >= '2022-10-28' AND created_date<= '2022-10-28'
To get all records for a single day using a TIMESTAMP or DATETIME type column you have these options:
WHERE DATE(created_date) = '2022-10-28'
However - Wrapping a column into a function you will loose the ability to use an index.
Other ways which can use an index:
WHERE created_date BETWEEN '2022-10-28 00:00:00' AND '2022-10-28 23:59:59'
WHERE created_date >= '2022-10-28'
AND created_date < '2022-10-29'
WHERE created_date >= '2022-10-28'
AND created_date < '2022-10-28' + INTERVAL 1 DAY
Your query
where created_date >= '2022-10-28' AND created_date <= '2022-10-28'
is equivalent with
where created_date = '2022-10-28'
If created_date is of type DATETIME or TIMESTAMP it is the same as
where created_date = '2022-10-28 00:00:00'
and the query would only return records with the exact time of 00:00:00.
This is my query
WHERE id = 14 AND start_time BETWEEN '2019-10-24 00:00:00' AND '2019-12-12 23:59:59'
ORDER BY created_date LIMIT 0 , 10
When I run this query then it returns me data of this data also. -> 2019-10-23T19:23:41.000Z
Issue: When I pass the 2019-10-24 then why it gives me data of 2019-10-23 date?
Note: start_time has a data type -> datetime in db.
It's not a issue by the way it's correct output.
Try this way
DATE_FORMAT(start_time, "%Y-%m-%d %H:%i:%s") as start_time
Due to diffrent formate, It may confused you.
use less than or equal to '<=' or greater than or equal to '>=' operator instead of BETWEEN.
Use this condition in your query.
WHERE id = 14 AND DATE(start_time) >= DATE('2019-10-24 00:00:00') AND DATE(start_time) <= DATE('2019-12-12 23:59:59') ORDER BY created_date LIMIT 0 , 10
OR
WHERE id = 14 AND DATE(start_time) >= '2019-10-24' AND DATE(start_time) <= '2019-12-12') ORDER BY created_date LIMIT 0 , 10
I would like to retrieve the top 1 value of result set of query which is connected using Union
SELECT TOP 1 * FROM
(
SELECT paused_time as end_time
FROM production_time
WHERE created_time = curdate()
UNION
SELECT resumed_time as end_time
FROM pause_timer
WHERE created_time = curdate()
UNION
SELECT end_time as end_time
FROM timer_idle
WHERE created_time = curdate()
) as end_time
ORDER BY end_time DESC
But could not get the expected result.
There is no TOP keyword in MySQL as far as I am aware. What you require is Limit:
SELECT * FROM
(
SELECT paused_time as end_time FROM production_time WHERE created_time = curdate()
UNION
SELECT resumed_time as end_time FROM pause_timer WHERE created_time = curdate()
UNION
SELECT end_time as end_time FROM timer_idle WHERE created_time = curdate()
) as end_time
ORDER BY end_time DESC
LIMIT 1
I'm in over my head with this query. I have a table which looks something like this (simplified):
Date Weight kg
-------------------
2012-04-16 12.4
2012-04-17 9.6
2012-04-16 5.4
2012-04-18 2.8
2012-04-16 4.5
... ...
I want the query to return this result:
Week.no. <3kg 3-7kg >7kg
----------------------------
16 2.8 9.9 22.0
... ... ... ....
This is what I have so far:
SELECT *, CONCAT(WEEK(`Date`)) AS Week, SUM(`Weight`) AS TotalWeight,
(SELECT SUM(`Weight`) FROM tbl_fangster WHERE `Weight` < 3 AND
`Date` >= '2012-04-01 00:00:00' AND `Date` <= '2012-04-30 00:00:00'
AND `Species` = 'Salmon' ) AS SumSmall
FROM tbl_fangster
WHERE `Date` >= '2012-04-01 00:00:00'
AND `Date` <= '2012-04-30 00:00:00'
AND `Species` = 'Salmon'
GROUP BY CONCAT(WEEK(`Date`))
but SumSmall returns the same number on each row, namely the total SumSmall rather than the SumSmall for each week. I've tried to copypaste my GROUP clause into the subquery, but it didn't work.
Use a CASE statement to handle the various conditions.
SELECT *, CONCAT(WEEK(`Date`)) AS Week, SUM(`Weight`) AS TotalWeight,
SUM(CASE WHEN Weight < 3 THEN Weight ELSE 0 END) AS SumSmall,
SUM(CASE WHEN Weight >= 3 AND Weight <= 7 THEN Weight ELSE 0 END) AS SumMedium,
SUM(CASE WHEN Weight > 7 THEN Weight ELSE 0 END) AS SumLarge
FROM tbl_fangster
WHERE `Date` >= '2012-04-01 00:00:00'
AND `Date` <= '2012-04-30 00:00:00'
AND `Species` = 'Salmon'
GROUP BY CONCAT(WEEK(`Date`))
Suppose I have the following table:
id | value | start_time | stop_time
-----------------------------------------------------------------
1 | value1 | 06:00:00 | 11:00:00
2 | value2 | 12:00:00 | 13:00:00
I need to select records whose time range overlap with 05:00–11:30. That means the record to be returned is row 1.
I tried using a query like this but it fails when the given time range crosses midnight. Perhaps it's impossible to do it without involving the date.
SELECT * FROM table WHERE
(
(start_time >= '05:00:00' AND stop_time <= '11:30:00') ||
(start_time >= '05:00:00' AND start_time <= '05:00:00') ||
(start_time <= '05:00:00' AND stop_time >= '11:30:00') ||
(stop_time >= '11:30:00' AND stop_time <= '05:00:00')
)
I'm thinking of enumerating all the minutes for the given time range and filtering using BETWEEN but there may be a better solution.
Dave,
The easiest way which i can see for you to achieve the desired result will be to use 'TIME' type for start_time and 'stop_time` fields.
Then you can select like this:
SELECT * FROM `YOUR_TABLE_NAME_HERE` WHERE `start_time` > '05:00:00' AND `stop_time` < '11:30:00'
Every range will need to be represented with a conditional, like so:
IF(start_time < stop_time, start_time >= '05:00:00' AND stop_time <= '11:30:00', start_time <= '05:00:00' AND stop_time >= '11:30:00')