0

Is it possible to bind an array to a name in a SQL statement? I'm looking for something similar to a Lisp/Haskell let-expression.

My first thought was to use a 'with' clause before the select statement that uses the array in a function call.

WITH thenumbers AS ARRAY(SELECT buildingnum FROM numbers WHERE area = 'Bu',
                     SELECT buildingnum FROM numbers WHERE area = 'Ba',
                     SELECT buildingnum FROM numbers WHERE area = 'E',
                     SELECT buildingnum FROM numbers WHERE area = 'O',
                     SELECT buildingnum FROM numbers WHERE area = 'Mi',
                     SELECT buildingnum FROM numbers WHERE area = 'Ho',
                     SELECT buildingnum FROM numbers WHERE area = 'Wa',
                     SELECT buildingnum FROM numbers WHERE area = 'Wi')
SELECT bnorm(area,preal,thenumbers) FROM electrical_energy;

Two details that might inform an answer are that I'm working with Postgres 8.3 and 'numbers' is a view rather than a table.

1 Answer 1

1

Something like this might work:

SELECT bnorm( area, 
              preal, 
              (select array_agg(buildingnum) 
               from numbers 
               where area in ('Bu','Ba','E','O','Mi','Ho','Wa','Wi') 
             ) 
FROM electrical_energy;

8.3 is pretty old and no longer supported you should really plan an upgrade now.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.