pg
pg copied to clipboard
UNION ALL and WITH
I think I found something undocumented in the Wiki pages and issues.
I need this query:
WITH cte AS (
SELECT
"player"."id",
"player"."created_at",
"player"."note"
FROM
"players" AS "player"
ORDER BY
"amount" DESC,
"id"
LIMIT 5
) ( SELECT * FROM cte ) UNION ALL
(
SELECT
"id",
"created_at",
CONCAT_WS ( '|', LAST_VALUE ( "created_at" ) OVER ( ), LAST_VALUE ( "id" ) OVER ( ) ) AS note
FROM
cte -- THIS IS CORRECT
LIMIT 1
)
and instead go-pg
keeps giving me this:
WITH "cte" AS (
SELECT
"player"."id",
"player"."created_at",
"player"."note"
FROM
"players" AS "player"
ORDER BY
"amount" DESC,
"id"
LIMIT 5
) ( SELECT * FROM "cte" ) UNION ALL
(
SELECT
"id",
"created_at",
CONCAT_WS ( '|', LAST_VALUE ( "created_at" ) OVER ( ), LAST_VALUE ( "id" ) OVER ( ) ) AS note
FROM
"players" AS "player" -- THIS IS THE PROBLEM, I need "cte" here
LIMIT 1
)
I'm using this code:
var players []*models.Player
queryAll := r.db.Model(&players).Limit(5)
queryNotes := r.db.Model((*models.Player)(nil)).ExcludeColumn("note").Limit(1)
queryNotes.ColumnExpr("CONCAT_WS ( '|', " + lastValues + " ) AS note")
err := queryAll.WrapWith("cte").Table("cte").UnionAll(queryNotes).Select(&players)
What can I do to fix this?
I can update Wiki for UNION ALL
and this case with WITH
if you want