How to do sql queries with intersect with Slick?

63 Views Asked by At

I am trying to build sql queries with slick where I need to intersect the results. I can build it with union (from Slick docs):

val q1 = coffees.filter(_.price < 8.0)
val q2 = coffees.filter(_.price > 9.0)

val unionQuery = q1 union q2
// compiles to SQL (simplified):
//   select x8."COF_NAME", x8."SUP_ID", x8."PRICE", x8."SALES", x8."TOTAL"
//     from "COFFEES" x8
//     where x8."PRICE" < 8.0
//   union select x9."COF_NAME", x9."SUP_ID", x9."PRICE", x9."SALES", x9."TOTAL"
//     from "COFFEES" x9
//     where x9."PRICE" > 9.0

I need to do something similar to this:

val q1 = coffees.filter(_.price < 8.0)
val q2 = coffees.filter(_.price > 9.0)

val intersectQuery = q1 intersect q2
// compiles to SQL (simplified):
//   select x8."COF_NAME", x8."SUP_ID", x8."PRICE", x8."SALES", x8."TOTAL"
//     from "COFFEES" x8
//     where x8."PRICE" < 8.0
//   intersect select x9."COF_NAME", x9."SUP_ID", x9."PRICE", x9."SALES", x9."TOTAL"
//     from "COFFEES" x9
//     where x9."PRICE" > 9.0

I've read here that there was a PR meant to do that but it's from 10 years ago and I can't see that code in the current source code. I am using Scala 2.13.

I've tried:

  • Looking for the method in the Query object.
  • Going through the official docs
  • Look for creative methods or what other people do in this situations
0

There are 0 best solutions below