-
Notifications
You must be signed in to change notification settings - Fork 205
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
How to SELECT DISTINCT on a single column? #119
Comments
There isnt currently a way to do this, with the SelectDistinct method but you could probably do something like... // by using the literal you can add what ever you want to the beginning of the select.
ds.Select(goqu.L("DISTINCT on (?)", gateTable.Col("id")), gateTable.Col(goqu.Star())) I havent tried this yet, so it may not work first try but should be close. |
Thanks. I tried several variations of what you suggested, but there's an extra Select(
goqu.L(`DISTINCT on (gates.id) "gates".*`),
goqu.L(arrayQuery).As("tags"),
) |
* [Added] Support for `DISTINCT ON` clauses #119
I added a new You example with the new distinct method would like like // this could be pulled out to a helper
array := func(elements ...interface{}) exp.SQLFunctionExpression {
return goqu.Func("array", elements...)
}
tags := goqu.From(goqu.T("gate_tags")).Where(goqu.Ex{
"gate_tags.gates_id": gateTable.Col("id"),
}).Select("tag")
sql, _, _ := goqu.From("test").
Select(gateTable.All(), array(tags).As("tags")).
Distinct(gateTable.Col("id")).
ToSQL()
fmt.Println(sql) Output: SELECT DISTINCT ON ("gates"."id") "gates".*, array((SELECT "tag" FROM "gate_tags" WHERE ("gate_tags"."gates_id" = "gates"."id"))) AS "tags" FROM "test" |
I'm trying to create the following query in using goqu with the postgres dialect:
I can get pretty close, but I can't figure out how to get the
SELECT DISTINCT on (gates.id)
part to parse properly. This is what I've got so far:How, if possible, would I use goqu to formulate
SELECT DISTINCT on (gates.id)
? I'd be happy to contribute this use case to the otherwise excellent docs.The text was updated successfully, but these errors were encountered: