Slick Join в результате запроса - PullRequest
0 голосов
/ 08 октября 2019

Я выполняю первую задачу со следующего веб-сайта 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's 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.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...