我试图将数据插入到表中,但在使用COUNT函数时遇到了问题。count_rentals和count_returns列应该表示租用和返回给定电影的次数。
我试图将数据插入到表中的dataset (sakila)没有租金和返回数,相反,它有一个return_date和rental_date列。我认为,使用这两列的计数将给出电影被租用和返回的次数(因为当电影被租用时,日期将出现在租赁日期中,而当它被返回时,日期将出现在返回日期),因此我编写了以下文章:
INSERT INTO sakila_snowflake.fact_rental (
rental_id,
rental_last_update,
customer_key,
staff_key,
film_key,
store_key,
rental_date_key,
return_date_key,
count_returns,
count_rentals,
rental_duration,
dollar_amount)
(SELECT
s_rental.rental_id,
s_rental.last_update,
s_customer.customer_id,
s_staff.staff_id,
s_film.film_id,
s_store.store_id,
s_rental.rental_date,
s_rental.return_date,
Count(s_rental.rental_date),
Count(s_rental.return_date),
s_rental.return_date - s_rental.rental_date,
(s_rental.return_date - s_rental.rental_date)*s_film.rental_rate
FROM
sakila.rental as s_rental,
sakila.customer as s_customer,
sakila.staff as s_staff,
sakila.film as s_film,
sakila.store as s_store
WHERE
s_rental.staff_id = s_staff.staff_id AND s_staff.store_id=s_store.store_id AND s_store.store_id = s_customer.store_id);但是,一旦我尝试运行它,我就会得到以下错误:
错误代码: 1140。在不带GROUP的聚合查询中,SELECT list的表达式#1包含非聚合列‘sakila.s_rental.rent_id’;这与sql_mode=only_full_group_by不兼容。
我不太清楚如何在不使用COUNT函数的情况下获得count_rental和count_return列租用和返回电影的次数。如果有人有任何建议,我将非常感激:)
发布于 2021-11-29 00:32:19
如果您想要计数,但不想按组分组,则需要执行窗口功能。如果你对电影中的计数感兴趣的话
count(s_rental.rental_date) over (partition by s_film.film.id)https://dev.mysql.com/doc/refman/8.0/en/window-functions-usage.html
https://stackoverflow.com/questions/70148507
复制相似问题