我想提取所有关联帐户年收入最高的联系人,我使用下面的 SOQL 查询
select name, account.AnnualRevenue from contact where account.AnnualRevenue=(select MAX(account.AnnualRevenue) from contact)
我也在下面使用了SOQL查询
select name, account.AnnualRevenue from contact where account.AnnualRevenue=(select MAX(AnnualRevenue) from Account)
在这两个查询中我都得到“未知错误解析查询”错误 请帮助我如何实现这一目标。提前致谢
您的第一个查询:
select name,
account.AnnualRevenue --> you are selecting column from ACCOUNT ...
from contact --> ... but - there's no ACCOUNT table in FROM clause
where account.AnnualRevenue =
(select MAX(account.AnnualRevenue) --> you are selecting column from ACCOUNT ...
from contact --> ... but - there's no ACCOUNT in FROM clause
);
类似的错误会导致您的第二个查询出现问题。
如果你把表格的描述贴出来会更容易协助;没有这些信息,我们只能猜测哪个列属于哪个表。我的猜测:
SQL> with
2 account (account_id, contact_id, annualrevenue) as
3 (select 100, 1, 100 from dual union all
4 select 101, 2, 200 from dual
5 ),
6 contact (contact_id, name) as
7 (select 1, 'Little' from dual union all
8 select 2, 'Foot' from dual
9 ),
基于这样的样本数据,account
表中的前
sort行按
annualrevenue
降序排列(这意味着最高收入在顶部):
10 --
11 temp as
12 (select account_id, contact_id, annualrevenue,
13 rank() over (order by annualrevenue desc) rnk
14 from account
15 )
最后,加入
contact
和temp
并获取排名最高的行:
16 select c.name, t.annualrevenue
17 from contact c join temp t on t.contact_id = c.contact_id
18 where t.rnk = 1;
NAME ANNUALREVENUE
------ -------------
Foot 200
SQL>
即使您的表看起来不像那样,现在您有一个可以帮助您编写自己的查询的工作示例。