【问题标题】:Syntax error in redshift sql query with subqueries带有子查询的 redshift sql 查询中的语法错误
【发布时间】:2020-01-26 20:23:48
【问题描述】:

总的来说,我对 SQL 很陌生,以前没有处理过红移。我正在尝试进行一个查询,该查询在 postgresql 中完美运行。但是我在红移中遇到语法错误。查询是:

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 我不明白那里有什么问题。 通过这个查询,我试图以某种顺序在阅读页面期间获取用户操作的会话列表。 将感谢任何帮助!

【问题讨论】:

  • 将子查询的别名从 test.table_1 更改为 t1 并在您的查询中使用它。
  • 文档没有明确说明它也适用于别名,但我猜别名命名应该遵循相同的规则:docs.aws.amazon.com/redshift/latest/dg/r_names.html 它说:不包含引号和空格。

标签: sql amazon-redshift


【解决方案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
  • 我从未在:: 周围看到空格。当然是允许的,但是类型转换的优先级很高,并且通常不带空格。
  • 我更喜欢时间戳比较而不是时间戳,而不是转换为间隔。间隔时间可能会发生奇怪的事情。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-04
    相关资源
    最近更新 更多