-
Notifications
You must be signed in to change notification settings - Fork 703
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding distinct on to PostgreSQL #500
Comments
How |
@Tapac |
AFAIU this a per column function, so you can try : class DistinctOn<T>(val expr: Column<T>) : Function<T>(expr.columnType) {
override fun toSQL(queryBuilder: QueryBuilder) = "DISTINCT ON (${expr.toSQL(queryBuilder)}) ${expr.toSQL(queryBuilder)}"
} |
I would also love to get DISTINCT ON 👍 @Tapac thanks for the input! I'm not experienced enough with Exposed to make this work from your example however. I'm unsure about the return type you've used. If you have the time I would appreciate an example? |
@mellson here's an example: Suppose you have these 2 tables:
You can use what @Tapac has above with a little bit of modification:
and then now suppose you want to get all sessions but distinct by user you can have:
|
Thank you @paulmuriithi - That's very kind of you 🙏 |
@mellson thanks a lot for your idea. Unfortunately I could not make it work with exposed 17.x . Maybe you can help me with it :)
The query being generated results in a syntax error. ERROR: syntax error at or near "," at character 35
this would be the right syntax:
It looks like "slice" concats all expressions with and "," . But we need to have SELECT DISTINCT ON (...) col1, col2 FROM table @Tapac , do you have an idea how solve this?
|
@bastman You can solve it by appending a class DistinctOn<T>(private val expr: Column<T>) : Function<T>(expr.columnType) {
override fun toQueryBuilder(queryBuilder: QueryBuilder) = queryBuilder {
append("DISTINCT ON (", expr, ") TRUE")
}
} Sure that will result in your database selecting a |
@KennethWussmann thank you so much. Appending " TRUE" does the trick ;) What do you think of this solution ? Works for me ...
Usage example:
|
see: JetBrains/Exposed#500 thanks to: Kenneth Wussmann (https://github.com/KennethWussmann)
We have been using the workaround outline here but we get a failure down the line when we use the |
Also upgrading to the latest 0.28.1 breaks this workaround. We get the following exception
|
Here's the fun Column<*>.distinctOn(vararg extraColumns: Column<*>) = DistinctOn(this, extraColumns)
class DistinctOn<T>(expr: Column<T>, columns: Array<out Column<*>>) : Function<T>(expr.columnType) {
private val distinctNames = listOf(expr, *columns)
.joinToString(
separator = ", ",
transform = {
"${it.table.tableName}.${it.name}"
}
)
private val colName = expr.table.tableName + "." + expr.name
override fun toQueryBuilder(queryBuilder: QueryBuilder) {
queryBuilder {
append(" DISTINCT ON ($distinctNames) $colName ")
}
}
} |
How do I add
DISTINCT ON (columns)
to SELECT clause?I want to make this query:
SELECT DISTINCT ON (testdummies.id) testdummies.id, testdummies.integer_array, testdummies.name FROM testdummies LEFT JOIN testdummies t0 ON (t0.id = ANY(testdummies.test_dummies) AND t0.name IN ('test2', 'test3') ) WHERE ((testdummies.private = false) OR (testdummies.private IS NULL)) AND t0.name IN ('test2', 'test3') LIMIT 10
I have looked at the prepareSQL function :
https://github.com/JetBrains/Exposed/blob/master/src/main/kotlin/org/jetbrains/exposed/sql/Query.kt#L159-L208
It doesn't look like I can supply a list of distinct columns. Is there another method that can do this?
The text was updated successfully, but these errors were encountered: