带子查询的redshift sql查询中的语法错误
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了带子查询的redshift sql查询中的语法错误相关的知识,希望对你有一定的参考价值。
我通常在SQL中是一个相当新的人,以前从未处理过redshift。我正在尝试进行一个查询,该查询在postgresql中非常有效。但是我在redshift中遇到语法错误。查询是:
SELECT
test.table_1.user_id as user_id,
test.table_1.timestamp as start_session,
test.table_1.step_3 :: timestamp + interval '1 hour' as end_session,
test.table_1.step_3 :: timestamp + interval '1 hour' - test.table_1.timestamp :: timestamp as session_duration
FROM (SELECT *,
min(case when page = 'second_page' then timestamp end) OVER (partition by user_id order by timestamp desc rows between unbounded preceding and unbounded following) as step_2,
min(case when page = 'third_page' then timestamp end) OVER (partition by user_id order by timestamp desc rows between unbounded preceding and unbounded following) as step_3
FROM test.table_1) test.table_1
WHERE
test.table_1.page = 'first_page' AND
step_2 > test.table_1.timestamp AND
step_3 > step_2 AND
step_3 :: timestamp - step_2 :: timestamp < '1 hour' AND
step_2 :: timestamp - test.table_1.timestamp :: timestamp < '1 hour'
ORDER BY
user_id,start_session
Error running query: syntax error at or near "." LINE 11: FROM test.vimbox_pages) test.vimbox_pages ^
行中的错误是FROM test.table_1) test.table_1
我不明白那里出了什么问题。通过此查询,我试图以某种顺序获取页面阅读过程中用户操作的会话列表。感谢您的帮助!
答案
别名是标识符,需要遵循标识符规则。您还可以通过其他方式简化查询:
SELECT t.user_id, t.timestamp as start_session,
(t.step_3::timestamp + interval '1 hour' as end_session),
(t.step_3::timestamp + interval '1 hour' - t.timestamp::timestamp) as session_duration
FROM (SELECT t.*,
MIN(CASE WHEN page = 'second_page' THEN timestamp END) OVER (PARTITION BY user_id) as step_2,
MIN(CASE WHEN page = 'third_page' THEN timestamp END) OVER (partition by user_id) as step_3
FROM test.table_1 t
) t
WHERE t.page = 'first_page' AND
step_2 > t.timestamp AND
step_3 > step_2 AND
step_3::timestamp < step_2::timestamp + interval '1 hour' AND
step_2::timestamp < timestamp + interval '1 hour'
ORDER BY user_id, start_session;
注意:
- 您的窗口条款不必要地复杂。如果要整个窗口范围,则不需要
ORDER BY
。 - 给定列名,到
timestamp
的转换应该是不必要的。但是我留给他们了。 t.user_id as user_id
是多余的。列名将始终为user_id
。- 我从没看到
::
周围的空格。当然可以允许它们,但是类型转换的优先级很高,并且通常不带空格。 - 我更喜欢时间戳比较而不是时间戳,而不是转换为间隔。奇怪的事情有时会发生。
以上是关于带子查询的redshift sql查询中的语法错误的主要内容,如果未能解决你的问题,请参考以下文章
在 REDSHIFT 中的 ALTER 语句中连接字符串(语法错误)
使用 case when 时出现 SQL (Redshift) 错误 - 不支持这种类型的相关子查询模式
read_sql 和 redshift 在 unicode 上给出错误