I'm looking to input a list of integers into a function and return rows based on this. In this case, I want to select some attributes based on employer id, e.g. select where employer id = 102,103 and 105.
This is what I have tried.
CREATE OR REPLACE FUNCTION dummy(VARIADIC e_id NUMERIC[])
RETURNS TABLE (
emp_id INT,
first_name VARCHAR,
last_name VARCHAR,
salary INT
)
AS $$
BEGIN
RETURN QUERY SELECT em.emp_id, em.first_name, em.last_name, em.salary
FROM employee em
WHERE em.emp_id IN e_id;
END; $$
LANGUAGE PLPGSQL;
SELECT *
FROM dummy(102, 103, 105);
The error appears to be from the line containing WHERE. How am I able to select based on the list of integers e_id?
WHERE em.emp_id IN e_id
Since you are dealing with a list of numbers, you could just use the any construct instead of in:
CREATE OR REPLACE FUNCTION dummy(VARIADIC e_id NUMERIC[])
RETURNS TABLE (
emp_id INT,
first_name VARCHAR,
last_name VARCHAR,
salary INT
)
AS $$
BEGIN
RETURN QUERY SELECT em.emp_id, em.first_name, em.last_name, em.salary
FROM employee em
WHERE em.emp_id = any (e_id);
END; $$
LANGUAGE PLPGSQL;
Demo on DB Fiddle
create table employee(emp_id int, first_name varchar(50), last_name varchar(50), salary int);
insert into employee values
(1, 'a', 'b', 50),
(2, 'c', 'd', 100),
(3, 'e', 'f', 400);
CREATE OR REPLACE FUNCTION dummy(VARIADIC e_id NUMERIC[])
RETURNS TABLE (
emp_id INT,
first_name VARCHAR,
last_name VARCHAR,
salary INT
)
AS $$
BEGIN
RETURN QUERY SELECT em.emp_id, em.first_name, em.last_name, em.salary
FROM employee em
WHERE em.emp_id = any (e_id);
END; $$
LANGUAGE PLPGSQL;
SELECT * FROM dummy(1, 2);
emp_id | first_name | last_name | salary
-----: | :--------- | :-------- | -----:
1 | a | b | 50
2 | c | d | 100
Related
I have a table where insertion is in this form.
Table
I want the verion to get update by 1 whenever there is a new row with same name and id.
Required output
I tried using a function and trigger.
CREATE OR REPLACE FUNCTION update_ver()
RETURNS TRIGGER
LANGUAGE PLPGSQL
AS
$$
BEGIN
update version
set ver = ver + 1
where new.name = 'A' and new.id ='1';
RETURN new;
END;
$$
-- create table
CREATE TABLE mytable (
"name" varchar NULL,
id int4 NULL,
phone varchar NULL,
email varchar NULL,
ver int4 NULL
);
-- create trigger function
CREATE OR REPLACE FUNCTION before_insert()
RETURNS TRIGGER
LANGUAGE PLPGSQL
AS
$$
begin
new.ver = (select coalesce(max(ver), 0) + 1 from mytable where name = new.name and id = new.id);
return new;
end;
$$
-- set trigger function to table
create trigger trg_before_insert before
insert
on
mytable for each row execute function before_insert();
-- inserting sample data
INSERT INTO mytable ("name", id, phone, email) VALUES('A', 1, '123', '123#email.com');
INSERT INTO mytable ("name", id, phone, email) VALUES('B', 2, '345', '345#email.com');
INSERT INTO mytable ("name", id, phone, email) VALUES('A', 1, '456', '456#email.com');
-- select data and view
select * from mytable;
Result:
name|id|phone|email |ver|
----+--+-----+-------------+---+
A | 1|123 |123#email.com| 1|
B | 2|345 |345#email.com| 1|
A | 1|456 |456#email.com| 2|
I need to create function returning schedule of trains in different stations.
I have something like that:
create or replace function f124(st varchar) returns table(odjazdy varchar, kierunek varchar, przewoznik varchar, peron varchar, tor int)
as
$$ declare s varchar; sta varchar:=st;
begin
drop table if exists f124p;
create table f124p(odjazdy varchar, kierunek varchar, przewoznik varchar, peron varchar, tor int);
select string_agg('insert into f124p select k."'||nr||'", ''stacja'', ''przewoznik'', t.peron, t.tor from gdymal_ic_kursy k inner join gdymal_ic_trasa t on k.stacja=t.stacja where t.stacja=''sta'';','') into s from generate_series(5110, 5118,2) as nr;
execute s;
return query select * from f124p;
end $$ language plpgsql;
The problem is that this function returns empty table. It shouldn't be like that beacuse there are information which must be in this table. I think there is a problem near k."'||nr||'"
The insert should bring data from columns named "5110", "5112" as varchars. (In gdymal_ic_kursy there are columns "5110", "5112", ..., "5118"). Maybe this is a problem. Maybe You have any tips how should I repair this function?
I don't know what it means, but it works here:
\i tmp.sql
CREATE TABLE gdymal_ic_kursy
( stacja text
, "5110" text
, "5111" text
, "5112" text
, "5113" text
, "5114" text
, "5115" text
, "5116" text
, "5117" text
, "5118" text
);
INSERT INTO gdymal_ic_kursy VALUES ('sta', '1', '2', '3', '4', '5', '6', '7', '8');
CREATE TABLE gdymal_ic_trasa
( stacja text
, peron text
, tor integer
);
INSERT INTO gdymal_ic_trasa(stacja,peron,tor) VALUES ('sta', 'one', 666 );
-- ------------------------------
create or replace function f124(st varchar)
returns table(odjazdy varchar, kierunek varchar, przewoznik varchar, peron varchar, tor int)
as
$func$
declare s varchar; sta varchar := st;
begin
drop table if exists f124p;
create table f124p(odjazdy varchar, kierunek varchar, przewoznik varchar, peron varchar, tor int);
select string_agg('insert into f124p select k."'||nr||'", ''stacja'', ''przewoznik'', t.peron, t.tor
from gdymal_ic_kursy k
inner join gdymal_ic_trasa t on k.stacja=t.stacja
where t.stacja=''sta'';','')
into s from generate_series(5110, 5118,2) as nr;
execute s;
return query select * from f124p;
end $func$ language plpgsql;
select *
FROM f124('OMG')
;
Result:
DROP SCHEMA
CREATE SCHEMA
SET
CREATE TABLE
INSERT 0 1
CREATE TABLE
INSERT 0 1
CREATE FUNCTION
NOTICE: table "f124p" does not exist, skipping
odjazdy | kierunek | przewoznik | peron | tor
---------+----------+------------+-------+-----
1 | stacja | przewoznik | one | 666
3 | stacja | przewoznik | one | 666
5 | stacja | przewoznik | one | 666
7 | stacja | przewoznik | one | 666
| stacja | przewoznik | one | 666
(5 rows)
I'm having some trouble working out the PostgreSQL documentation for recursive queries, and wonder if anyone might be able to offer a suggestion for the following.
Here's the data:
Table "public.subjects"
Column | Type | Collation | Nullable | Default
-------------------+-----------------------------+-----------+----------+--------------------------------------
id | bigint | | not null | nextval('subjects_id_seq'::regclass)
name | character varying | | |
Table "public.subject_associations"
Column | Type | Collation | Nullable | Default
------------+-----------------------------+-----------+----------+--------------------------------------------------
id | bigint | | not null | nextval('subject_associations_id_seq'::regclass)
parent_id | integer | | |
child_id | integer | | |
Here, a "subject" may have many parents and many children. Of course, at the top level a subject has no parents and at the bottom no children. For example:
parent_id | child_id
------------+------------
2 | 3
1 | 4
1 | 3
4 | 8
4 | 5
5 | 6
6 | 7
What I'm looking for is starting with a child_id to get all the ancestors, and with a parent_id, all the descendants. Therefore:
parent_id 1 -> children 3, 4, 5, 6, 7, 8
parent_id 2 -> children 3
child_id 3 -> parents 1, 2
child_id 4 -> parents 1
child_id 7 -> parents 6, 5, 4, 1
Though there seem to be a lot of examples of similar things about I'm having trouble making sense of them, so any suggestions I can try out would be welcome.
To get all children for subject 1, you can use
WITH RECURSIVE c AS (
SELECT 1 AS id
UNION ALL
SELECT sa.child_id
FROM subject_associations AS sa
JOIN c ON c.id = sa. parent_id
)
SELECT id FROM c;
CREATE OR REPLACE FUNCTION func_finddescendants(start_id integer)
RETURNS SETOF subject_associations
AS $$
DECLARE
BEGIN
RETURN QUERY
WITH RECURSIVE t
AS
(
SELECT *
FROM subject_associations sa
WHERE sa.id = start_id
UNION ALL
SELECT next.*
FROM t prev
JOIN subject_associations next ON (next.parentid = prev.id)
)
SELECT * FROM t;
END;
$$ LANGUAGE PLPGSQL;
Try this
--- Table
-- DROP SEQUENCE public.data_id_seq;
CREATE SEQUENCE "data_id_seq"
INCREMENT 1
MINVALUE 1
MAXVALUE 9223372036854775807
START 1
CACHE 1;
ALTER TABLE public.data_id_seq
OWNER TO postgres;
CREATE TABLE public.data
(
id integer NOT NULL DEFAULT nextval('data_id_seq'::regclass),
name character varying(50) NOT NULL,
label character varying(50) NOT NULL,
parent_id integer NOT NULL,
CONSTRAINT data_pkey PRIMARY KEY (id),
CONSTRAINT data_name_parent_id_unique UNIQUE (name, parent_id)
)
WITH (
OIDS=FALSE
);
INSERT INTO public.data(id, name, label, parent_id) VALUES (1,'animal','Animal',0);
INSERT INTO public.data(id, name, label, parent_id) VALUES (5,'birds','Birds',1);
INSERT INTO public.data(id, name, label, parent_id) VALUES (6,'fish','Fish',1);
INSERT INTO public.data(id, name, label, parent_id) VALUES (7,'parrot','Parrot',5);
INSERT INTO public.data(id, name, label, parent_id) VALUES (8,'barb','Barb',6);
--- Function
CREATE OR REPLACE FUNCTION public.get_all_children_of_parent(use_parent integer) RETURNS integer[] AS
$BODY$
DECLARE
process_parents INT4[] := ARRAY[ use_parent ];
children INT4[] := '{}';
new_children INT4[];
BEGIN
WHILE ( array_upper( process_parents, 1 ) IS NOT NULL ) LOOP
new_children := ARRAY( SELECT id FROM data WHERE parent_id = ANY( process_parents ) AND id <> ALL( children ) );
children := children || new_children;
process_parents := new_children;
END LOOP;
RETURN children;
END;
$BODY$
LANGUAGE plpgsql VOLATILE COST 100;
ALTER FUNCTION public.get_all_children_of_parent(integer) OWNER TO postgres
--- Test
SELECT * FROM data WHERE id = any(get_all_children_of_parent(1))
SELECT * FROM data WHERE id = any(get_all_children_of_parent(5))
SELECT * FROM data WHERE id = any(get_all_children_of_parent(6))
CREATE OR REPLACE FUNCTION public.get_locations(
location_word varchar(50)
)
RETURNS TABLE
(
country varchar(50),
city varchar(50)
)
AS $$
DECLARE
location_word_ varchar(50);
BEGIN
location_word_:=concat(location_word, '%');
RETURN QUERY EXECUTE format(' (SELECT c.country, ''''::varchar(50) as city FROM webuser.country c
WHERE lower(c.country) LIKE %L LIMIT 1)
UNION
(SELECT c.country,ci.city FROM webuser.country c
JOIN webuser.city ci ON c.country_id=ci.country_id
WHERE lower(ci.city) LIKE %L LIMIT 4)',
location_word_,
location_word_ ) ;
END
$$ language PLPGSQL STABLE;
SELECT public.get_locations('a'::varchar(50));
I get this;
+get_locations +
+record +
------------------
+(Andorra,"") +
+(Germany,Aach) +
+(Germany,Aalen) +
+(Germany,Achim) +
+(Germany,Adenau)+
How can i place/get the values column by column like below? Because otherwise i can not match the values correctly. I should get the values column by column as countries and cities etc.
|country | city |
-------------------------------
| Andorra | "" |
| Germany | Aach |
| Germany | Aalen |
| Germany | Achim |
| Germany | Adenau |
Your function is declared as returns table so you have to use it like a table:
SELECT *
FROM public.get_locations('a'::varchar(50));
Unrelated, but:
Your function is way too complicated, you don't need dynamic SQL, nor do you need a PL/pgSQL function.
You can simplify that to:
CREATE OR REPLACE FUNCTION public.get_locations(p_location_word varchar(50))
RETURNS TABLE(country varchar(50), city varchar(50))
AS $$
(SELECT c.country, ''::varchar(50) as city
FROM webuser.country c
WHERE lower(c.country) LIKE concat(p_location_word, '%')
LIMIT 1)
UNION ALL
(SELECT c.country ,ci.city
FROM webuser.country c
JOIN webuser.city ci ON c.country_id = ci.country_id
WHERE lower(ci.city) LIKE concat(p_location_word, '%')
LIMIT 4)
$$
language SQL;
I have a table
create table t
(id serial primary key,
name text,
val INTEGER);
insert into t(name, val)
select 'user1', x'0001'::INT
union all
select 'user1', x'0010'::INT
union all
select 'user1', x'0110'::INT
union all
select 'user2', x'0001'::INT
How I can select values into variable using bit operation for given name?
create or replace function get_union(
name text,
OUT retval int
)
as $BODY$
begin
-- ?
end
$BODY$ language plpgsql;
For example, the function should return 111 for name 'user1'
select to_hex(x'0001'::int | x'0010'::int | x'0100'::int);
---
111
select to_hex(bit_or(val)), bit_or(val), bit_or(val)::bit(16)
from t
where name = 'user1';
to_hex | bit_or | bit_or
--------+--------+------------------
111 | 273 | 0000000100010001