Calculating aggregations of multiple columns without groupBy

15 views
Skip to first unread message

Olaf Tomczak

unread,
May 14, 2018, 3:47:09 AM5/14/18
to Slick / ScalaQuery
Hello,

I'm trying to perform a query like this using slick (3.x):

select sum(sar_column07), sum(sar_column08), sum(sar_column09), sum(sar_column10) from simple_accounting_record;

All I could think about was something like:

simpleAccountingRecordsTable.map { record ⇒
(
record.column07,
record.column08,
record.column09,
record.column10
)
}.groupBy(_ ⇒ 1).map {
case (_, q) ⇒ (
q.map(_._1).sum.getOrElse(Decimal.Zero),
q.map(_._2).sum.getOrElse(Decimal.Zero),
q.map(_._3).sum.getOrElse(Decimal.Zero),
q.map(_._4).sum.getOrElse(Decimal.Zero)
)
}

Is there a cleaner way to acheive this?

Thanks,
Olaf


Richard Dallaway

unread,
May 14, 2018, 10:33:15 AM5/14/18
to scala...@googlegroups.com
It’s the only way I know. Would be good to hear if anyone else has a better way.

There’s an example on http://books.underscore.io/essential-slick/essential-slick-3.html#grouping if you scroll down a bit to the blue box called “Group by True”.

Regards
Richard

--

---
You received this message because you are subscribed to the Google Groups "Slick / ScalaQuery" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scalaquery+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/scalaquery/1b588e23-1265-40d0-8f99-2b3b92ef99fc%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Olaf Tomczak

unread,
May 14, 2018, 10:56:45 AM5/14/18
to Slick / ScalaQuery
Ok, thanks Richard
Reply all
Reply to author
Forward
0 new messages