【发布时间】:2021-11-21 03:22:54
【问题描述】:
是的,这是一项任务。所以任务是输出两列'first name'和'last name'的条件:
-A u (B ∩ -C ∩ -(A ∩ -( B u D)))
答:所有周一和周五没有购物的消费者 (time_by_day.the_day)
B:所有购买“非消耗品”的消费者 (product_class.product_family)
C:购买超过 10 件商品的所有消费者 (sales_fact_1997.unit_sales) 一次 (sales_fact_1997.time_id)
D:来自加拿大的女性消费者(consumer.gender、consumer.country)
这是我目前得到的
SELECT
c.fname,
c.lname
FROM
customer AS c
INNER JOIN sales_fact_1997 AS s ON c.customer_id = s.customer_id
INNER JOIN time_by_day AS t ON s.time_id = t.time_id
INNER JOIN product AS p ON s.product_id = p.product_id
INNER JOIN product_class AS pc ON p.product_class_id = pc.product_class_id
Where
NOT t.the_day in ('Monday', 'Friday') OR
(
pc.product_family = 'Non-Consumable' AND
NOT SUM(s.unit_sales) > 10 AND
NOT (
t.the_day in ('Monday', 'Friday') AND
NOT (
pc.product_family = 'Non-Consumable' OR
(c.country = 'Canada' AND c.gender = 'F')
)
)
)
GROUP BY concat(c.customer_id, s.time_id)
结果出错了
#1111 - Invalid use of group function
但我不知道代码的哪一部分是错误的。我很确定这可能是 WHERE 部分。但我不知道我做错了什么。
条件 C 是我真正苦苦挣扎的地方。我可以很好地查询 C
SELECT
t.time_id,
c.customer_id,
c.fullname,
round(SUM(s.unit_sales),0) as tot
FROM
customer as c
INNER JOIN sales_fact_1997 as s ON c.customer_id = s.customer_id
INNER JOIN time_by_day as t on s.time_id=t.time_id
GROUP BY concat(c.customer_id, s.time_id)
ORDER BY c.customer_id, t.time_id
但是尝试将其合并到主代码中对我来说很难。
在线阅读我假设我应该使用 HAVING 而不是 WHERE。
如果有人能指出我正确的方向,我将不胜感激。
This是我使用的数据库。
【问题讨论】:
-
你能提供表格描述,一些插入数据和预期结果吗?
Where NOT t.the_day in应该是where t.the_day not in或者你应该使用not exists -
@ErgestBasha This 是数据库。
-
所有非聚合列都应该是 group by 的一部分。
标签: mysql sql group-by where-clause having