2012-02-09 10 views
12

aşağıdaki sorgu düşünün tipi tamsayı geçerli:sütun "statement_date" tarih türünde ama ifadesi

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; 

Bu döndürüyor:

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 Benim masa yapısı şöyledir:

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

cevap

13

statement_date sütununa imps.userid kodunu yerleştiriyorsunuz. Bu başarısız olmalı.

count(*)::integer as clicks goes into id 
sum(amount_cents)::integer as amount_cents goes into userid 
imps.user_id goes into statement_date 

sen bunu yapabilirsin ekleyerek sırasını belirtmek için:

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

Ah, ben aynı adlı sütunun eklenen izlenim oldu. Bu açıklıyor. –

+0

Benim için işe yaradı ifadesi şu oldu: 'INSERT INTO statement_line_items (col1, col2, ...) select data_for_col1, data_for_col2, ... xyz'den. Amazon Redshift kullanıyorum. –

İlgili konular