join based on the closest date value - sql

I would like to join on date that is the closest to my end date, whether it's before or after my end date.
TableA:
A.ID A.StartDate A.EndDate A.ModifiedDate
1 1/1/17 1/15/18 1/16/18
2 2/1/17 3/1/18 3/2/18
TableB
B.SetDate B.ID Reason
1/16/18 1 LeftGroup
3/8/18 2 Booted
3/6/18 2 Terminated
Output:should look like this
Final:
ID StartDate EndDate ModifiedDate SetDate Reason
1 1/1/17 1/15/18 1/16/18 1/16/18 LeftGroup
2 2/1/17 3/1/18 3/4/18 3/6/18 Terminated
My query below:
select *
from TableA
left join TableB b on a.id = b.id and AND TRUNC(SH.SET_DATE) BETWEEN TRUNC(CCP.STARTDATE) AND GREATEST(TRUNC(CCP.ENDDATE), TRUNC(CCP.MODIFIED_DATE))
--Need to add another join to pickup any loose enddates that do not have a reason associated to them.
something like
left join TableB b2 on a.id = b2.id
and ...
and tableb is null

This is essentially answered in this post.
Where is says:
SELECT TOP 1 *
FROM x
WHERE x.date < 'somedate'
ORDER BY x.date DESC
HTH,
Sean

Related

SQL - FILTER condition in JOIN clause

I am inserting data to an already existing TableA and the following query is a part of a stored procedure. This part of the query inserts values into some columns of TableA. The stored procedure is very lengthy which has several insert statements to fill out the different columns in TableA.
INSERT INTO TableA (ID, Event, Date, Amount, Status_, Country)
(SELECT DISTINCT ID, Event, Date, Amount, c.Status, b.Country
FROM TableA1 a
JOIN TableB1 b ON b.employeeID = a.ID
JOIN TableC1 c ON c.Status = ‘Active’)
Usually, a join condition consists of two columns, for example (a.Status_ = c.Status). But here, it's replaced with a filter condition (JOIN TableC1 c ON c.Status = ‘Active’).
The select query alone executes well and returns results. I'm trying to understand the effect made by this filter condition.
Can you explain, please?
Thanks
It's the same as this...
SELECT DISTINCT
ID, Event, Date, Amount, c.Status, b.Country
FROM
(
TableA1 a
INNER JOIN
TableB1 b
ON b.employeeID = a.ID
)
CROSS JOIN
(
SELECT * FROM TableC1 WHERE Status = 'Active'
)
c
In effect, the INNER JOIN is resolved, and then each row from that is joined to every row from TableC1 WHERE Status = 'Active'
For example
TableA1
ID
Event
Date
Amount
1
e1
2022-01-01
11
2
e2
2022-02-02
22
TableB1
EmployeeID
Country
1
c1
2
c2
TableC1
some_id
Status
1
Sleeping
2
Active
3
Active
4
Sleeping
Would yield...
ID
Event
Date
Amount
Country
Status
(some_id, added by me)
1
e1
2022-01-01
11
c1
Active
2
1
e1
2022-01-01
11
c1
Active
3
2
e2
2022-02-02
22
c2
Active
2
2
e2
2022-02-02
22
c2
Active
3
You probably should have...
INNER JOIN
TableC1 c
ON c.some_id = b.some_other_id
AND c.Status = 'Active'

How to join only latest date values from another table and prevent duplication

I'm trying to lookup a unique value from table b and get it into table a.
Table b stores multiple values that are changing by date.
I would like to join but only getting the values with the latest date from table b.
Table a
Unique ID
1
2
Table b
Date Unique ID Price
01/01/2019 1 100
01/02/2019 1 101
01/03/2019 1 102
01/01/2019 2 90
01/02/2019 2 91
01/03/2019 2 92
Expected result
Unique ID Price Date
1 102 01/03/2019
2 92 01/03/2019
Appreciate your help!
Have a sub-query that returns each UniqueID together with its max date. IN that result.
select * from tablename
where (UniqueID, date) in (select UniqueID, max(date)
from tablename
group by UniqueID)
You want correlated subquery :
select b.*
from tableb b
where b.date = (select max(b1.date) from tableb b1 where b1.UniqueID = b.UniqueID);
If you want to go with JOIN then you can do JOIN with subquery :
select a.UniqueID , b.Price, b.Date
from tablea a inner join
tableb b
on b.UniqueID = a.UniqueID
where b.date = (select max(b1.date) from tableb b1 where b1.UniqueID = a.UniqueID);
A correlated subquery?
select b.*
from b
where b.date = (select max(b2.date) from b b2 where b2.unique_id = b.unique_id);

