Issues from a WITH clause (CTE).
I have the following postgreSQL schema:
In the entries the same name can be repeated in many rows. For each name, I just want to keep the rows with the highest
lastUpdatedAt
, keeping other columns along. I can get these values using the following SQL query:
But, I can't rewrite this in drizzle. Any help is greatly appreciated. Thanks!4 Replies
I get the above error message.
Oh, add .as() to the max function.
max(schema.price.lastUpdatedAt).as("lastUpdatedAt")
To select arbitrary SQL values as fields in a CTE and reference them in other CTEs or in the main query, you need to add aliases to them:
If you don’t provide an alias, the field type will become DrizzleTypeError and you won’t be able to reference it in other queries. If you ignore the type error and still try to use the field, you will get a runtime error, since there’s no way to reference that field without an alias.Updated the above query
Thanks! It works!