2

Can anyone please help me figure this out, I have 3 tables: Customer, Products and Products_ordered and I am trying to find customers who have ordered more than 1 product. Here is my query:

SELECT customer_id, product_id
FROM product_ordered
GROUP BY customer_id
HAVING COUNT (customer_id)>1;

I am getting this error: Error report: SQL Error: ORA-00979: not a GROUP BY expression 00979. 00000 - "not a GROUP BY expression"

Thanks for helping

4

3 回答 3

2

尝试

select customer_id, product_id from product_ordered group by customer_id,product_id having count (customer_id)>1;
于 2013-09-26T02:29:18.673 回答
1

尝试:

SELECT customer_id
FROM product_ordered
GROUP BY customer_id
HAVING COUNT (customer_id)>1;

问题是 product_id 不是 group by 的一部分。如果进行分组依据,则只能选择分组依据中的列或使用聚合函数。该查询将返回多次出现的 customer_id。我不知道你的表结构,但如果你想要更多数据,那么只需 id 让我们知道你使用的是什么 sql 版本,SQL Sever、MYSQL 或 Oracle,我可以尝试用窗口函数编写一些东西。

于 2013-09-26T02:41:09.137 回答
0

您真的不想选择订购不止一种产品的客户吗?

多个订单行,或多个产品,或多个独特产品?

如果您作为内联查询运行

(select customer_id from product_ordered group by customer_id having count (customer_id) > 1)

您将看到下达多个订单行的所有客户。但是一个订单可能有多行,或者一行的多个订单,yada yada...

尝试一下select customer_id from product_ordered group by customer_id having count(distinct product_id)>1,您可以真正看到购买了不止一种独特产品的客户。

于 2014-07-31T18:08:37.973 回答