Efficiently calculate time since last transaction [duplicate] - sql

This question already has answers here:
Compare 3 Consecutive rows in a table
(2 answers)
Closed 8 years ago.
I have a large table of transactions identified by user id and date. For each user's last transaction, I would like to calculate the time elapsed since the previous transaction. Is there something like a lag operator I can use to do this?

You can find an example of using Window Aggregate functions to accomplish LEAD and LAG in Teradata here.

Related

MSACCESS SQL Date greater than 3 days ago - Pass Through query to Oracle [duplicate]

This question already has answers here:
Add days Oracle SQL
(6 answers)
Closed 12 months ago.
I am having an issue trying to add a portion to my WHERE clause to get only the last 3 days of data rather than grab the whole table. The date format comes through as 'dd-Mon-yy'
Here is my current WHERE clause
WHERE ("IAINVN00"."REF_LOCN" LIKE '51C%'
OR "IAINVN00"."REF_LOCN" LIKE '511%')
This works fine, just brings back way too much data. When I add:
and "IAINVN00"."ADJDATE" >= (Date()-3)
This brings back an error of "ODBC--call failed. [Oracle][ODBC][Ora]ORA-00936: missing expression (#936)"
I have tried using this as well and get the same error
DateAdd("d",-3,Date())
In order to fix this, instead of using Date, I needed to use SysDate.
and "IAINVN00"."ADJDATE" >= sysdate - 3

Fetch the time between two timestamps in R [duplicate]

This question already has answers here:
Subset a dataframe between 2 dates
(8 answers)
Closed 5 years ago.
Please run the code below: The "patients$time" column gives the timestamp. I want to fetch all the records between two times say first row value "2017-01-02 11:41:53" and 226th row value "2017-08-07 09:06:07". I want to basically get all the records between these two times. I tried dbGetquery but am getting an error. Please help.
library(bupaR)
patients
Try this:
patients[patients$time > '2017-01-02 11:41:53' & patients$time < '2017-08-07 09:06:07',]

How can we calculate the number of days between two dates in oracle-sql ? exact syntax? [duplicate]

This question already has an answer here:
Get the number of days between two dates in Oracle, inclusive of the dates
(1 answer)
Closed 7 years ago.
I have tried this
select datediff(day,doi,dateeg) from Cardholders;
error:ORA-00904 : "DATEDIFF" invalid identifier
here doi and dateeg are of date datatype in cardholders relation ?
In Oracle, just use subtraction. This will probably do what you want:
select trunc(dateeg) - trunc(doi) from Cardholders

How to find whether two times are over lapping or not using SQL? [duplicate]

This question already has answers here:
Determine Whether Two Date Ranges Overlap
(39 answers)
Closed 8 years ago.
I have two sets of times of which, we need to check some overlapping occurs or not. For eg. (BeginTime_1, EndTime_1) and (BeginTime_2, EndTime_2) are the two sets of timings. I need to find whether these timings overlap or not. Is it possible to do using SQL. I have achieved it using C#, and now I need to do it using SQL. Is there any builtin function for achieving this? Any help will be highly appreciated. (I'm considering only time in hh:mm:ss)
The query uses the following logic:
If an EndTime occurs between the other Begin and End times, then they overlap.
Similarly if a BeginTime occurs between the other Begin and End times, then they overlap.
NOTE: It doesn't consider NULL values because even if any of the columns are NULLABLE you'd need to clarify how NULLS should be interpretted. E.g. NULL EndTime might imply it is still ongoing and should assume current time as EndTime.
SELECT *
FROM TheTable
WHERE (EndTime_1 BETWEEN BeginTime_2 AND EndTime_2)
OR (EndTime_2 BETWEEN BeginTime_1 AND EndTime_1)
OR (BeginTime_1 BETWEEN BeginTime_2 AND EndTime_2)
OR (BeginTime_2 BETWEEN BeginTime_1 AND EndTime_1)
Another approach is:
If there's no overlap, then one of the BeginTimes must be greater than the other EndTime.
So there is an overlap if the above condition is not true.
This assume begin/end times are logically consistent. I.e. you never begin before you end.
SELECT *
FROM TheTable
WHERE NOT (BeginTime_1 > EndTime_2 OR BeginTime_2 > EndTime_1)
Applying logic rules, the WHERE clause can be changed to:
WHERE BeginTime_1 <= EndTime_2 AND BeginTime_2 <= EndTime_1

VB.Net MS-Access querying the latest entry depending on date [duplicate]

This question already has an answer here:
Filter query result by most recent date
(1 answer)
Closed 8 years ago.
Here's the table below I am using in MS-Access.
There can be multiple entries for the same item but I need to get the value corresponding to the latest dated entry. For example considering the below dataset,
If I want to know what the closingStock for item 'XYZ' is, it should return 70 as that is the latest entry (as per date /dd-mm-yyyy). I am using ms-access and vb.net for this, which I am very much unfamiliar with and have tried using a max(date) etc, but access syntax/interface seems a bit weird to me now. Would appreciate any help. I would prefer to do this in vb.net code.
Try this:
SELECT Max(tblInventory.transdate) AS MaxOftransdate, tblInventory.item, Last(tblInventory.closingStock) AS LastOfclosingStock
FROM tblInventory
GROUP BY tblInventory.item;
Replace the tblInventory with whatever your table name is. You can also replace the 'Max' with 'Last' if you prefer
Craig