Running Total Over Repeating Interval in Oracle SQL - 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

Related

Grouped differences in timestampls in SQLite3

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

Creating a new calculated column in SQL

Is there a way to find the solution so that I need for 2 days, there are 2 UD's because there are June 24 2 times and for the rest there are single days.
I am showing the expected output here:
Primary key UD Date
-------------------------------------------
1 123 2015-06-24 00:00:00.000
6 456 2015-06-24 00:00:00.000
2 123 2015-06-25 00:00:00.000
3 658 2015-06-26 00:00:00.000
4 598 2015-06-27 00:00:00.000
5 156 2015-06-28 00:00:00.000
No of times Number of days
-----------------------------
4 1
2 2
The logic is 4 users are there who used the application on 1 day and there are 2 userd who used the application on 2 days
You can use two levels of aggregation:
select cnt, count(*)
from (select date, count(*) as cnt
from t
group by date
) d
group by cnt
order by cnt desc;

Search for unique record for columns with multiple identifer

This maybe a little confusing, I'm trying to get a result where it'll only return records without the "F" as tran_type. The problem is some record contains both the "C" and "F" tran_type so if I do a (where tran_type <> "F") it shows all the records (1,2,3,4,5) but I only want record_no 2 and 4 to show. Any assistance is highly appreciated. Thanks
record_no name description tran_type trancode amount
1 user1 apple C 1149 $76.27
1 user1 apple C 1149 $25.00
1 user1 apple F 1164 $(2,500.00)
1 user1 apple C 1161 $(199.76)
2 user2 orange C 1157 $150.00
2 user2 orange C 1158 $(150.00)
3 user3 orange C 1159 $(25.00)
3 user3 orange F 1164 $(1,305.62)
3 user3 orange C 1151 $16.90
3 user3 orange C 1164 $(994.38)
4 user4 orange C 1159 $10.70
4 user4 orange C 1147 $35.00
5 user5 apple C 1149 $5.50
5 user5 apple F 1164 $(50.00)
Try the following
Select *
From table1
Where record_no
Not in (select record_no from table1 where tran_type = 'F')
Assuning that table1 is the table name

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

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.