2012-02-09 9 views
12

Si consideri la seguente query:colonna "statement_date" è di tipo data, ma l'espressione è di tipo integer

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; 

Questo sta tornando:

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 

mia struttura della tabella per 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" 

risposta

13

Si sta inserendo il imps.userid nella colonna statement_date. Questo deve fallire.

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

di specificare l'ordine si sta inserendo si può fare questo:

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

Ah, ho avuto l'impressione che esso inserito nella stessa colonna di nome. Questo lo spiega. –

+0

Per me l'istruzione di inserimento che ha funzionato era: 'INSERIRE in statement_line_items (col1, col2, ...) selezionare data_for_col1, data_for_col2, ... da xyz'. Sto usando Amazon Redshift. –

Problemi correlati