创建一个返回表格的Postgres函数

时间:2016-09-13 16:34:43

标签: postgresql function

我有这个功能

CREATE OR REPLACE FUNCTION public.getusers(in user_id numeric)
  RETURNS SETOF record
AS
$BODY$
  DECLARE
    ids character varying;
BEGIN



CREATE or REPLACE  TEMP VIEW test AS
         SELECT fx.*,EXTRACT(epoch FROM fx.time -(fx.timebuffer::text||' minute')::INTERVAL)::DOUBLE PRECISION as startTime
        ,EXTRACT(epoch FROM fx.time +(fx.timebuffer::text||' minute')::INTERVAL)::DOUBLE PRECISION as endTime
        -- ... maybe more columns and expressions ...
        FROM userlocation fx
        ;

CREATE or REPLACE  TEMP VIEW buffer AS
SELECT *,st_buffer(location,ul.buffer) as buffered from userlocation ul
        ;


 RETURN QUERY (
select "UserInfo".username as uid,"UserInfo"."userID" as uname,"UserInfo".id as id from "UserInfo" , (

  (SELECT f1."userID"--,f1."ID" as locationID
   FROM test f1
   WHERE EXISTS(
       SELECT *
       FROM test f2
       WHERE numrange((f2.startTime) :: NUMERIC, (f2.endTime) :: NUMERIC) &&
             numrange((f1.startTime) :: NUMERIC, (f1.endTime) :: NUMERIC)
             AND f2.locationtype = f1.locationtype
             AND f2."userID"=user_id
             AND f2.locationtype = 2)
           --  AND f2."ID" <> f1."ID")

  INTERSECT

  SELECT u1."userID"--,u1."ID"
  FROM buffer AS u1,
    (SELECT *
     FROM buffer ul) AS u2
  WHERE st_intersects(u1.buffered, u2.buffered) = true
        AND u1.locationtype = u2.locationtype
         AND f2."userID"=user_id
        AND u1.locationtype = 2)
      --  AND u1."ID" <> u2."ID")
  INTERSECT

  (SELECT f3."userID"--,f3."ID"
   FROM test f3
   WHERE EXISTS(
       SELECT *
       FROM test f4
       WHERE numrange((f4.startTime) :: NUMERIC, (f4.endTime) :: NUMERIC) &&
             numrange((f3.startTime) :: NUMERIC, (f3.endTime) :: NUMERIC)
             AND f4.locationtype = f3.locationtype

    AND f2."userID"=user_id
             AND f4.locationtype = 1)
          --   AND f4."ID" <> f3."ID")

  INTERSECT

  SELECT u3."userID"--,u3."ID"
  FROM buffer AS u3,
    (SELECT *
     FROM buffer ul) AS u4
  WHERE st_intersects(u3.buffered, u4.buffered) = true
        AND u3.locationtype = u4.locationtype

    AND f2."userID"=user_id
        AND u3.locationtype = 1)
       -- AND u3."ID" <> u4."ID")

  ) inter WHERE  inter."userID"="UserInfo".username);
    --Some code which build the ids string, not interesting for this issue

   -- RETURN QUERY EXECUTE 'SELECT users.id, users.firstname, users.lastname
   -- FROM public.users WHERE ids IN (' || ids || ')';






END;
$BODY$
LANGUAGE plpgsql VOLATILE;

这个查询应该给我一个表,当我在RETURN QUERY(....)中运行查询部分它运行正常但是当我运行这个函数时它给了我这个错误

ERROR: missing FROM-clause entry for table "f2"
  Where: PL/pgSQL function getusers(numeric) line 20 at RETURN QUERY

我做错了什么?能帮我找到问题吗? 感谢

1 个答案:

答案 0 :(得分:1)

INTERSECT查询的第2,第3和第4部分,您尝试访问f2部分中WHERE部分中的表u1."userID" = user_id -- second f3."userID" = user_id -- third u3."userID" = user_id -- fourth 中的字段。你可能想分别输入:

  "@angular/compiler-cli": "github:angular/compiler-cli-builds",
  "@angular/common": "2.0.0-rc.6",
  "@angular/compiler": "2.0.0-rc.6",
  "@angular/core": "2.0.0-rc.6",
  "@angular/forms": "^2.0.0-rc.6",
  "@angular/http": "2.0.0-rc.6",
  "@angular/platform-browser": "2.0.0-rc.6",
  "@angular/platform-browser-dynamic": "2.0.0-rc.6",
  "@angular/platform-server": "2.0.0-rc.6",
  "@angular/router": "3.0.0-rc.2",