How to use simple math in Slick Queries

I have this query that I want to convert to slick:

SELECT (date_part('epoch', SUM(end_time - start_time))*1000)::bigint FROM v2_game

I could live with an act happening in scala and not in a database, but I don’t understand how to do the subtraction. end_time and start_time are both dates represented as timestamps in the database. So far I have this:

val datePart = SimpleFunction.binary[String, Date, Double]("date_part")
  val q = for {
    g <- Games
  } yield datePart("epoch", g.startTime)

So how to subtract / add values? Google doesn't return any results in my searches, either it's really simple, or nobody wants to use a little math in the queries.

I am using Slick 1.0.1 with postgres sql

+4
source share
3 answers

You can look into the function agein postgres. It calculates the interval between dates.

http://www.postgresql.org/docs/9.3/static/functions-datetime.html

+1

slick-pg. , postgres, . DateTime, ,

val q = for {
  g <- Games
} yield (g.endTime - g.startTime).part("epoch")

SELECT date_part('epoch', endTime - startTime) FROM games
+1

, : http://slick.typesafe.com/doc/1.0.1/api/index.html#scala.slick.lifted.NumericColumnExtensionMethods

, , , , . , , 1esha, :

  val datePartLong = SimpleFunction.binary[String, Date, Long]("date_part")
  val age = SimpleFunction.binary[Date, Date, Date]("age")

  val q = Query((for {
    g <- Games
  } yield datePartLong("epoch", age(g.endTime, g.startTime)) * 1000L).sum)

A little caveat is that it calls the date_part method on each line and then the sum, but I cannot find a way to do it the other way around, it just doesn't work by type. According to the explanation, the cost of a query is 164 for a smooth query and 139 for pure sql. I think this is normal, no need to do premature optimization. :)

0
source

All Articles