I have a table with two columns "date_from" and "date_to". When the user inputs a date i want to get the row where te picked date is between those two, however the years on those dates dont matter for me - only the months wich form something like seasons i have come to this :
SELECT `date_from` , `date_to`
FROM `language_courses_accommodation_periods`
WHERE DATE_FORMAT( `date_from` , '%c' ) <= DATE_FORMAT( STR_TO_DATE( '2017-02-03', '%Y-%m-%d' ) , '%c' )
AND DATE_FORMAT( `date_to` , '%c' ) >= DATE_FORMAT( STR_TO_DATE( '2017-02-03', '%Y-%m-%d' ) , '%c' )
AND DATE_FORMAT( `date_from` , '%e' ) <= DATE_FORMAT( STR_TO_DATE( '2017-02-03', '%Y-%m-%d' ) , '%e' )
AND DATE_FORMAT( `date_to` , '%e' ) >= DATE_FORMAT( STR_TO_DATE( '2017-02-03', '%Y-%m-%d' ) , '%e' )
This works in some cases but a season can start for example at november and
end at march and then my query doesent return the correct result.
Use BETWEEN, which doesn't care about the order of the arguments at the right:
SELECT `date_from`, `date_to`
FROM `language_courses_accommodation_periods`
WHERE DATE_FORMAT(STR_TO_DATE('2017-02-03', '%Y-%m-%d'), '%c')
BETWEEN DATE_FORMAT(`date_from`, '%c')
AND DATE_FORMAT(`date_to`, '%c')
I don't think there is any additional value in repeating this condition with the %e format.
UPDATED: Try this query it might work by splitting the days from the months you are creating too many possibilities for the answer to be.
SELECT `date_from` , `date_to`
FROM `language_courses_accommodation_periods`
WHERE DATE_FORMAT( `date_from` , '%c-%d' ) <= DATE_FORMAT( STR_TO_DATE( '2017-02-03', '%Y-%m-%d' ) , '%c-%d' )
AND DATE_FORMAT( `date_to` , '%c-%d' ) >= DATE_FORMAT( STR_TO_DATE( '2017-02-03', '%Y-%m-%d' ) , '%c-%d' )
Related
Below my query:
SELECT DATE_FORMAT( FROM_UNIXTIME( `temp_timing` ) , '%Y-%m-%d %H:%i:%s' ) AS timing, temperature, deviceid
FROM tempdata
WHERE deviceid =29
AND DATE_FORMAT( FROM_UNIXTIME( `temp_timing` ) , '%Y-%m-%d' ) >= '2018-07-20'
AND DATE_FORMAT( FROM_UNIXTIME( `temp_timing` ) , '%Y-%m-%d' ) <= '2018-07-27'
ORDER BY timing ASC
LIMIT 0 , 30
It can be by the following query
SELECT DATE_FORMAT( FROM_UNIXTIME( `temp_timing` ) , '%Y-%m-%d %H:%i:%s' ) AS timing, AVG( temperature ) AS avg, deviceid
FROM tempdata
WHERE deviceid =29
AND DATE_FORMAT( FROM_UNIXTIME( `temp_timing` ) , '%Y-%m-%d' ) >= '2018-07-20'
AND DATE_FORMAT( FROM_UNIXTIME( `temp_timing` ) , '%Y-%m-%d' ) <= '2018-07-27'
GROUP BY DATE( timing ) , HOUR( timing )
ORDER BY `timing` ASC
LIMIT 0 , 30
Hi There I spent an afternoon getting my sql to dynamically populate the date ranges for a chart.js graph and decided to share the code to help anyone in the future.
The code that i have written will take the date range you have inputted and check the time between the two dates then depending on the time between the two dates will give you a different time span for your graph.
Here is the full query
SELECT
name AS "Company"
IF( DATEDIFF( FROM_UNIXTIME( [end_date], '%Y-%m-%d' ), FROM_UNIXTIME(
[start_date], '%Y-%m-%d' )) <= 8,
DATE_FORMAT( FROM_UNIXTIME( insert_date, '%Y-%m-%d' ), '%a %D %M'),
IF( DATEDIFF( FROM_UNIXTIME( [end_date], '%Y-%m-%d' ), FROM_UNIXTIME(
[start_date], '%Y-%m-%d' )) BETWEEN 8 AND 62,
CONCAT("WC - ", DATE_FORMAT(DATE_ADD(MAKEDATE(FROM_UNIXTIME( insert_date,
'%Y' ), 1), INTERVAL WEEK(FROM_UNIXTIME( insert_date, '%Y-%m-%d' ))
WEEK), "%d %M %Y")),
IF( DATEDIFF(FROM_UNIXTIME( [end_date], '%Y-%m-%d' ), FROM_UNIXTIME(
[start_date], '%Y-%m-%d' )) > 62,
CONCAT(MONTHNAME(STR_TO_DATE(MONTH(FROM_UNIXTIME( insert_date, '%Y-%m-%d'
)), '%m'))," ",FROM_UNIXTIME( insert_date, '%Y' )),''))) AS labels ,
CONCAT(
"Generated (",COUNT( insert_date ),")"
) AS toolTip
FROM (inserted_cases)
WHERE insert_date BETWEEN [start_date] AND [end_date]
GROUP BY labels
Now the bit that does all the calculations for you is here and will work for quite a few other things as well but im suggesting it for chart.js as i have tested it on here.
IF( DATEDIFF( FROM_UNIXTIME( [end_date], '%Y-%m-%d' ), FROM_UNIXTIME(
[start_date], '%Y-%m-%d' )) <= 8,
DATE_FORMAT( FROM_UNIXTIME( insert_date, '%Y-%m-%d' ), '%a %D %M'),
IF( DATEDIFF( FROM_UNIXTIME( [end_date], '%Y-%m-%d' ), FROM_UNIXTIME(
[start_date], '%Y-%m-%d' )) BETWEEN 8 AND 62,
CONCAT("WC - ", DATE_FORMAT(DATE_ADD(MAKEDATE(FROM_UNIXTIME(
insert_date, '%Y' ), 1), INTERVAL WEEK(FROM_UNIXTIME(
insert_date, '%Y-%m-%d' )) WEEK), "%d %M %Y")),
IF( DATEDIFF(FROM_UNIXTIME( [end_date], '%Y-%m-%d' ), FROM_UNIXTIME(
[start_date], '%Y-%m-%d' )) > 62,
CONCAT(MONTHNAME(STR_TO_DATE(MONTH(FROM_UNIXTIME( insert_date, '%Y-
%m-%d' )), '%m'))," ",FROM_UNIXTIME( insert_date, '%Y' )),''))) AS
labels ,
I hope this can help to save so time in the future. also if you know of a better way im always open to suggestion and learning new things. happy coding guys. :)
SELECT MAX( PRC_MIN_LENGTH ) PRC_MIN_LENGTH, MIN( PRC_MAX_LENGTH ) PRC_MAX_LENGTH, MAX( PRC_MIN_WIDTH ) PRC_MIN_WIDTH, MIN( PRC_MAX_WIDTH ) PRC_MAX_WIDTH
FROM (
SELECT PRDT_PRICE_CODE, MIN( PRC_MIN_LENGTH ) PRC_MIN_LENGTH, MAX( PRC_MAX_LENGTH ) PRC_MAX_LENGTH, MIN( PRC_MIN_WIDTH ) PRC_MIN_WIDTH, MAX( PRC_MAX_WIDTH ) PRC_MAX_WIDTH
FROM PRODUCT_PRICE_INFO
WHERE PRDT_PRICE_CODE
IN (
SELECT PRDT_PRICE_CODE
FROM PRODUCT
WHERE PRODUCT_ID =1
UNION SELECT PRDT_PRICE_CODE
FROM PRODUCT_OPTION
WHERE PROD_OPT_ID
IN (
'1', '101', '201', '303', '401'
)
)
AND CURDATE( )
BETWEEN DATE_SUB( CURDATE( ) , INTERVAL 1
DAY )
AND DATE_ADD( CURDATE( ) , INTERVAL 1
DAY )
GROUP BY PRDT_PRICE_CODE
)PRC_RANGE
This query is running in MySQL database but not in SQLite.
Where is the mistake and how can I fix this?
SQLite uses different date functions.
You would have to write the date comparison like this:
...
AND date('now') BETWEEN date('now', '-1 days')
AND date('now', '+1 days')
...
(This is a faithful translation, and will make the query run; but it's doubtful that this query does what you want in either MySQL or SQLite.)
SELECT COUNT(*) AS cnt FROM ( (SELECT 'used' FROM b_names_1) UNION ALL
(SELECT 'used' FROM b_names_2)) AS t
WHERE 'used' = 1
This code words very well..
But I want to sort the results with time intervals, so when I tried just to add this lines at the bottom, it doesnt work:
AND registration_date_time >= STR_TO_DATE( '2013-01-28 00:00:00', '%Y-%m-%d %H:%i:%s' ) AND registration_date_time < STR_TO_DATE( '2013-02-02 00:00:00', '%Y-%m-%d %H:%i:%s' )
The piece of code looked like this:
SELECT COUNT(*) AS cnt FROM ( (SELECT 'used' FROM b_names_1) UNION ALL
(SELECT 'used' FROM b_names_2)) AS t
WHERE 'used' = 1 AND registration_date_time >= STR_TO_DATE( '2013-01-28 00:00:00', '%Y-%m-%d %H:%i:%s' ) AND registration_date_time < STR_TO_DATE( '2013-02-02 00:00:00', '%Y-%m-%d %H:%i:%s' )
it gives me the following error: #1054 - Unknown column 'registration_date_time' in 'where clause'
any idea how to correctly implement that date code to the SQL?
Those columns have to come FROM somewhere, add them to the inner queries:
SELECT COUNT(*) AS cnt
FROM (
(SELECT 'used', registration_date_time FROM b_names_1)
UNION ALL
(SELECT 'used', registration_date_time FROM b_names_2)
) AS t
WHERE 'used' = 1
AND registration_date_time >= STR_TO_DATE( '2013-01-28 00:00:00', '%Y-%m-%d %H:%i:%s' )
AND registration_date_time < STR_TO_DATE( '2013-02-02 00:00:00', '%Y-%m-%d %H:%i:%s' )
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Calculate a running total in MySQL
I need to get the sum of counts which is grouped for each of the dates.Now I am running the following query and getting the out put as follows :
SELECT `timestamp` , COUNT( * )
FROM `A`
WHERE `timestamp` >= '2013-01-04 07:12:12'
GROUP BY DATE_FORMAT( `timestamp` , '%Y-%m-%d' )
and I am getting
OUTPUT:
timestamp count(*)
-------------------------------------------------- -----------
2013-01-04 07:58:21 4
2013-01-05 09:28:56 38
2013-01-06 00:03:04 10
Now what I need is, I need to get the total sum of the counts grouped by date. That is for the second date it should be 42 and for third date it should be 52. How can I do this in a query?
Give it a try:
SELECT `timestamp` , #sum:= ifnull(#sum, 0 ) + COUNT( * )
FROM `A`
WHERE `timestamp` >= '2013-01-04 07:12:12'
GROUP BY DATE_FORMAT( `timestamp` , '%Y-%m-%d' )
Try :
SELECT
timestamp t ,
(select count(*) from A where timestamp <= t)
FROM A
GROUP BY timestamp
ORDER BY timestamp
Can you try below SQL
SELECT DATE_FORMAT( ts_date , '%Y-%m-%d' ) as ts_dt_out, SUM(cnt)
FROM
(
SELECT `timestamp` ts_date , COUNT( * ) as cnt
FROM `A`
WHERE `timestamp` >= '2013-01-04 07:12:12'
GROUP BY DATE_FORMAT( `timestamp` , '%Y-%m-%d' )
)
as inner
WHERE ts_date >= '2013-01-04 07:12:12'
GROUP BY ts_dt_out
Note: Not tested let me know if it does not work
Here is a simple way
SELECT
`timestamp` ,
COUNT( * )
FROM `A`
WHERE `timestamp` >= '2013-01-04 07:12:12'
GROUP BY DATE(`timestamp`)