SQL join query about dates - sql

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)

Related

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);

join based on the closest date value

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

SQL Select Statement issue - returning rows conditionally on a 2nd table

I could really use some help with the following SQL Select statement scenario:
I need to select all rows from a table conditionally depending on whether a userID has already entered data into a second table with the same ID.
Example:
Select all rows from TABLE A for idNumber where idNumber not in
TABLE B
but for each idNumber that IS in TABLE B, still return row unless a
specific userID is in that row in TABLE B.
TABLE A
========
idNumber|type|Date
1 A 01/01/01
2 A 01/01/01
3 B 01/01/01
4 B 01/01/01
5 B 01/01/01
TABLE B
========
idNumber|type|userID
1 A 0000
3 B 0000
4 B 1111
userID to exclude records for = 1111
SQL Query should return:
idNumber|type|Date
1 A 01/01/01
2 A 01/01/01
3 B 01/01/01
5 B 01/01/01
Apologies for the long winded post but i hope it makes sense.
Many thanks in advance,
ukjezza.!!
Select idNumber, type, Date
From TableA
Where Not Exists (
Select 1
From TableB
Where TableB.idNumber = TableA.idNumber
And TableB.userID = 1111
)
Another choice:
Select TableA.idNumber, TableA.type, TableA.Date
From TableA
Left Join TableB
On TableB.idNumber = TableA.idNumber
And TableB.userId = 1111
Where TableB.idNumber Is Null
Looks like a LEFT JOIN and COALESCE could take care of it:
SELECT a.*
FROM TableA as a
LEFT JOIN TableB as b
ON a.idNumber = b.idNumber
WHERE COALESCE(b.userID, -1) != 1111
select A.*
from TableA as A
left outer join TableB as B
on A.idNumber = B.idNumber
where B.idNumber is null or
B.userID <> '1111'

sql query for 3 tables

i have 3 tables (A,B,C)
Table A -
ID Name
1 Sam
2 Manuel
3 Jane
Table B
ID Tab_A_ID Name
1 1 Meer
2 1 Kutti
3 2 Mikaro
Table C
ID Tab_B_ID Price
1 1 255.11
2 1 30.52
3 3 125.22
I need a query that shall pick up the top price for TableA-Name from TableC. So only 1 top price for 1 nae record.
e.g.-
Sam - 255.11
Manuel - 125.22
How can i get this?
To get the max price per entry in A:
SELECT a.Name,
MAX(c.price)
FROM a
INNER JOIN b
ON a.id = b.tab_a_id
INNER JOIN c
ON b.id = c.tab_b_id
GROUP BY a.id, a.name
To get the max price per entry A per entry B:
SELECT a.Name,
b.Name
MAX(c.price)
FROM a
INNER JOIN b
ON a.id = b.tab_a_id
INNER JOIN c
ON b.id = c.tab_b_id
GROUP BY a.id, b.id, a.name, b.name
Note that entries in A without corresponding entires in B or entries in B without corresponding entries in C will not appear in the result. Use LEFT JOIN if you want to include these in the result.

How to join with group by statement

I have two tables
TableA
Custid prodid Amt1
1 10 200
2 20 300
3 10 400
4 30 300
5 20 400
6 30 200
TableB
prodid Amt2
10 100
20 200
30 300
and I want to sum of all amount group by prodid. Can anyone give me both ansi and Tsql queries?
SELECT a.prodid,sum(a.Amt1 + b.Amt2) as AmtSum
FROM TableA a JOIN TableB b ON (a.prodid = b.prodid)
GROUP BY a.prodid;
What about that? :-)
SELECT SUM(a.Amt1),SUM(b.Amt2)
FROM (TableA AS a INNER JOIN TableB AS b ON
a.prodid = b.prodid)
GROUP BY a.prodid
I believe this will work anywhere:
SELECT a.Custid,a.prodid,sum(a.Amt1 + b.Amt2) as AmtSum FROM TableA a JOIN TableB b ON (a.prodid = b.prodid) GROUP BY a.Custid,a.prodid;
try this... I think it will also work if some product id not in TABLEB
SELECT a.prodid,SUM(a.Amt1),SUM(b.Amt2)
FROM TableA AS a left outer JOIN TableB AS b ON a.prodid = b.prodid
GROUP BY a.prodid