SQL JOIN - retrieve MAX DateTime from second table and the first DateTime after previous MAX for other value - sql

I have issue with creating a proper SQL expression.
I have table TICKET with column TICKETID
TICKETID
1000
1001
I then have table STATUSHISTORY from where I need to retrieve what was the last time (maximum time) when that ticket entered VENDOR status (last VENDOR status) and when it exited VENDOR status (by exiting VENDOR status I mean the first next INPROG status, but only first INPROG after the VENDOR status, it's always INPROG the next status after VENDOR status). Also it is also possible that VENDOR status for ID does not exist at all in STATUSHISOTRY (then nulls should be returned), but INPROG exists always - it can be before but also and after VENDOR status, if ID is not anymore in VENDOR status.
Here is the example of STATUSHISTORY.
ID TICKETID STATUS DATETIME
1 1000 INPROG 01.01.2017 10:00
2 1000 VENDOR 02.01.2017 10:00
3 1000 INPROG 03.01.2017 10:00
4 1000 VENDOR 04.01.2017 10:00
5 1000 INPROG 05.01.2017 10:00
6 1000 HOLD 06.01.2017 10:00
7 1000 INPROG 07.01.2017 10:00
8 1001 INPROG 02.02.2017 10:00
9 1001 VENDOR 03.02.2017 10:00
10 1001 INPROG 04.02.2017 10:00
11 1001 VENDOR 05.02.2017 10:00
So the result when doing the query from TICKET table and doing the JOIN with table STATUSHISTORY should be:
ID VENDOR_ENTERED VENDOR_EXITED
1000 04.01.2017 10:00 05.01.2017 10:00
1001 05.02.2017 10:00 null
Because for ID 1000 last VENDOR status was at 04.01.2017 and the first INPROG status after the VENDOR status for that ID was at 05.01.2017 while for ID 1001 the last VENDOR status was at 05.02.2017 and after that INPROG status did not happen yet.
If VENDOR did not exist then both columns should be null in result.
I am really stuck with this, trying different JOINs but without any progress.
Thank you in advance if you can help me.

You can do this with window functions. First, assign a "vendor" group to the tickets. You can do this using a cumulative sum counting the number of "vendor" records on or before each record.
Then, aggregate the records to get one record per "vendor" group. And use row numbers to get the most recent records. So:
with vg as (
select ticket,
min(datetime) as vendor_entered,
min(case when status = 'INPROG' then datetime end) as vendor_exitied
from (select sh.*,
sum(case when status = 'VENDOR' then 1 else 0 end) over (partition by ticketid order by datetime) as grp
from statushistory sh
) sh
group by ticket, grp
)
select vg.tiketid, vg.vendor_entered, vg.vendor_exited
from (select vg.*,
row_number() over (partition by ticket order by vendor_entered desc) as seqnum
from vg
) vg
where seqnum = 1;

You can aggregate to get max time, then join onto all of the date values higher than that time, and then re-aggregate:
select a.TicketID,
a.VENDOR_ENTERED,
min( EXIT_TIME ) as VENDOR_EXITED
from (
select TicketID,
max( DATETIME ) as VENDOR_ENTERED
from StatusHistory
where Status = 'VENDOR'
group by TicketID
) as a
left join
(
select TicketID,
DATETIME as EXIT_TIME
from StatusHistory
where Status = 'INPROG'
) as b
on a.TicketID = b.TicketID
and EXIT_TIME >= a.VENDOR_ENTERED
group by a.TicketID,
a.VENDOR_ENTERED
DB2 is not supported in SQLfiddle, but a standard SQL example can be found here.

Related

Is there a way to get the most recent and the original record with a number of conditions included using SQL

I want to get the most recent data and also the original data for each group in a table but with a set of conditions.
Below is the current structure of dataset/table.
Each group can have multiple items
Each item_id can have the same item_name and these are known as change item_names with one significant difference the (). The number inside defines how many iterations of changes are made.
Each item_id can have multiple status but for the example below it is simplified to only 2 status Draft->Approved.
group
date
item_id
item_name
status
price
stock
A
2022-01-01
36FG-34-45
AB-1234
Draft
15
100
B
2022-01-02
28AE-23-67
CD-4567
Approved
30
120
A
2022-01-05
45RE-12-99
DE-1234
Approved
20
300
C
2022-01-07
78ED-14-88
EA-4532
Draft
10
500
B
2022-01-05
45AB-16-77
CD-4567(1)
Draft
35
200
A
2022-01-03
76JJ-98-66
DE-1234(1)
Approved
50
250
A
2022-02-02
17KL-10-43
DE-1234(2)
Draft
12
400
C
2022-03-03
97EE-42-17
AE-2468
Approved
25
450
The output required: take the most recent item_id for each group & when involved in the change process and the status is not equal to approve then take the most recent item_id that has been approved for each group.
Also to note it won't necessarily be the second most recent record per group that is approved can be further back in the timeline and process.
group
date
item_id
item_name
status
price
stock
original_item_id
original_item_name
original_status
original_price
original_stock
A
2022-02-02
17KL-10-43
DE-1234(2)
Draft
12
400
76JJ-98-66
DE-1234(1)
Approved
50
250
B
2022-01-05
28AE-23-67
CD-4567(1)
Draft
35
200
45AB-16-77
CD-4567
Approved
30
120
C
2022-03-03
97EE-42-17
AE-2468
Approved
25
450
NULL
NULL
NULL
NULL
NULL
Your example output for group A shows the original item name (most recent item name for that group that was approved) as DE-1234(1). This has a date of 1/3/2022, however item name DE-1234 has a date of 1/5/2022 making it the most recent item id that was approved from group A. Because of that, my output differs from yours for that reference.
Here is a link to the SQL Fiddle where I recreated this.
Here is the query I created for this:
First we create a CTE that ranks your items by group to get the most recent per group.
WITH cte AS--rank records by group ordered by date DESC
(
SELECT
[group]
,[date]
,item_id
,item_name
,status
,price
,stock
,ROW_NUMBER() OVER (PARTITION BY [group] ORDER BY [date] DESC) AS rn
FROM t
)
Then we get filter the CTE to only approved and re-rank to get the most recently approved by group.
,cte2 AS--rank joined records where status = approved by group ordered by date DESC
(
SELECT
a.[group]
,a.[date]
,a.item_id
,a.item_name
,a.status
,a.price
,a.stock
,b.[group] AS original_group
,b.[date] AS original_date
,b.item_id AS original_item_id
,b.item_name AS original_item_name
,b.status AS original_status
,b.price AS original_price
,b.stock AS original_stock
,ROW_NUMBER() OVER (PARTITION BY a.[group] ORDER BY b.rn) rn--get most recent record that was approved
FROM cte a
LEFT JOIN cte b ON
a.[group] = b.[group]
AND b.rn > a.rn--b is a previous record
AND b.status = 'Approved'
WHERE a.rn = 1--Most recent item id
)
Lastly, we query cte2 filtering for only the most recent record that was approved
SELECT
[group]
,[date]
,item_id
,item_name
,status
,price
,stock
--,original_group
--,original_date
,original_item_id
,original_item_name
,original_status
,original_price
,original_stock
FROM cte2
WHERE rn = 1--filter for most recent record that was approved

Check for condition in GROUP BY?

Take this example data:
ID Status Date
1 Pending 2/10/2020
2 Pending 2/10/2020
3 Pending 2/10/2020
2 Pending 2/10/2020
2 Pending 2/10/2020
1 Complete 2/15/2020
I need an SQL statement that will group all the data but bring back the current status. So for ID 1 the group by needs a condition that only returns the Completed row and also returned the pending rows for ID 2 and 3.
I am not 100% how to write in the condition for this.
Maybe something like:
SELECT ID, Status, Date
FROM table
GROUP BY ID, Status, Date
ORDER BY ID
The problem with this is the resulting data would look like:
ID Status Date
1 Pending 2/10/2020
1 Complete 2/15/2020
2 Pending 2/10/2020
3 Pending 2/10/2020
But I need:
ID Status Date
1 Complete 2/15/2020
2 Pending 2/10/2020
3 Pending 2/10/2020
What can I do to check for the Completed status so I can only return Completed in the group by?
Do only GROUP BY the ID column. Use MIN() to chose Complete before Pending.
SELECT ID, MIN(Status)
FROM table
GROUP BY ID
ORDER BY ID
To use Date as 'last row indicator', you can:
DECLARE #Src TABLE (
ID int,
Status varchar(20),
Date Date
)
INSERT #Src VALUES
(1, 'Pending' ,'2/10/2020'),
(1, 'Complete' ,'2/15/2020'),
(2, 'Pending' ,'2/10/2020'),
(3, 'Pending' ,'2/10/2020');
SELECT TOP 1 WITH TIES *
FROM #Src
ORDER BY ROW_NUMBER() OVER (PARTITION BY ID ORDER BY Date DESC)
Result:
ID Status Date
----------- -------------------- ----------
1 Complete 2020-02-15
2 Pending 2020-02-10
3 Pending 2020-02-10

