我正在尝试使用一个自身连接的表来运行更新查询,我想将M_USER
列分配给select语句返回的第一行。我知道在SQL Server中,我可以使用TOP 1
,而我认为在Vertica中等效为LIMIT 1
。
所以我尝试用LIMIT 1
编写查询,但出现此错误:
ERROR: Correlated subquery expression without aggregates and with limit is not supported
这是我的查询:
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
我只想获取子查询返回的第一个UPDATED_USER
。我应该使用LIMIT还是任何其他方式编写查询?
答案 0 :(得分:0)
我认为您可以为此使用等级功能:
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
答案 1 :(得分:0)
在子查询中使用最大值或最小值
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