Sql 从最大日期提取数据
我有如下记录Sql 从最大日期提取数据,sql,oracle,plsql,Sql,Oracle,Plsql,我有如下记录 --------------------------------------------------------------------- | AcnttNo | Date1 | Balance1 | Date2 | balance3 | date4 | balance4 | |-------------------------------------------------------------------- | 123 | 50282 | 3456 | 45
---------------------------------------------------------------------
| AcnttNo | Date1 | Balance1 | Date2 | balance3 | date4 | balance4 |
|--------------------------------------------------------------------
| 123 | 50282 | 3456 | 45465 | 56557 | 4556 | 324235 |
| 123 | 56757 | 23434 | 234235 | 344324 | 56476 | 5676 |
| 123 | 435 | 2434 | 2343 | 234545 | 24245 | 2423424 |
---------------------------------------------------------------------
例如:
对于每个AcnttNo
将有多行余额和日期数据。我需要得到最大日期的余额
我正在使用PL/SQL developer和oracle数据库我不确定为什么您的表中有多个日期/余额,但是,下面的内容应该会让您了解一些有趣的内容,您可以处理这些内容
SELECT *
FROM YourTable T
WHERE NOT EXISTS (
SELECT *
FROM YourTable T2
WHERE T2.AcntNo = T.AcntNo
AND T2.Date1 > T.Date1
)
如果您想要具有最长日期的行:
select
*
from
YourTable y
where
greatest(y.date1, y.date2, y.date3) =
(select max(greatest(yx.date1, yx.date2, yx.date3))
from
YourTable yx)
如果您确实需要与该行最大日期匹配的余额:
select
greatest(y.date1, y.date2, y.date3) as GreatestDate,
case greatest(y.date1, y.date2, y.date3)
when y.Date1 then
y.balance1
when y.date2 then
y.balance2
when y.date3 then
y.balance3
end as GreatestDateBalance
from
YourTable y
where
greatest(y.date1, y.date2, y.date3) =
(select max(greatest(yx.date1, yx.date2, yx.date3))
from
YourTable yx)
但我认为你真正需要的是重新考虑你的桌子设计