Case statement for HIVE platform

I have a table with the following columns:
ID
Scheduled Date
Status
Target Date
I need to extract 'Status' corresponding to minimum 'Appointment Date' for each ID. If not available then I need to extract status corresponding to the minimum 'Target Date' for that ID.
Sample data:
ID | Scheduled_Date | Status | Target_Date
1 12/11/2017 Completed 12/11/2017
1 12/12/2017 Completed 12/12/2017
2 12/13/2017 Completed 12/13/2017
3 12/14/2017 Pending 12/14/2017
3 12/15/2017 Pending 12/15/2017
4 Confirmed 12/18/2017
4 Confirmed 12/19/2017
5 12/14/2017 Completed 12/14/2017
5 12/15/2017 Pending 12/15/2017
Can you please correct the code that I am trying to write?
SELECT ID,
CASE WHEN ID IS NOT NULL THEN
CASE WHEN MIN(SCHEDULED_DATE) IS NOT NULL
THEN STATUS
ELSE
END
CASE WHEN MIN(TARGET_DATE) IS NOT NULL
THEN STATUS
ELSE ''
END
FROM FIRST_STATUS
Try this query.
SELECT id,
status
FROM yourtable t
WHERE COALESCE (Scheduled_Date,
Target_Date) IN
(SELECT MIN(COALESCE (Scheduled_Date,Target_Date))
FROM yourtable i
WHERE i.ID = t.id
GROUP BY i.ID);
DEMO
Use row_number() analytic function:
select id,
status
from
(
select id,
status,
row_number() over(partition by id, order by nvl(Scheduled_Date,Target_Date)) rn
from yourtable t
)s
where rn=1
;

