2012-02-09 45 views
12

考虑以下查询:列 “statement_date” 的类型是日期,但表达的类型为整数

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; 

这被返回:

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 

我给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" 

回答

13

您正在将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 

要指定要插入你可以这样做的顺序:

INSERT INTO statement_line_items (col1, col2, ...) 
values (select data_for_col1, data_for_col2, ...) 
+5

啊,我的印象是,它插入到相同的命名列下。这解释了它。 – 2012-02-09 11:30:46

+0

对我来说,插入语句的工作原理是:'插入到statement_line_items(col1,col2,...) 从xyz'中选择data_for_col1,data_for_col2,...。我正在使用Amazon Redshift。 – 2016-03-02 10:54:29

相关问题