SQL 如何 return 最新日期
SQL how to return the latest date
在下面的代码中,我得到了 col0 的多行。
col0 不是唯一的,例如 - col0 可能有四行值为 5,两行值为 7,等等。
我想 return col0 中的每个值仅占一行。
假设 numToDate 是一个不言自明的 UDF(对 ISO),我想 return col0 中的每个值一行,其中 col3 是最近的日期。
SELECT col0, col1, numToDate(col2), numToDate(col3)
FROM myTable
where col1 = 'someValue';
您可以使用 row_number()
:
select col0, col1, numToDate(col2), numToDate(col3)
from (select t.*,
row_number() over (partition by col0 order by numToDate(col3) desc) as seqnum
from mytable t
) t
where seqnum = 1;
在下面的代码中,我得到了 col0 的多行。
col0 不是唯一的,例如 - col0 可能有四行值为 5,两行值为 7,等等。
我想 return col0 中的每个值仅占一行。 假设 numToDate 是一个不言自明的 UDF(对 ISO),我想 return col0 中的每个值一行,其中 col3 是最近的日期。
SELECT col0, col1, numToDate(col2), numToDate(col3)
FROM myTable
where col1 = 'someValue';
您可以使用 row_number()
:
select col0, col1, numToDate(col2), numToDate(col3)
from (select t.*,
row_number() over (partition by col0 order by numToDate(col3) desc) as seqnum
from mytable t
) t
where seqnum = 1;