Time Difference between per person between consecutive rows - mysql

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;

Related

how can i write a query to get in between values from a column by not mentioning the complete string

for example ive got values like
date column
Mar 03 2021 21:00:00 GMT(central daylight time)
Mar 04 2021 22:00:00 GMT(india standard time)
Mar 05 2021 22:00:00 GMT(india standard time)
Mar 08 2021 21:00:00 GMT(central daylight time)
i want the values between mar 03 2021 to mar 05 2021
is there any way that i can get those values without mentioning the whole string and only by mentioning the date ive tried trim and like but they are good for getting one value but i want n number of values between those days ignoring the rest of the string in query
Presuming that all values share the same date format in the first 11 characters of that column, you'd convert it to a date value (using the TO_DATE function with appropriate format mask) and compare it to wanted date values.
Sample data in lines #1 - 6; query begins at line #7.
SQL> with test (col) as
2 (select 'Mar 03 2021 21:00:00 GMT(central daylight time)' from dual union all
3 select 'Mar 04 2021 22:00:00 GMT(india standard time)' from dual union all
4 select 'Mar 05 2021 22:00:00 GMT(india standard time)' from dual union all
5 select 'Mar 08 2021 21:00:00 GMT(central daylight time)' from dual
6 )
7 select *
8 from test
9 where to_date(substr(col, 1, 11), 'Mon dd yyyy', 'nls_date_language = english')
10 between date '2021-03-03' and date '2021-03-05';
COL
-----------------------------------------------
Mar 03 2021 21:00:00 GMT(central daylight time)
Mar 04 2021 22:00:00 GMT(india standard time)
Mar 05 2021 22:00:00 GMT(india standard time)
SQL>
Look at the "Between" Operator in sql.
https://www.w3schools.com/sql/sql_between.asp
in PL/SQL you may do date query like this :
today is: 15/03/2021
select * from table
where date_column between to_date('03/03/2021','DD/MM/YYYY')
and to_date('06/03/2021','DD/MM/YYYY')
The first thing be would to fix the data model, even if this just parameter somewhere. Beyond that however I do not think you have indicated the issue you are having other than spiting up a string, and relative poor job even of just that. But perhaps you are looking for something like:
with junk_string (js) as
( select 'Mar 03 2021 21:00:00 GMT(central daylight time)' from dual union all
select 'Mar 04 2021 22:00:00 GMT(india standard time)' from dual union all
select 'Mar 05 2021 22:00:00 GMT(india standard time)' from dual union all
select 'Mar 08 2021 21:00:00 GMT(central daylight time)' from dual
)
, junk_set( junk_date, junk_rest) as
( select cast( to_timestamp (substr(js, 1, instr(js, 'GMT') - 1)
, 'Mon dd yyyy hh24:mi:ss'
)
at time zone 'GMT' as date
)
, substr(js, instr(js, 'GMT') +3)
from junk_string
)
select *
from junk_set
where junk_date >= date '2021-03-03'
and junk_date < date '2021-03-05';

Node JS | Grabbing last 7 days of data from the database

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

Sql Server Query not getting desire Result

I want show record month wise and date wise. My query is this but I am not getting how I can achieve this.
Query is
select
iWorkItemId
, convert(varchar(20),Progress.dtProgressDate,106) as dtProgressDate
, vsDescription
, WPD.rWeightage
, Progress.ProgressPerc as ProgressPerc
, Convert(decimal(6,2)
, ((WPD.rWeightage * Progress.ProgressPerc)/100)) as TotalProgress
from tblPkgWorkItems WPD
join tblPkgWorkItemsProgress Progress on WPD.iWorkItemId = Progress.iWorkItemsId
where iPackage='7'
group by iWorkItemId, dtProgressDate,vsDescription,rWeightage,ProgressPerc,progressmonth
order by progressmonth ASC,dtProgressDate ASC , iWorkItemId ASC
Column name : (1) progressmonth (Store Month Name)
(2) dtProgressDate (Store complete date)
Example :
In our table we are storing the records like in this manner:
30 Apr 2013
31 Aug 2013
28 Feb 2013
30 Jan 2013
Then I want to show records like this
30 Jan 2014
if some record is inserted in 15 jan 2014 then After 30 jan 2014 then 15 jan 2014 record will show.
28 Feb 2014
30 Apr 2014
30 Aug 2014
Means firstly Jan records will come then Feb record then March records.
Means all records will come month by order then sort by date wise .
My data is being returned like this:
iWorkItemId dtProgressDate progressmonth rWeightage
320 30 Apr 2014 Apr 2
321 20 Apr 2014 Apr 3
320 10 Feb 2014 Feb 4
321 9 Jan 2014 Jan 7
I want record should come first 9 jan 2014 then 10 Feb 2014 (Means Order by Month) then after it 30 April 2014, then 20 april 2014 (Then Sot by Date).
iWorkItemId will repeat win every month.
For my problem is I want to show
Firstly Jan records then Feb records , then March records
And whenever Jan month is coming then All Date which is belong to jan month that will come under JAN month.
Firstly Higher date of JAN month (Means ) 31 Jan 2014, 30 jan 2014, 29 jan 2014, 28 feb 2014, then 27 feb 2014.
Means first records fetch month wise then on particular month that records sort by datewise

MS Access year to date numbers by month

I have a query in an Access database which returns the results below:
MthName 2010
Jan £4.51
Feb £10.20
Mar £17.51
Apr £22.86
May £28.82
Jun £33.30
Jul £37.96
Aug £42.52
Sep £47.88
Oct £54.25
Nov £60.52
Dec £65.80
That is fine but these are year to date numbers and I would like to create a query that could give me the actual month numbers instead.
Taking the above sample the Jan number is clearly £4.51 but the Feb number is
(£10.2-£4.51)= £5.70
I have tried using something like DLOOKUP but it seems that would be really slow and wasn't working correctly.
This can be achieved very easily in Excel but I was hoping to find a query for future use.
Thanks
Avoid using Dlookup as much as you can.
if it is possible to add column [mntNum] with SEQUENCE number of month :
mtnNum MthName 2010
01 Jan £4.51
02 Feb £10.20
03 Mar £17.51
04 Apr £22.86
05 May £28.82
06 Jun £33.30
07 Jul £37.96
08 Aug £42.52
09 Sep £47.88
10 Oct £54.25
11 Nov £60.52
12 Dec £65.80
Query
SELECT table.mntNum, table.mntName, table.[2010], [2010]-nz((SELECT [prev].[2010] FROM [table] as [prev] where [prev].[mntNum]=[table].[mntNum]-1),0) AS JUST_THIS_MONTH
FROM [table];
UPD:
Result of this query
mntNum mntName 2010 JUST_THIS_MONTH
1 Jan 4.51 4.51
2 Feb 10.2 5.69
3 Mar 17.51 7.31
4 apr 22.86 5.35
5 may 28.82 5.96
6 jun 33.3 4.48
7 jul 37.96 4.66
8 Aug 42.52 4.56
9 Sep 47.88 5.36
10 Oct 54.25 6.37
11 Nov 60.52 6.27
12 Dec 65.8 5.27999999999999

Getting data between two date strings in MySQL

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!