2012-02-09 13 views
12

であり、次のクエリを考えてみます。コラム "statement_dateは、" 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; 

これは戻っている:statement_line_itemsため

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 

私のテーブルの構造は次のとおりです。

"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

statement_dateの列にimps.useridを入れています。それは失敗しなければならない。

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

ああ、私はそれは同じ名前の列に挿入という印象の下にありました。それはそれを説明する。 –

+0

私のために働いていたINSERTステートメントは次のとおりでした:INSERT INTO statement_line_items(col1、col2、...) select data_for_col1、data_for_col2、... from xyz'。 Amazon Redshiftを使用しています。 –

関連する問題