gpt4 book ai didi

mysql - 如何修复 'You tried to execute a query that does not include X as an aggregate function'

转载 作者:行者123 更新时间:2023-11-29 16:01:15 26 4
gpt4 key购买 nike

我的数据库中有一个表“客户”,其中包含以下内容:

CustomerID | SupplierID

每个客户都会与供应商一起插入数据库,这意味着特定客户可以多次列出。

我有一个特定客户,其 CustomerID=X

我正在尝试用 SQL 编写一个查询,返回满足以下等式的客户表:用户与 X 之间的共同供应商数量 > 客户 X 的供应商数量的 80%。

我成功获得了一张表格:

CustomerID1 | CustomerID2 | Num of Mutual Suppliers

其中所有行中的 CustomerID1=X ,但我无法包含客户 X 拥有的供应商数量,以便仅选择满足条件的供应商。

select user1
, user2
, num_in_common
, COUNT(*)
from (select f1.CustomerID as user1, f2.CustomerID as user2, count(*) as num_in_common
from Customers f1 inner join
Customers f2
on f1.SupplierID = f2.SupplierID
group by f1.CustomerID, f2.CustomerID)
where user1 = X;

我期望收到如下表格:

CustomerID1 | CustomerID2 | Num of Mutual Suppliers | Num of X's Suppliers

但我收到错误“您尝试执行的查询不包含指定表达式“user1”作为聚合函数的一部分”。

最佳答案

我认为之前的答案不正确,所以既然OP已经澄清了这个问题,我只是提供一个更准确的答案。

select x.customerid, c.customerid,
(count(*) / num_x) as ratio
from customers c cross join
(select @X as customerid, count(*) as num_x
from customers c
where c.customerid = @X
) x
where exists (select 1
from customers cx
where cx.supplierid = c.supplierid and
cx.customerid = @X
)
group by c.customerid, x.num_x
having count(*) / x.num_x >= 0.8
order by ratio desc;

Here是代码语法正确的演示(没有数据)。

Here是一个适用于模拟数据的演示。

关于mysql - 如何修复 'You tried to execute a query that does not include X as an aggregate function',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56193414/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com