plpgsql

How do I write a function in plpgsql that compares a date with a timestamp without time zone?

社会主义新天地 提交于 2021-02-17 03:12:31
问题 I want to write a function that returns a table with all the rows between firstDate and lastDate . The rows have datatype timestamp without time zone They also have to be of a specific node id. This is my function: CREATE OR REPLACE FUNCTION get_measurements_by_node_and_date(nodeID INTEGER, firstDate date, lastDate date) RETURNS TABLE (measurement_id INTEGER, node_id INTEGER, carbon_dioxide DOUBLE PRECISION, hydrocarbons DOUBLE PRECISION, temperature DOUBLE PRECISION, humidity DOUBLE

How do I write a function in plpgsql that compares a date with a timestamp without time zone?

こ雲淡風輕ζ 提交于 2021-02-17 03:08:24
问题 I want to write a function that returns a table with all the rows between firstDate and lastDate . The rows have datatype timestamp without time zone They also have to be of a specific node id. This is my function: CREATE OR REPLACE FUNCTION get_measurements_by_node_and_date(nodeID INTEGER, firstDate date, lastDate date) RETURNS TABLE (measurement_id INTEGER, node_id INTEGER, carbon_dioxide DOUBLE PRECISION, hydrocarbons DOUBLE PRECISION, temperature DOUBLE PRECISION, humidity DOUBLE

Using Postgres domains to simplify function input validation

巧了我就是萌 提交于 2021-02-10 20:46:24
问题 Using Postgres 11.5, I've been looking at CREATE DOMAIN since yesterday, and would like to clarify how they can/can't help with function parameters. Ideally, I'd like to use a domain to screen parameter inputs easily, but with a helpful error response. As an example, I'm using a simple first-case, a domain that blocks null and empty strings: CREATE DOMAIN text_not_empty AS text NOT NULL CHECK (value <> ''); I tried this out as a field type for a table, and it's great. When we don't allow

Using Postgres domains to simplify function input validation

霸气de小男生 提交于 2021-02-10 20:45:06
问题 Using Postgres 11.5, I've been looking at CREATE DOMAIN since yesterday, and would like to clarify how they can/can't help with function parameters. Ideally, I'd like to use a domain to screen parameter inputs easily, but with a helpful error response. As an example, I'm using a simple first-case, a domain that blocks null and empty strings: CREATE DOMAIN text_not_empty AS text NOT NULL CHECK (value <> ''); I tried this out as a field type for a table, and it's great. When we don't allow

PostgreSQL function returning a data cube

混江龙づ霸主 提交于 2021-02-10 06:31:28
问题 First off, the iceberg-cube query is defined as in Let's say I have a relation item,location,year,supplier,unit_sales , and I would like to write a plpgsql functions as a wrapper around the query in the image, to specify the parameter N , like so: create or replace function iceberg_query( percentage integer ) returns cube /* Code here */ as $$ declare numrows int; begin select count(*) into numrows from sales; select item, location, year, count(*) from sales group by cube(item,location,year)

PostgreSQL function returning a data cube

回眸只為那壹抹淺笑 提交于 2021-02-10 06:31:28
问题 First off, the iceberg-cube query is defined as in Let's say I have a relation item,location,year,supplier,unit_sales , and I would like to write a plpgsql functions as a wrapper around the query in the image, to specify the parameter N , like so: create or replace function iceberg_query( percentage integer ) returns cube /* Code here */ as $$ declare numrows int; begin select count(*) into numrows from sales; select item, location, year, count(*) from sales group by cube(item,location,year)

postgreSQL Fibonacci Sequence - Query has no destination for result data

泄露秘密 提交于 2021-02-08 06:23:33
问题 So I wrote a Fibonacci sequence function like this: CREATE OR REPLACE FUNCTION fibonacci (lastN INTEGER) RETURNS int AS $$ BEGIN WITH RECURSIVE t(a, b) AS ( VALUES(0,1) UNION ALL SELECT GREATEST(a, b), a + b AS a from t WHERE b < $1 ) SELECT a FROM t; END; $$ LANGUAGE plpgsql; But when I called: SELECT * FROM fibonacci(20); the console shows: ERROR: query has no destination for result data HINT: If you want to discard the results of a SELECT, use PERFORM instead. CONTEXT: PL/pgSQL function

postgreSQL Fibonacci Sequence - Query has no destination for result data

蹲街弑〆低调 提交于 2021-02-08 06:23:07
问题 So I wrote a Fibonacci sequence function like this: CREATE OR REPLACE FUNCTION fibonacci (lastN INTEGER) RETURNS int AS $$ BEGIN WITH RECURSIVE t(a, b) AS ( VALUES(0,1) UNION ALL SELECT GREATEST(a, b), a + b AS a from t WHERE b < $1 ) SELECT a FROM t; END; $$ LANGUAGE plpgsql; But when I called: SELECT * FROM fibonacci(20); the console shows: ERROR: query has no destination for result data HINT: If you want to discard the results of a SELECT, use PERFORM instead. CONTEXT: PL/pgSQL function

PostgreSQL: Checking for NEW and OLD in a function for a trigger

喜你入骨 提交于 2021-02-08 03:54:45
问题 I want to create a trigger which counts rows and updates a field in an other table. My current solution works for INSERT statements but failes when I DELETE a row. My current function: CREATE OR REPLACE FUNCTION update_table_count() RETURNS trigger AS $$ DECLARE updatecount INT; BEGIN Select count(*) into updatecount From source_table Where id = new.id; Update dest_table set count=updatecount Where id = new.id; RETURN NEW; END; $$ LANGUAGE 'plpgsql'; The trigger is a pretty basic one, looking

PostgreSQL: Checking for NEW and OLD in a function for a trigger

狂风中的少年 提交于 2021-02-08 03:54:13
问题 I want to create a trigger which counts rows and updates a field in an other table. My current solution works for INSERT statements but failes when I DELETE a row. My current function: CREATE OR REPLACE FUNCTION update_table_count() RETURNS trigger AS $$ DECLARE updatecount INT; BEGIN Select count(*) into updatecount From source_table Where id = new.id; Update dest_table set count=updatecount Where id = new.id; RETURN NEW; END; $$ LANGUAGE 'plpgsql'; The trigger is a pretty basic one, looking