简体   繁体   English

如何在子查询中使用LIMIT 1

[英]How can I use LIMIT 1 in subquery

I'm trying to run an update querywith a table that joins on itself and I want to assign the column M_USER to the first row that the select statement is returning. 我正在尝试使用一个自身连接的表来运行更新查询,我想将M_USER列分配给select语句返回的第一行。 I know in SQL Server, I could use TOP 1 and i think the equivalent would be LIMIT 1 in Vertica. 我知道在SQL Server中,我可以使用TOP 1而我认为在Vertica中等效为LIMIT 1

So I tried to write the query with the LIMIT 1 but i'm getting this error: 因此,我尝试使用LIMIT 1来编写查询,但出现此错误:

ERROR: Correlated subquery expression without aggregates and with limit is not supported

Here's my query: 这是我的查询:

UPDATE REPORT.sub_2019 a
SET M_USER= (Select u.UPDATED_USER 
        from REPORT.sub_2019 u 
        where u.MBR_ID = a.MBR_ID 
        and u.NAME= a.NAME and u.STATUS_REASON = 'Pending' limit 1)
where a.RESULT is not null

I just want to grab the first UPDATED_USER that the subquery is returning. 我只想获取子查询返回的第一个UPDATED_USER Should I use LIMIT or any other way of writing the query? 我应该使用LIMIT还是任何其他方式编写查询?

I think you could use the rank function for this: 我认为您可以为此使用等级函数:

UPDATE REPORT.sub_2019 a
SET M_USER= (Select u.UPDATED_USER, rank() OVER (PARTITION BY <field> ORDER BY <field> DESC) as rank 
        from REPORT.sub_2019 u 
        where u.MBR_ID = a.MBR_ID 
        and u.NAME= a.NAME 
        and u.STATUS_REASON = 'Pending'
        and rank = 1)
where a.RESULT is not null

Use max or min in subquery 在子查询中使用max或min

UPDATE REPORT.sub_2019 a
SET M_USER= (Select min(u.UPDATED_USER )
        from REPORT.sub_2019 u 
        where u.MBR_ID = a.MBR_ID 
        and u.NAME= a.NAME and u.STATUS_REASON = 'Pending')
where a.RESULT is not null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM