在SQL中绑定数组

时间:2014-03-08 19:19:17

标签: sql postgresql

是否可以将数组绑定到SQL语句中的名称?我正在寻找类似于Lisp / Haskell let-expression的东西。

我的第一个想法是在函数调用中使用数组的select语句之前使用'with'子句。

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;

可能会给出答案的两个细节是我正在使用Postgres 8.3而'数字'是一个视图而不是一个表。

1 个答案:

答案 0 :(得分:1)

这样的事可能有用:

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已经过时了,不再支持你真的现在计划升级。