views:

23

answers:

2

The following query:

SELECT SUM (peak_power) FROM t_mpi_pv WHERE info_id IN ( 
 SELECT mp_info_id FROM t_mp WHERE mp_id IN (
  ARRAY[29800001,29800008,29800015,29800022,29800029]
 )
)

Doesn't work. I get the error:

"Operator does not exist: integer = integer[]".

But

SELECT SUM (peak_power) FROM t_mpi_pv WHERE info_id IN ( 
 SELECT mp_info_id FROM t_mp WHERE mp_id = 29800001  
)

Works.

What am I doing wrong?

+1  A: 

Try:

SELECT SUM (peak_power) FROM t_mpi_pv WHERE info_id IN ( 
 SELECT mp_info_id FROM t_mp WHERE mp_id IN 
  (29800001,29800008,29800015,29800022,29800029)
 )
)
codaddict
It works! Thank you!
MrB
A: 

Alternatively, use this syntax:

SELECT  SUM(peak_power)
FROM    t_mpi_pv
WHERE   info_id IN
        (
        SELECT  mp_info_id
        FROM    t_mp
        WHERE   mp_id = ANY(ARRAY[29800001,29800008,29800015,29800022,29800029])
        )

Useful if you pass the array as a single bound variable or a parameter of a function:

CREATE FUNCTION fn_peak_power (mpids INT[])
AS
$$
        SELECT  SUM(peak_power)
        FROM    t_mpi_pv
        WHERE   info_id IN
                (
                SELECT  mp_info_id
                FROM    t_mp
                WHERE   mp_id = ANY($1)
                );
$$
LANGUAGE 'sql';
Quassnoi