[英]How can I limit my sql select to return only one row for max(date), and not multiple rows?
I'm trying to select only one id, which has the latest date column, but this sql query returns multiple rows. 我正在尝试仅选择一个ID,该ID具有最新的date列,但是此sql查询返回多行。 How can I select only the latest?
如何只选择最新的? MAX() doesn't seem to work
MAX()似乎不起作用
select ra.sourceid as partner1, max(ra.date) from ra_table ra join rap_table rap on parentid=ra.sourceid
where (ra.type = 'Normal' and (ra.name IS NOT NULL or ra.parent IS NOT NULL) and rap.createdate > ra.createdate)
group by ra.sourceid
;
On Oracle 12c or later: 在Oracle 12c或更高版本上:
select ra.sourceid as partner1,
ra.date
from ra_table ra
join rap_table rap
on parentid=ra.sourceid
where (ra.type = 'Normal'
and (ra.name IS NOT NULL or ra.parent IS NOT NULL)
and rap.createdate > ra.createdate)
order by ra.date desc
fetch first row only;
On Oracle prior to 12c: 在12c之前的Oracle上:
select *
from (
select ra.sourceid as partner1,
ra.date
from ra_table ra
join rap_table rap
on parentid=ra.sourceid
where (ra.type = 'Normal'
and (ra.name IS NOT NULL or ra.parent IS NOT NULL)
and rap.createdate > ra.createdate)
order by ra.date desc
)
where rownum = 1
Both queries here will return the id and date for the most recent date value among the results. 这两个查询都将返回结果中最新日期值的ID和日期。 There's no need to aggregate since we're determining what to return with the sort and we only want the one row.
无需汇总,因为我们正在确定要返回的排序内容,而我们只需要一行。
不确定您的情况是否正确,但最后SELECT FIRST 1 ROWS ONLY
”会有所帮助。
You could use MIN( sourceid ) KEEP ( DENSE_RANK FIRST ORDER BY ra.date DESC )
to get the lowest sourceid
for the highest date
value: 您可以使用
MIN( sourceid ) KEEP ( DENSE_RANK FIRST ORDER BY ra.date DESC )
获得date
值最高的最低sourceid
:
SELECT MIN( ra.sourceid ) KEEP ( DENSE_RANK FIRST ORDER BY ra.date DESC ) as partner1,
max(ra.date)
from ra_table ra
join rap_table rap
on parentid=ra.sourceid
where ra.type = 'Normal'
and (ra.name IS NOT NULL or ra.parent IS NOT NULL)
and rap.createdate > ra.createdate;
You can use top 1 in your query with descending order: 您可以按降序使用查询中的前1个:
select Top 1 ra.sourceid as partner1, max(ra.date) from ra_table ra join rap_table rap on parentid=ra.sourceid
where (ra.type = 'Normal' and (ra.name IS NOT NULL or ra.parent IS NOT NULL) and rap.createdate > ra.createdate)
group by ra.sourceid order by max(ra.date) desc
MAX() is a group by function, as such if you ask for more than one column it will give you the max unique combination. MAX()是按功能分组的,因此,如果您要求多个列,它将为您提供最大的唯一组合。 As you're using an ID column in your query you are getting uniques always (defeats the purpose of a max).
在查询中使用ID列时,您总是会获得唯一身份(违背了max的目的)。
You should tweak your query a bit: 您应该对查询进行一些调整:
select TOP 1 ra.sourceid as partner1 FROM ra_table ra
WHERE ra.date = (SELECT max(ra.date)
from
ra_table ra
join rap_table rap on parentid=ra.sourceid
where
(ra.type = 'Normal' and (ra.name IS NOT NULL or ra.parent IS NOT NULL) and rap.createdate > ra.createdate)
group by ra.sourceid)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.