列"statement_date"的类型为date,但expression的类型为integer

Nei*_*ton 17 postgresql

请考虑以下查询:

INSERT INTO     statement_line_items
SELECT      count(*)::integer as clicks, sum(amount_cents)::integer as amount_cents, imps.user_id, imps.created_at::date as statement_date
FROM        impression_events imps
INNER JOIN  transactions t ON t.event_id = imps.id
  AND       t.event_type = 'ImpressionEvent'
  AND       amount_cents >= 0
WHERE       imps.created_at >= (now() - interval '8 days')::date
  AND       imps.created_at < (now() - interval '7 day')::date
  AND       imps.clicked = true
GROUP BY    imps.user_id, imps.created_at::date;
Run Code Online (Sandbox Code Playgroud)

这是回归:

ERROR:  column "statement_date" is of type date but expression is of type integer
LINE 2: ...icks, sum(amount_cents)::integer as amount_cents, imps.user_...
                                                             ^
HINT:  You will need to rewrite or cast the expression.

********** Error **********

ERROR: column "statement_date" is of type date but expression is of type integer
SQL state: 42804
Hint: You will need to rewrite or cast the expression.
Character: 117
Run Code Online (Sandbox Code Playgroud)

我对statement_line_items的表结构是:

"id";              "integer"
"user_id";         "integer"
"statement_date";  "date"
"description";     "character varying(255)"
"clicks";          "integer"
"amount_cents";    "integer"
"created_at";      "timestamp without time zone"
"updated_at";      "timestamp without time zone"
Run Code Online (Sandbox Code Playgroud)

jue*_*n d 16

你把它imps.userid放在你的statement_date专栏中.那不得不失败.

count(*)::integer as clicks                   goes into id
sum(amount_cents)::integer as amount_cents    goes into userid
imps.user_id                                  goes into statement_date
Run Code Online (Sandbox Code Playgroud)

要指定要插入的顺序,可以执行以下操作:

INSERT INTO statement_line_items (col1, col2, ...)
values (select data_for_col1, data_for_col2, ...)
Run Code Online (Sandbox Code Playgroud)

  • 啊,我的印象是它插入到同一个命名列中.这解释了它. (6认同)
  • 对我来说,有效的插入语句是:“INSERT INTO statements_line_items (col1, col2, ...) select data_for_col1, data_for_col2, ... from xyz”。我正在使用亚马逊红移。 (2认同)