Grouped differences in timestampls in SQLite3 - sql

I have a table that looks like the following:
Transaction ID
Timestamp
User ID
1
2021-11-02 8:08
USER1
2
2021-11-02 8:10
USER2
3
2021-11-02 8:07
USER2
4
2021-11-02 8:15
USER1
5
2021-11-02 8:18
USER2
I want to create a third column, that essentially says, for a given transaction, how long since that users last transaction. Essentially, subtract the users last timestamp. The output table would look like this:
Transaction ID
Timestamp
User ID
Time Taken
1
2021-11-02 8:08
USER1
None
2
2021-11-02 8:10
USER2
3
3
2021-11-02 8:07
USER2
None
4
2021-11-02 8:15
USER1
7
5
2021-11-02 8:18
USER2
8
How can I do this with a query in SQlite3?

We can use LAG() along with the JULIANDAY() function here:
SELECT
TransactionID,
Timestamp,
UserID,
COALESCE(CAST((JULIANDAY(Timestamp) -
JULIANDAY(LAG(Timestamp) OVER (PARTITION BY UserID
ORDER BY Timestamp))) * 1440 AS INTEGER), 'None') AS "TimeTaken"
FROM yourTable
ORDER BY Timestamp;
Note that in order for the above to work, your text timestamps will have to be in a valid literal format. So instead of:
2021-11-02 8:08
you would need:
2021-11-02 08:08:00

Related

finding the minimal date_time within a window of time using sql

Im trying to model some data using sql, the column i would like to generate is date_started - all others is given.
date_started = the minimal date_created with the same id1 and id2 in range of 2 hours
that not belong to any other group of rows.
for example, for date_created = 2021-11-02 05:23:41.769,
date_started = 2021-11-02 05:23:41.769 itself.
because 2021-11-02 04:10:39.823 is in range of two hours but belong to 2021-11-02 02:16:28.544 group already.
id1
id2
date_created
date_started
1
2
2021-11-02 02:16:28.544
2021-11-02 02:16:28.544
1
2
2021-11-02 02:52:52.504
2021-11-02 02:16:28.544
1
2
2021-11-02 04:10:39.823
2021-11-02 02:16:28.544
1
2
2021-11-02 05:23:41.769
2021-11-02 05:23:41.769
1
2
2021-11-02 06:33:11.564
2021-11-02 05:23:41.769
1
2
2021-11-02 08:30:14.564
2021-11-02 08:30:14.564
It is a little bit unclear what you mean as your description could be interpreted differently from what is done in your example (should a new session start whenever there is a 2h gap between the previous "first" activity or whenever there is 2h of no activity?). Either way I think looking into sessionization might be helpful here (includes lots of example code) as this is ultimately what you're trying to do.

TSQL query to return most recent record based on another columns value

I have a table that contains a list of expiration dates for various companies. The table looks like the following:
ID CompanyID Expiration
--- ---------- ----------
1 1 2016-01-01
2 1 2015-01-01
3 2 2016-04-02
4 2 2015-04-02
5 3 2014-01-03
6 4 2015-04-09
7 5 2015-07-20
8 5 2016-05-01
I am trying to build a TSQL query that will return just the most recent record for every company (i.e. CompanyID). Such as:
ID CompanyID Expiration
--- ---------- ----------
1 1 2016-01-01
3 2 2016-04-02
5 3 2014-01-03
6 4 2015-04-09
8 5 2016-05-01
It looks like there is a exact correlation between ID and Expiration. If that is true, ie the later the Expiration the higher the ID, then you could simply pull Max(ID) and Max(Expiration) which are 1:1 and group by CompanyID:
Select max(ID), CompanyID, max(Expiration) from Table group by Company ID

Running Total Over Repeating Interval in Oracle SQL

