Postgres 区分大小写
Postgres distinct on with case
我有一个查询,我在 DISTINCT ON
中使用 CASE
语句,如下所示:
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
我尝试在 distinct on 子句中使用像 as _group_key_0
这样的别名,但这又是一个不同的错误。
有没有办法在 distinct on 中使用 CASE 语句并使其工作?
您必须在 ORDER BY
子句的开头添加 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 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,
events.timestamp ASC;
我有一个查询,我在 DISTINCT ON
中使用 CASE
语句,如下所示:
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
我尝试在 distinct on 子句中使用像 as _group_key_0
这样的别名,但这又是一个不同的错误。
有没有办法在 distinct on 中使用 CASE 语句并使其工作?
您必须在 ORDER BY
子句的开头添加 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 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,
events.timestamp ASC;