0

I am using Scala 2.12 and have required libraries downloaded via build.sbt.

I have my DB output in below format. enter image description here

Basically, it is like per valuation date and book, there can be multiple currency data.

I have group by on book (majorly), which will have list of Pnl data based on currency.

Just the rough representation:

{ Bookid: 1234,
  BookName: EQUITY,
  PnlBreakdown: [currency: cad, actual_pnl_local: 100, actual_pnl_cde: 100], [currency: usd, actual_pnl_local: 100, actual_pnl_cde: 130]
}

Basically. Key will be book and value will be list of pnl data.

I have a case class defined as below:

case class PnlData(valuation_date: Option[String], currency: Option[String],pnl_status: Option[String],actual_pnl_local: Option[String] ,actual_pnl_cde: Option[String], actual_pnl_local_me_adj: Option[String] ,actual_pnl_cde_me_adj: Option[String] ) {
  override def toString():String= {
    s"valuation_date=$valuation_date,currency=$currency,pnl_status=$pnl_status,actual_pnl_local=$actual_pnl_local,actual_pnl_cde=$actual_pnl_cde,actual_pnl_local_me_adj=$actual_pnl_local_me_adj,actual_pnl_cde_me_adj=$actual_pnl_cde_me_adj"
  }
}

case class BookLevelDaily(book_id: Option[String], book: Option[String], pnlBreakdown: List[SaPnlData]){
  override def toString():String= {
    s"book_id=$book_id,book=$book,pnl=$pnlBreakdown"
  }
}

Basically, my final object is of type BookLevelDaily.

How do I translate the DB output (above) to my BookLevelDaily object?

I can convert the entire result to the list, but further how should I do groupBy?

val list: List[BookLevelDaily] =
      sql"""
           |SELECT QUERY TO GET ABOVE RESULTSET
        """.stripMargin.map(rs =>
        BookLevelDaily(
          valuation_date = rs.stringOpt("valuation_date"),
          book_id = rs.stringOpt("book_id"),
          book =  rs.stringOpt("book"),
          currency=  rs.stringOpt("currency"),
          pnl_status=  rs.stringOpt("pnl_status"),
          actual_pnl_local=  rs.stringOpt("actual_pnl_local"),
          actual_pnl_cde=  rs.stringOpt("actual_pnl_cde"),
          actual_pnl_local_me_adj= rs.stringOpt("actual_pnl_local_me_adj"),
          actual_pnl_cde_me_adj= rs.stringOpt("actual_pnl_cde_me_adj")
        )
      ).list().apply()

Firstly above is not of type BookLevelDaily. So how to iterate or group by to separate Pnl level data and map it to key (book).

4

1 回答 1

0

如果我理解正确的话,它似乎是一对多的关系(一:book_level_daily,多:pnl_breakdown)。如果是这样,请检查以下文档。

http://scalikejdbc.org/documentation/one-to-x.html

于 2019-03-09T00:36:20.747 回答