How to calculate time difference of rows using Lag/Lead in DB2? - sql

I have this following table:
id Date Hour Description Username
1 2015-05-13 10:08 SessionClosed Thierry
2 2015-05-12 23:30 SessionClosed Leao
3 2015-05-12 20:50 SessionOpened Thierry
4 2015-05-11 17:10 SessionOpened Leao
How can I calculate the difference in time of each user's session?
I'm using DB2.
The result should look like this:
id Date Hour Description Username DiffTime
1 2015-05-13 10:08 SessionClosed Thierry 14:18
2 2015-05-12 23:30 SessionClosed Leao 30:20
3 2015-05-12 20:50 SessionOpened Thierry 00:00
4 2015-05-11 17:10 SessionOpened Leao 00:00

Related

How to merge records with aggregate historical data?

I have a table with individual records and another which holds historical information about the individuals in the former.
I want to extract information about the individuals from the second table. Both tables have timestamp. It is very important that the historical information happened before the record in the first table.
Date_Time name
0 2021-09-06 10:46:00 Leg It Liam
1 2021-09-06 10:46:00 Hollyhill Island
2 2021-09-06 10:46:00 Shani El Bolsa
3 2021-09-06 10:46:00 Kilbride Fifi
4 2021-09-06 10:46:00 Go
2100 2021-10-06 11:05:00 Slaneyside Babs
2101 2021-10-06 11:05:00 Hillview Joe
2102 2021-10-06 11:05:00 Fairway Flyer
2103 2021-10-06 11:05:00 Whiteys Surprise
2104 2021-10-06 11:05:00 Astons Lucy
The name is the variable by which you connect the two tables:
Date_Time name cc
13 2021-09-15 12:16:00 Hollyhill Island 6.00
14 2021-09-06 10:46:00 Hollyhill Island 4.50
15 2021-05-30 18:28:00 Hollyhill Island 3.50
16 2021-05-25 10:46:00 Hollyhill Island 2.50
17 2021-05-18 12:46:00 Hollyhill Island 2.38
18 2021-04-05 12:31:00 Hollyhill Island 3.50
19 2021-04-28 12:16:00 Hollyhill Island 3.75
I want to add aggregated data from this table to the first. Such as adding the cc mean and count.
Date_Time name
1 2021-09-06 10:46:00 Hollyhill Island
This line I would add 5 for cc count and 3.126 for the cc mean. Remember the historical records need to be before the date time of the individual records.
I am a bit confused how to do this efficiently. I know I need to groupby the historical data.
Also the individual records are usually in groups of Date_Time, if that makes it any easier.
IIUC:
try:
out=df1.merge(df2,on='name',suffixes=('','_y'))
#merging both df's on name
out=out.mask(out['Date_Time']<=out['Date_Time_y']).dropna()
#filtering results
out=out.groupby(['Date_Time','name'])['cc'].agg(['count','mean']).reset_index()
#aggregrating values
output of out:
Date_Time name count mean
0 2021-09-06 10:46:00 Hollyhill Island 5 3.126

SQL : GROUP and MAX multiple columns

I am a SQL beginner, can anyone please help me about a SQL query?
my table looks like below
PatientID Date Time Temperature
1 1/10/2020 9:15 36.2
1 1/10/2020 20:00 36.5
1 2/10/2020 8:15 36.1
1 2/10/2020 18:20 36.3
2 1/10/2020 9:15 36.7
2 1/10/2020 20:00 37.5
2 2/10/2020 8:15 37.1
2 2/10/2020 18:20 37.6
3 1/10/2020 8:15 36.2
3 2/10/2020 18:20 36.3
How can I get each patient everyday's max temperature:
PatientID Date Temperature
1 1/10/2020 36.5
1 2/10/2020 36.3
2 1/10/2020 37.5
2 2/10/2020 37.6
Thanks in advance!
For this dataset, simple aggregation seems sufficient:
select patientid, date, max(temperature) temperature
from mytable
group by patientid, date
On the other hand, if there are other columns that you want to display on the row that has the maximum daily temperature, then it is different. You need some filtering; one option uses window functions:
select *
from (
select t.*,
rank() over(partition by patientid, date order by temperature desc)
from mytable t
) t
where rn = 1

Create an event log from an excel file by turning columns into repeated rows