Get rows from table of last 5 days in IBM DB2 by joining another table

I have two tables in DB2.
table1
----
sidpk pid
--------------------------
24gsr3 xyz
67sfr9 xyz
67sts8 xyz
table2
-------------------------
sid date
--------------------------
24gsr3 2017-09-24
67sfr9 2017-09-23
67sts8 2017-09-15
.
.
and so on
I'm trying to fetch all rows that are available that were created 5 days before from today from table2.
In the above case, I expect it should return be 24gsr3, 67sfr9
What is missing in my query?
Please find it.
SELECT a.sidpk from table1 a
LEFT JOIN
table2 b
ON a.sidpk= b.sid
WHERE a.pid='xyz' AND b.date>= DATE_SUB(CURDATE(), INTERVAL 5 DAY) AND
b.date<= CURDATE()
ORDER BY b.date DESC
This should work
SELECT a.sidpk from table1 a
LEFT JOIN table2 b
ON a.sidpk= b.sid
WHERE a.pid='xyz'
AND b.date >= current date - 5 days
AND b.date <= current date
ORDER BY b.date DESC

MaxMin Function within Select Statement SQL 2012

I am having a few issues making a MAX function work within the select statement See example data below:
Table 1 Table 2
Visit_ID Car_ID Move_ID Visit_ID MoveStartDate MoveEndDate
A 1 1 A 25/07/2016 27/07/2016
B 2 2 A 28/07/2016 28/07/2016
C 1 3 B 19/07/2016 22/07/2016
D 3 4 D 28/06/2016 30/06/2016
I would like my select statement to pick the min start time and Max start time based on the Visit_ID so I would be expecting:
Result
Visit_ID Car_ID StartDate EndDate
A 1 25/07/2016 28/07/2016
B 2 19/07/2016 22/07/2016
So far I have tried I already have Inner Joins in my select statement:
,(MAX (EndDate) WHERE Visit.Visit_ID = Move.Visit_ID) AS End Date
I have looked at some other queries with a second select statement within the select so you end up with something like:
Select Visit_ID, Car_ID ,(Select MAX(EndDate) FULL OUTER JOIN Table 2 ON Table 1.Visit_ID = Table 2.Visit_ID Group By Table 1.Visit_ID) AS End Date
Hope I have provided enough info currently stumped.
If you also want Car_ID = 3 in the result:
select t1.Visit_ID, t1.Car_ID, MIN(MoveStartDate), MAX(MoveEndDate)
from table1 t1
join table2 t2 on t1.Visit_ID = t2.Visit_ID
group by t1.Visit_ID, t1.Car_ID
Returns:
SQL>select t1.Visit_ID, t1.Car_ID, MIN(MoveStartDate), MAX(MoveEndDate)
SQL&from table1 t1
SQL& join table2 t2 on t1.Visit_ID = t2.Visit_ID
SQL&group by t1.Visit_ID, t1.Car_ID;
visit_id car_id
======== =========== ==================== ====================
A 1 25/07/2016 28/07/2016
B 2 19/07/2016 22/07/2016
D 3 28/06/2016 30/06/2016
3 rows found
I did not check it but your can try this
WITH cte
AS
(select Move_ID,Visit_ID,min(MoveStartDate) AS mMS,MAX(MoveEndDate) AS mME
FROM Table_2
GROUP BY Move_ID,Visit_ID)
SELECT c.Move_ID,c.Visit_ID,T1.Car_ID,c.mMS,c.mME
FROM Table_1 as T1 JOIN cte as C
ON c.Visit_ID=T1.Visit_ID

SQL join query about dates

I have a table A as
ID Settle Date
1 1/15/2013
2 2/15/2013
and Table B as
ID From Rate
1 1/1/2013 1.5
1 2/1/2013 2
2 1/1/2013 4
2 2/1/2013 5
I want a join such that i get
ID Settle Date Rate
1 1/15/2013 1.5
2 2/15/2013 5
Basically, want to get the rate applicable for the date. Please advise
Use BETWEEN in your join:
select
a.ID
SettleDate,
Rate
FROM TableA a
INNER JOIN TableB b
ON a.SettleDate BETWEEN b.From AND b.To
AND a.ID = b.ID
OK Adjusting for your "small" change (which isn't small):
select
a.ID
SettleDate,
Rate
FROM TableA a
INNER JOIN TableB b
ON a.ID = b.ID
AND b.[From] = (SELECT MAX([From]) FROM TableB WHERE ID = a.ID AND From <= a.SettleDate)