我有一个查询,我在CASE
里面使用这样的语句DISTINCT ON
:
SELECT
DISTINCT ON(COALESCE(users.cuid,events.uid),
CASE WHEN events.props->>'prop' IS NULL THEN '$none' WHEN events.props->>'prop' = '' THEN '$none' ELSE events.props->>'prop' END)
COALESCE(users.cuid, events.uid) as coal_user_id,
CASE WHEN events.props->>'prop' IS NULL THEN '$none' WHEN events.props->>'prop' = '' THEN '$none' ELSE events.props->>'prop' END AS _group_key_0, events.uid as event_uid
FROM events JOIN users ON events.uid=users.id
WHERE events.project_id='<>' AND timestamp>='<>' AND timestamp<='<>'
ORDER BY coal_user_id, _group_key_0, events.timestamp ASC
运行时,我收到此错误:
SELECT DISTINCT ON expressions must match initial ORDER BY expressions
我尝试as _group_key_0
在 distinct on 子句中使用别名,但这又是抛出一个不同的错误。
有没有办法以不同的方式使用 CASE 语句并使其工作?