I have an Excel sheet like the following:
ID Arrival Passed Berthing Date UnBerthing Date Departure Passed
1 13/05/2017 15:30 13/05/2017 16:00 31/05/2017 20:44 31/05/2017
2 15/05/2017 16:56 15/05/2017 17:15 16/05/2017 00:00 16/05/2017
3 20/05/2017 09:54 20/05/2017 10:26 20/05/2017 18:07 20/05/2017
4 24/05/2017 16:09 24/05/2017 16:35 25/05/2017 01:03 25/05/2017
5 29/05/2017 10:30 29/05/2017 10:45 29/05/2017 17:33 29/05/2017
I need this in the following format:
ID Event Time
1 Arrival 13/05/2017 15:30
1 Berth 13/05/2017 16:00
1 UnBerth 31/05/2017 20:44
1 Departure 31/05/2017 20:58
2 Arrival 15/05/2017 16:56
2 Berth 15/05/2017 17:15
2 UnBerth 16/05/2017 00:00
2 Departure 16/05/2017 00:04
etc
I've searched the web and this site(youtube...), but with no right answer, i've tried the transpose function and pivot table, but i couldn't make it.
Any help would be appreciated.
Thanks you.
Assuming that your dataset is in range A2:E6.
For getting ID:
=INDEX($A$2:$E$6,CEILING(ROWS($A$1:A1)/4,1),1)
For getting Event:
=CHOOSE(MOD(ROWS($A$1:A1)-1,4)+1,"Arrival","Berth","Unberth","Departure")
For getting Time:
=INDEX($A$2:$E$6,CEILING(ROWS($A$1:A1)/4,1),MOD(ROWS($A$1:A1)-1,4)+2)
and then copy down until you get error.

How to split 24 Hour time by 10 minutes

I am trying to make a graph on BI which needs me to make so the time variable in my database groups for each 10 minutes IE - 11:04:00, 11:08:30, 11:00:28 are grouped as 11-1 then 11-2, ... ,11-6, 12-1 so on. -
06:00:00 06-1
06:03:00
06:06:00
06:09:00
06:12:00 06-2
06:15:00
06:18:00
06:21:00 06-2
06:24:00
06:27:00
06:30:00 06-3
06:33:00
06:36:00
06:39:00
06:42:00 06-4
06:45:00
06:48:00
06:51:00 06-5
06:54:00
06:57:00
07:00:00 07-1
07:03:00
07:06:00
07:09:00
07:12:00 07-2
Is there anyway I can do this on the BI?
Thank you for helping.
This will give you 10-minute block groupings, starting with 0:
=FormatDate([Date];"HH")+"-"+Floor(ToNumber(FormatDate([Date];"mm"))/10)

SQL - Working Out Time 'Period'

I am compiling a report from 2 different data sources. The request is that I provide an hourly breakdown of sales, quotes and calls so I can provide the call to quote rate etc.
The telephone data comes in the format of 'hourly' like below:
Date Start Offered Answered
----------------------- ------------- ------------------------------
2016-05-09 00:00:00.000 08:00 0 0
2016-05-09 00:00:00.000 09:00 7 5
2016-05-09 00:00:00.000 10:00 7 7
2016-05-09 00:00:00.000 11:00 7 6
2016-05-09 00:00:00.000 12:00 10 10
2016-05-09 00:00:00.000 13:00 5 5
2016-05-09 00:00:00.000 14:00 2 2
2016-05-09 00:00:00.000 15:00 2 2
2016-05-09 00:00:00.000 16:00 7 7
2016-05-09 00:00:00.000 17:00 7 7
2016-05-09 00:00:00.000 18:00 0 0
2016-05-09 00:00:00.000 19:00 0 0
This suits me down to the ground so I can show the data hourly i.e. between 08:00 and 09:00 there were no calls offer and subsequently none answered.
Now the difference here is the Quote/Sales system provides the time as such 0932, 1001 etc.
What would be the best way to put these Quotes/Sales into the appropriate 'hourly' pots.
I.e. 0932 should essentially be 09:00 as a time below and 1001 should be 10:00.
Is the best way to accomplish this just CASE statements based on the first 2 digits of the time i.e.
CASE WHEN SUBSTRING([QuoteTime],1,2) = 09 THEN '0900' END
I figure I could do it this way but it would be 'cumbersome' and quite a performance hit on a number of rows.
Thoughts?