I am trying to get last 7 days of data from my database. I have a table called date and I know that I could easily use date >= DATE(NOW()) - INTERVAL 7 DAY but that won't work for me because I have date values like that: Jan 22 2017 16: +0, Jan 22 2017 15: +0, Jan 22 2017 14: +0, Jan 22 2017 13: +0, Jan 22 2017 12: +0 etc. What could be the different way doing it?
Regards
Seems like you store the date as a string.
In this case you could use STR_TO_DATE
SELECT STR_TO_DATE(date,'%M %d %Y %h: +0') AS converted_date, [...] FROM [...] WHERE converted_date >= DATE(NOW()) - INTERVAL 7 DAY
SQL Fiddle
Related
I have usages table which contains field values like below
id | page_open_time
1, 28 May 2019 08:39:11
2, 12 July 2019 18:39:11
3, 22 December 2019 11:39:11
4, 23 June 2019 12:39:11
Now I would like to get all records where page_open_time is greater than 1 july, 2019. For this, I wrote this query below , but it didnt help. Is there any way, I can perform this ?
SELECT * FROM `usages` where page_open_time > '2019-07-01'
Your dates are not in a valid MySQL DATETIME format, so you need to convert them before comparison, using STR_TO_DATE:
SELECT *
FROM usages
WHERE STR_TO_DATE(page_open_time, '%d %M %Y %H:%i:%s') > '2019-07-01'
Output
id page_open_time
2 12 July 2019 18:39:11
3 22 December 2019 11:39:11
Demo on dbfiddle
I have some data which (broadly speaking) consist of following fields:
Person TaskID Start_time End_time
Alpha 1 'Wed, 18 Oct 2017 10:10:03 GMT' 'Wed. 18 Oct 2017 10:10:36 GMT'
Alpha 2 'Wed, 18 Oct 2017 10:11:16 GMT' 'Wed, 18 Oct 2017 10:11:28 GMT'
Beta 1 'Wed, 18 Oct 2017 10:12:03 GMT' 'Wed, 18 Oct 2017 10:12:49 GMT'
Alpha 3 'Wed, 18 Oct 2017 10:12:03 GMT' 'Wed, 18 Oct 2017 10:13:13 GMT'
Gamma 1 'Fri, 27 Oct 2017 22:57:12 GMT' 'Sat, 28 Oct 2017 02:00:54 GMT'
Beta 2 'Wed, 18 Oct 2017 10:13:40 GMT' 'Wed, 18 Oct 2017 10:14:03 GMT'
For this data, my required output is something like:
Person TaskID Time_between_attempts
Alpha 1 NULL ['Wed, 18 Oct 2017 10:10:03 GMT' - NULL]
Alpha 2 0:00:40 ['Wed, 18 Oct 2017 10:11:16 GMT' -'Wed, 18 Oct 2017 10:10:36 GMT']
Beta 1 NULL ['Wed, 18 Oct 2017 10:12:03 GMT' - NULL]
Alpha 3 0:00:35 ['Wed, 18 Oct 2017 10:12:03 GMT' -'Wed, 18 Oct 2017 10:11:28 GMT']
Gamma 1 NULL ['Fri, 27 Oct 2017 22:57:12 GMT' - NULL]
Beta 2 0:00:51 ['Wed, 18 Oct 2017 10:13:40 GMT' -'Wed, 18 Oct 2017 10:12:49 GMT']
My requirements are as below:
a. For a given person (Alpha, Beta or Gamma), the first occurrence of the variable 'time_between_attempts' would be zero/NULL - in the example I have shown it as NULL.
b. The second (and the subsequent) times, the same person appears will have a non NULL or non-zero 'time_between_attempts'. This variable is calculated by taking the difference between the ending time of the previous task and the starting time of the next task.
I have following question in this regard:
How to write a SQL script which can help me achieve the desired output?
Please note that the TaskID is written as integer just for simplification. In the original data, TaskID is complicated and consists of non-continuous strings as:
'q:1392763916495:441',
'q:1392763916495:436'
Any advice on this would be greatly appreciated.
Using self Join() method.
SELECT a.person,
a.taskid,
TIMEDIFF (DATE_FORMAT(STR_TO_DATE(a.Start_time, '%a, %d %b %Y %H:%i:%s'), '%Y-%m-%d %H:%i:%s') ,DATE_FORMAT(STR_TO_DATE(b.End_time, '%a, %d %b %Y %H:%i:%s'), '%Y-%m-%d %H:%i:%s') ) as Time_between_attempts,
a.Start_time,
b.End_time
FROM test a
LEFT JOIN test b
ON a.person = b.person
AND a.taskid = b.taskid + 1
ORDER BY 1, 2;
But this will ignore timezone.
This answers the original version of the question.
You can use lag() and timestampdiff() for the calculation. Assuming your value is a real date/time or timestamp, then you can easily calculate the value in seconds:
select t.*,
timestampdiff(start_time,
lag(end_time) over (partition by person_id order by start_time)
seconds
)
from t;
If the values are stored as string, fix the data! In the meantime, you can use str_to_date() in the function.
To get this as a time value:
select t.*,
(time(0) +
interval timestampdiff(start_time,
lag(end_time) over (partition by person_id order by start_time)
seconds
) second
)
from t;
I am creating SQL reports to query results from NetBackup OpsCenter Database (MySQL?)
And am learning how to use SQL, so please forgive my ignorance.
The report I created below, shows the failures for the previous day (as opposed to previous 24 hours, which would be different each time its run).
SELECT (GETDATE()-1) AS 'Date', statusCode AS STATUS, COUNT(*) AS COUNT
FROM domain_JobArchive
WHERE DATEDIFF(day, UtcBigIntToNomTime(endTime), GETDATE()) =1
and masterServerId=59
GROUP BY statusCode;
And I wanted to change this, so it reports the Date being used as a header ... not its own column.
current output:
Count of errors for yesterday (non-PCI)
Date STATUS COUNT
Apr 24, 2013 11:43:10 AM 288 1
Apr 24, 2013 11:43:10 AM 0 6861
Apr 24, 2013 11:43:10 AM 1 52
Apr 24, 2013 11:43:10 AM 6 63
Apr 24, 2013 11:43:10 AM 50 1
Apr 24, 2013 11:43:10 AM 58 2
Apr 24, 2013 11:43:10 AM 191 1
Total 7 Rows , 1 Page(s)
desired output:
Count of errors for Apr 24, 2013 (non-PCI)
STATUS COUNT
0 6861
1 52
6 63
50 1
58 2
191 1
288 1
Total 7 Rows , 1 Page(s)
This way, each time you execute - it pulls up the same exact data.
And it would be even better, if it went from 7am yesterday to 7am today ... regardless of when its executed.
I think this should work. Subtract 7 hours from your date time, then compare it to yesterday's date. You can just remove the date from you SELECT statement. It's not needed.
SELECT statusCode AS STATUS, COUNT(*) AS COUNT
FROM domain_JobArchive
WHERE DATE(DATE_SUB(UtcBigIntToNomTime(endTime), INTERVAL 7 HOUR)) = DATE_SUB(curdate(), INTERVAL 1 DAY)
and masterServerId=59
GROUP BY statusCode;
I have been recording Twitter data for a project I'm working on the date information is saved as Thu, 14 Jul 2011 06:21:48 +0000 in a string field.
How do I select data that falls between two dated using mySQL? I can get data larger than a value or smaller than a value but not between to values.
The data for example is:
Thu, 14 Jul 2011 06:21:48 +0000
Thu, 14 Jul 2011 12:18:21 +0000
Thu, 14 Jul 2011 18:48:00 +0000
Thu, 14 Jul 2011 23:48:02 +0000
Fri, 15 Jul 2011 06:48:10 +0000
Fri, 15 Jul 2011 12:48:00 +0000
Fri, 15 Jul 2011 18:43:32 +0000
Fri, 15 Jul 2011 23:44:08 +0000
Sat, 16 Jul 2011 06:47:08 +0000
Sat, 16 Jul 2011 12:46:49 +0000
Sat, 16 Jul 2011 18:45:41 +0000
Sat, 16 Jul 2011 23:41:27 +0000
My SQL string is:
SELECT *
FROM twitter
WHERE SUBSTR(twitter_date, 6, 11) >= '2011-06-15'
AND SUBSTR(twitter_date, 6, 11) <= '2011-06-21'
I've tried BETWEEN statements as well but no luck.
Any help will be appreciated!
You can't use between because they are strings and not actual date fields. If you know the format of the date will always be the same, you can do the following:
STR_TO_DATE(str,format)
SELECT *
FROM twitter
WHERE STR_TO_DATE(twitter_date, '%a, %c %b %Y %k:%i:%s')
between '2011-06-15' AND '2011-06-21'
You are comparing "6 Jul 2011" with 2011-06-15.
It should be
SELECT *
FROM twitter
WHERE SUBSTR(twitter_date, 6, 11) >= '6 Jul 2011'
AND SUBSTR(twitter_date, 6, 11) <= '21 Jul 2011'
You may want to look into MySQLs STR_TO_DATE function. This will give you a date and between should work as expected.
The query will fail. You're comparing two STRINGS, not dates. You need to tell MySQL explicitly that you want to treat the strings as dates. Since it's date information, store it in a date or datetime field type in MySQL, which saves you all this trouble:
SELECT STR_TO_DATE(SUBSTR(twitter_date, 6, 11)) >= '2011-06-15' ...
Forcing your left-hand-side to be a proper date value will hint to MySQL that it should treat the right-hand-side as a date as well.
Once you have altered your table to hold the date data in proper DATETIME fields, you may want to refer to my answer to the following question regarding dates and filtering them, which may prove useful.
For example, in your query / data above, what would happen if you had no data for one of the dates you were querying? You'd get no output for that date, which may look odd when charting, etc. If you use the technique I describe in the attached question, you can get that additional information, and other useful stuff like the day of the week, month name, etc.
Select all months within given date span, including the ones with 0 values
Hope that helps!
I use mySql 5 and IIS.
I have products, that have a start date field and an end date field.
I need to run a query that will take user entered Start and End dates, and output the number of days that the product ran within the date range.
Example:
Offer1 - July 1 2011 thru July 31 2011
Query - July 1 2011 thru Sept 15 2011
Results = 31
Example:
Offer1 - July 1 2011 thru July 31 2011
Query - July 1 2011 thru July 15 2011
Results = 15
If your products have a start_date and an end_date and your query has a qstart_date and a qend_date, then we want the number of days between:
GREATEST(start_date, qstart_date)
and
LEAST(end_date,qend_date)
. In MySQL I think this looks like
1 + DATEDIFF ( 'd' , GREATEST(start_date, qstart_date) , LEAST(end_date,qend_date) )
And you'll want to ignore negative numbers, replacing them with "0".