Using Oracle SQL. Data all exists in a single table named tracks.
Results Needed as an OR statement:
Need the date value of day 1, the date value of day 7, and the count of records for each instance where the number of events that occurred in a 7 day range exceeded 4 grouped by UserID and Dept;
Need the date value of day 1, the date value of day 30, and the count of records for each instance where the number of events that occurred in a 30 day range exceeded 6 grouped by UserID and Dept.
This query will be applied to a full year of sporadically scheduled events.
Each record in the example data below represents 1 event.
UserID Event Date Dept
User1 1/1/2013 A
User1 1/2/2013 A
User1 1/3/2013 A
User1 1/10/2013 A
User1 1/11/2013 A
User1 1/12/2013 A
User1 1/13/2013 A
User1 1/14/2013 A
User2 1/21/2013 B
User2 1/22/2013 B
User2 1/23/2013 B
User2 1/24/2013 B
User2 1/25/2013 B
User2 1/27/2013 B
User2 1/28/2013 B
User2 4/1/2013 B
Result set for the above example should resemble:
UserID Dept Day1 Day7 Day30 7DayEventCount 30DayEventCount
User1 A 1/10/2013 1/16/2013 2/8/2013 5 5
User1 A 1/1/2013 1/7/2013 1/30/2013 3 15
User1 A 1/2/2013 1/8/2013 1/31/2013 2 14
User1 A 1/3/2013 1/9/2013 2/1/2013 1 13
User1 A 1/4/2013 1/10/2013 2/2/2013 1 12
User1 A 1/5/2013 1/11/2013 2/3/2013 2 11
...
User2 B 1/21/2013 1/27/2013 2/19/2013 6 7
User2 B 1/22/2013 1/28/2013 2/20/2013 6 6
User2 B 1/23/2013 1/29/2013 2/21/2013 5 5

SQL - Datediff between rows with Rank Applied

I am trying to work out how to to apply a datediff between rows where a rank is applied to the USER ID;
Example of how the data below;
UserID Order Number ScanDateStart ScanDateEnd Minute Difference Rank | Minute Difference Rank vs Rank+1
User1 10-24 10:20:00 10:40:00 20 1 | 5
User1 10-25 10:45:00 10:50:00 5 2 | 33
User1 10-26 11:12:00 11:45:00 33 3 | NULL
User2 10-10 00:09:00 00:09:20 20 1 | 4
User2 10-11 00:09:24 00:09:25 1 2 | 15
User2 10-12 00:09:40 00:10:12 32 3 | 3
User2 10-13 00:10:15 00:10:35 20 4 | NULL
What i'm looking for is how to code the final column of this table.
The rank is applied to UserID ordered by ScanDateStart.
Basically, i want to know the time between the ScanDateEnd of Rank 1, to ScanDateStart of Rank2, and so on, but for each user.... (calculating time between order processing etc)
Appreciate the help
This can be achieved by performing a LEFT JOIN to the same table on the UserID column and the Rank column, plus 1.
The following (simplified) pseudo-code should illustrate how to achieve this:
SELECT R.UserID,
R.Rank,
R1.Diff
FROM Rank R
LEFT JOIN Rank R1 ON R1.UserID = R.UserID AND R1.Rank = R.Rank + 1
Effectively, you are showing the UserID and Rank from the current row, but the Difference from the row of the same UserID with the Rank + 1.

Select first and last occurrence of repeated colums in sql server

I have a table like this,
Date is in yyyy-mm-dd format
Name Date Credits
--------------------------------
Bill 2013-04-04 5
Paul 2013-04-05 4
Bill 2013-04-05 3
Angel 2013-04-07 9
Bill 2013-05-01 8
Paul 2013-05-02 7
Bill 2013-06-15 6
Angel 2013-07-22 15
Paul 2013-07-23 7
Angel 2013-08-11 9
And my expected result is
Name MinDate MaxDate Credits
-----------------------------------------------
Bill 2013-04-04 2013-06-15 1
Paul 2013-04-05 2013-07-23 3
Angel 2013-04-07 2013-08-11 0
How to form the Query. Help needed.
My approach would be something like this:
SELECT t1.name, MIN(t1.date) AS MinDate, MAX(t1.date) AS MaxDate
FROM table t1
GROUP BY t1.name
I don't know how you calculate your credits, though, so I left this one out.
If it's SUM(t1.credit) or something alike, just add this to the FROM-clause.
Hope this helps.