How can I get the rows with the distinct TypeID
with the maximum EffectivityDate
?
-----------------------------------------
| ID | TypeID | Value | EffectivityDate |
-----------------------------------------
| 1 | 1 | 2.3 | 1990-01-01 |
| 2 | 1 | 3.4 | 1999-10-31 |
| 3 | 2 | 1.1 | 1990-01-01 |
| 4 | 2 | 2.2 | 1999-10-31 |
| 5 | 3 | 6.1 | 1999-10-31 |
-----------------------------------------
-----------------------------------------
| ID | TypeID | Value | EffectivityDate |
-----------------------------------------
| 2 | 1 | 3.4 | 1999-10-31 |
| 4 | 2 | 2.2 | 1999-10-31 |
| 5 | 3 | 6.1 | 1999-10-31 |
-----------------------------------------
Any help?
You can get their maximum EffectiveDate
in a subquery then join it again with its own table,
SELECT a.*
FROM tableName a
INNER JOIN
(
SELECT TypeID, MAX(EffectivityDate) maxDate
FROM tableName
GROUP BY TypeID
) b ON a.TypeID = b.TypeID AND
a.EffectivityDate = b.maxDate
try this:
select *
from your_table t
join
(select TypeID ,max(EffectivityDate ) as EffectivityDate
from your_table
group by TypeID )a
on t.TypeID =a.TypeID
and t.EffectivityDate =a.EffectivityDate
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With