【问题标题】:Update Redshift table from query从查询更新 Redshift 表
【发布时间】:2015-11-21 22:32:51
【问题描述】:

我正在尝试通过查询更新 Redshift 中的表:

update mr_usage_au au
inner join(select mr.UserId,
                  date(mr.ActionDate) as ActionDate,
                  count(case when mr.EventId in (32) then mr.UserId end) as Moods,
                  count(case when mr.EventId in (33) then mr.UserId end) as Activities,
                  sum(case when mr.EventId in (10) then mr.Duration end) as Duration
           from   mr_session_log mr
           where  mr.EventTime >= current_date - interval '1 days' and mr.EventTime < current_date
           Group By mr.UserId,
                    date(mr.ActionDate)) slog on slog.UserId=au.UserId
                                             and slog.ActionDate=au.Date
set au.Moods = slog.Moods,
    au.Activities=slog.Activities,
    au.Durarion=slog.Duration

但我收到以下错误:

ERROR: syntax error at or near "au".

【问题讨论】:

标签: postgresql amazon-web-services sql-update amazon-redshift


【解决方案1】:

这对于 Redshift(或 Postgres)来说是完全无效的语法。让我想起了 SQL Server ...

应该像这样工作(至少在当前的 Postgres 上):

UPDATE mr_usage_au
SET    Moods = slog.Moods
     , Activities = slog.Activities
     , Durarion = slog.Duration       
FROM (
   select UserId
        , ActionDate::date
        , count(CASE WHEN EventId = 32 THEN UserId END) AS Moods
        , count(CASE WHEN EventId = 33 THEN UserId END) AS Activities
        , sum(CASE WHEN EventId = 10 THEN Duration END) AS Duration
   FROM   mr_session_log
   WHERE  EventTime >= current_date - 1  -- just subtract integer from a date
   AND    EventTime <  current_date
   GROUP  BY UserId, ActionDate::date
   ) slog
WHERE slog.UserId = mr_usage_au.UserId
AND   slog.ActionDate = mr_usage_au.Date;

这通常是 Postgres 和 Redshift 的情况:

  • 使用FROM 子句加入其他表。
  • 您不能在SET 子句中对目标列进行表限定。

另外,Redshift was forked from PostgreSQL 8.0.2,这是很久以前的事了。仅应用了对 Postgres 的一些后期更新。

我简化了一些其他细节。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-19
    • 1970-01-01
    • 2020-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多