Skip to content

Instantly share code, notes, and snippets.

@cheptsov
Last active December 19, 2015 18:29
Show Gist options
  • Save cheptsov/5999327 to your computer and use it in GitHub Desktop.
Save cheptsov/5999327 to your computer and use it in GitHub Desktop.
Just added function and group by support to the Expose SQL library for Kotlin
select (Cities.name, сount(Users.id)) from Cities join Users groupBy Cities.name forEach {
val (cityName, userCount) = it
if (userCount > 0) {
println("$userCount user(s) live(s) in $cityName")
} else {
println("Nobody lives in $cityName")
}
}
@cheptsov
Copy link
Author

Produces:

SQL: SELECT Cities.name, COUNT(Users.id) FROM Cities LEFT JOIN Users ON Cities.id = Users.city_id GROUP BY Cities.name

Nobody lives in Prague
1 user(s) live(s) in St. Petersburg
2 user(s) live(s) in Munich

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment