我正在以下网站https://www.freecodecamp.org/news/project-1-analyzing-dvd-rentals-with-sql-fd12dd674a64/上进行第一个问题的研究,以确定“什么是最热门和租金最低(需求最多)的类型,它们的总销售额是多少?”给出了所需的SQL下面。
with q1 as (select c.name as genre, sum(p.amount) as total_spent from
film f
join film_category fc
using (film_id)
join category c
using (category_id)
join inventory i
using (film_id)
join rental r
using (inventory_id)
join payment p
using (rental_id)
group by c.name
order by total_spent desc),
q2 as (select c.name as genre, count(cu.customer_id) as number_of_rentals from
film f
join film_category fc
using (film_id)
join category c
using (category_id)
join inventory i
using (film_id)
join rental r
using (inventory_id)
join customer cu
using (customer_id)
group by c.name
order by number_of_rentals desc)
select genre, number_of_rentals, total_spent
from q1 join q2
using (genre)
order by number_of_rentals desc;
现在我要复制的是Scala的Slick(3.1)ORM。该过程涉及进行两个查询q1和q2,然后将它们连接在一起以获得第三个查询。下面给出前两个查询
val q1 = (for {
(((((film, filmCategory), category), inventory), rental), payment) <- Film join
FilmCategory on (_.filmId === _.filmId) join
Category on (_._2.categoryId === _.categoryId) join
Inventory on (_._1._1.filmId === _.filmId) join
Rental on (_._2.inventoryId === _.inventoryId) join
Payment on (_._2.rentalId === _.rentalId)
} yield (category.name, payment.amount)) groupBy (_._1) map {
case (name, ids) =>
name -> ids.map(_._2).sum
} sortBy (_._2 desc)
val q2 = (for {
(((((film, filmCategory), category), inventory), rental), customer) <- Film join
FilmCategory on (_.filmId === _.filmId]) join
Category on (_._2.categoryId === _.categoryId) join
Inventory on (_._1._1.filmId === _.filmId) join
Rental on (_._2.inventoryId === _.inventoryId) join
Customer on (_._2 === _.customerId)
} yield (category.name, customer.customerId)) groupBy (_._1) map {
case (name, ids) =>
name -> ids.map(_._2).length
} sortBy (_._2 desc)
最后的连接由
给出val result = for {
((genre, count), price) <- q1 join q2 on (_._1 === _._1)
} yield (genre, count, price)
即将这两个表联接在category.name列上。但是,尽管这不会产生编译错误,但会给出
的运行时错误。 org.postgresql.util.PSQLException: ERROR: column x2.x20 does not exist
我可以解决此问题的唯一方法是用
替换联接val result = for {
(genre1, count) <- q1
(genre2, price) <- q2 if genre2 === genre1
} yield (genre1, count, price)
问题:第一次加入有什么问题?为什么我需要在后者中用双重理解代替它?在Slick Select查询返回的结果上进行联接的正确方法是什么。