SQL select specific group from table

I have a table named trades like this:
id trade_date trade_price trade_status seller_name
1 2015-01-02 150 open Alex
2 2015-03-04 500 close John
3 2015-04-02 850 close Otabek
4 2015-05-02 150 close Alex
5 2015-06-02 100 open Otabek
6 2015-07-02 200 open John
I want to sum up trade_price grouped by seller_name when last (by trade_date) trade_status was 'open'. That is:
sum_trade_price seller_name
700 John
950 Otabek
The rows where seller_name is Alex are skipped because the last trade_status was 'close'.
Although I can get desirable output result with the help of nested select
SELECT SUM(t1.trade_price), t1.seller_name
WHERE t1.seller_name NOT IN
(SELECT t2.seller_name FROM trades t2
WHERE t2.seller_name = t1.seller_name AND t2.trade_status = 'close'
ORDER BY t2.trade_date DESC LIMIT 1)
from trades t1
group by t1.seller_name
But it takes more than 1 minute to execute above query (I have approximately 100K rows).
Is there another way to handle it?
I am using PostgreSQL.
I would approach this with window functions:
SELECT SUM(t.trade_price), t.seller_name
FROM (SELECT t.*,
FIRST_VALUE(trade_status) OVER (PARTITION BY seller_name ORDER BY trade_date desc) as last_trade_status
FROM trades t
) t
WHERE last_trade_status <> 'close;
GROUP BY t.seller_name;
This should perform reasonably with an index on seller_name
select
sum(trade_price) as sum_trade_price,
seller_name
from
trades
inner join
(
select distinct on (seller_name) seller_name, trade_status
from trades
order by seller_name, trade_date desc
) s using (seller_name)
where s.trade_status = 'open'
group by seller_name

Get Status value based on timesheet date

I have an Assets table that has an audit log of when a particular status of that Asset changes Status... so look's something similar to this
AssetId CapexStatus Date
------- ----------- -----
AM706 1 2017-02-03
AM706 0 2017-02-07
AM706 1 2017-02-10
I then have a timesheet table which has the AssetID and a transaction date on it. I basically want to pull the Capex Status out of the AssetLog table based on the AssetId and the current Capex Status at the time of the transaction date. eg. If the transaction date is 8th Feb then the Capex Status should be "0".
Timesheet table
TimesheetId AssetId TimesheetDate
----------- ------- -------------
1 AM706 2017-02-01
2 AM706 2017-02-08
3 AM706 2017-02-12
I think something like this might do it:
select
t.*,
a.CapexStatus
from
TimeSheet t
outer apply (Select top 1 * from AssetLog al
where
al.AssetID = t.AssetID
and al.Date < t.TimesheetDate
order by al.Date desc) a
create view vwMaxCapex
as
select top 1 capexStatus, date, AssetId from AssetsLog
order by date asc
go
select a.AssetId, a.timesheetDate,
(select capexstatus
from vwMaxCapex
where date<=a.timesheetDate and assetId=a.AssetId) capex
from timetable a