Skip to content

Commit 0335829

Browse files
committed
Disambiguate ORDER BY in balance history DISTINCT ON
The SELECT aliased bucket_timestamp AS timestamp, so the secondary ORDER BY timestamp DESC resolved to the alias (a constant within each bucket) instead of the source row's timestamp. DISTINCT ON then picked an arbitrary hour per day. Rename the underlying column to source_timestamp in the CTE so the ORDER BY references it unambiguously and end-of-day is selected.
1 parent a836bcb commit 0335829

1 file changed

Lines changed: 2 additions & 2 deletions

File tree

api/v1_users_balance_history.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,15 +118,15 @@ func (app *ApiServer) v1UsersBalanceHistory(c *fiber.Ctx) error {
118118
WHEN @granularity::text = 'daily' THEN date_trunc('day', timestamp)
119119
ELSE timestamp
120120
END AS bucket_timestamp,
121-
timestamp,
121+
timestamp AS source_timestamp,
122122
balance_usd
123123
FROM hourly_totals
124124
)
125125
SELECT DISTINCT ON (bucket_timestamp)
126126
bucket_timestamp AS timestamp,
127127
balance_usd
128128
FROM bucketed
129-
ORDER BY bucket_timestamp ASC, timestamp DESC
129+
ORDER BY bucket_timestamp ASC, source_timestamp DESC
130130
`
131131

132132
rows, err := app.pool.Query(c.Context(), sql, pgx.NamedArgs{

0 commit comments

Comments
 (0)