Skip to content

Instantly share code, notes, and snippets.

@krlicmuhamed
Created August 29, 2015 13:05
Show Gist options
  • Save krlicmuhamed/72f8b73d983f13c2a6a4 to your computer and use it in GitHub Desktop.
Save krlicmuhamed/72f8b73d983f13c2a6a4 to your computer and use it in GitHub Desktop.
An example of postgres function ( stored procedure ) that returns a query.
CREATE OR REPLACE FUNCTION stored_get_data_history(SPGroupID integer, FromDate timestamp with time zone, ToDate timestamp with time zone)
RETURNS TABLE (device_id integer, "location" geometry, accuracy DOUBLE PRECISION, speed DOUBLE PRECISION, azimuth DOUBLE PRECISION, "createdAt" timestamp with time zone) LANGUAGE PLPGSQL AS $$
BEGIN
RETURN QUERY SELECT
"Data".device_id,
"Data".location,
"Data".accuracy,
"Data".speed,
"Data".azimuth,
"Data"."createdAt"
FROM
public."Data",
public."DeviceMemberships"
WHERE
"DeviceMemberships".device_id = "Data".device_id AND
"DeviceMemberships".group_id = SPGroupID AND
"Data"."createdAt" >= FromDate AND
"Data"."createdAt" <= ToDate;
END;
$$;
SELECT * FROM stored_get_data_history(0, '2015-08-29 14:12:17.914+02','2015-08-29 14:50:27.37+02');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment