1

Still trying to get familiar with scalikejdbc. What is the simplest way to just use sql syntax to send a query using scalike jdbc into a table to get max date? Something really simple like the below works fine but gives me an error when I try to add max around the column.

  val maxDate: Option[String] = DB readOnly { implicit session =>
    sql"select <column> from <table>"
      .map(rs => rs.string("<column")).first.apply()
  }

this does not work:

val maxDate: Option[String] = DB readOnly { implicit session =>
    sql"select max(<column>) from <table>"
      .map(rs => rs.string("<column")).first.apply()
  }

error: Failed to retrieve value because The column name not found.. If you're using SQLInterpolation,...

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
horatio1701d
  • 8,809
  • 14
  • 48
  • 77
  • Expressions like `max(...)` either don't have a column name or alias, or get one derived from the expression itself, your assumption that it would somehow inherit the name of the column used inside the expression, is - at least for most DBMS I know - wrong. – Mark Rotteveel Jan 27 '18 at 14:27

1 Answers1

0

I expect this happens because column max(MyColumn) does not have name "MyColumn" by default. You may try something like this instead

  val maxDate: Option[String] = DB readOnly { implicit session =>
    sql"select max(MyColumn) as MyColumn_max from MyTable"
      .map(rs => rs.string("MyColumn_max")).first.apply()
  }
SergGr
  • 23,570
  • 2
  • 30
  • 51