Function in PostgreSQL to insert from one table to another? - postgresql

I have the following:
I got the tables:
Equipos (Teams)
Partidos (Matches)
The columns num_eqpo_loc & num_eqpo_vis from the table partidos reference to the table equipo. They reference to the num_eqpo column.
As you can see here:
create table equipos
(num_eqpo serial,
ciudad varchar (30),
num_gpo int,
nom_equipo varchar (30),
primary key (num_eqpo),
foreign key (num_gpo) references grupos (num_gpo))
create table partidos
(semana int,
num_eqpo_loc int,
num_eqpo_vis int,
goles_loc int,
goles_vis int, primary key (semana,num_eqpo_loc,num_eqpo_vis),
foreign key (num_eqpo_loc) references equipos (num_eqpo),
foreign key (num_eqpo_vis) references equipos (num_eqpo))
I want to get the following output:
In one hand, I created a table called general:
CREATE TABLE general
(
equipo character varying(30) NOT NULL,
partidos_jug integer,
partidos_gana integer,
partidos_emp integer,
partidos_perd integer,
puntos integer,
goles_favor integer,
CONSTRAINT general_pkey PRIMARY KEY (equipo)
)
In the other, I have the function:
CREATE OR REPLACE FUNCTION sp_tablageneral () RETURNS TABLE (
equipo character varying(30)
, partidos_jug int
, partidos_gana int
, partidos_emp int
, partidos_perd int
, puntos int
, goles_favor int) AS
$BODY$
DECLARE cont int:= (SELECT count(num_eqpo)FROM equipos);
r partidos%ROWTYPE;
BEGIN
while cont>0
LOOP
SELECT INTO equipo nom_equipo FROM equipos AS E WHERE E.num_eqpo=cont;
SELECT INTO partidos_jug COUNT(*) FROM partidos as P WHERE (P.num_eqpo_loc=cont OR P.num_eqpo_vis=cont);
SELECT INTO partidos_gana COUNT(*) FROM partidos AS P WHERE (P.num_eqpo_loc=cont AND P.goles_loc>P.goles_vis OR P.num_eqpo_vis=cont AND P.goles_vis>P.goles_loc);
SELECT INTO partidos_emp COUNT(*) FROM partidos AS P WHERE (P.num_eqpo_loc=cont AND P.goles_loc=P.goles_vis OR P.num_eqpo_vis=cont AND P.goles_loc=P.goles_vis);
SELECT INTO partidos_perd COUNT(*) FROM partidos as P WHERE ( (P.num_eqpo_loc=cont AND P.goles_loc<P.goles_vis) OR (P.num_eqpo_vis=cont AND P.goles_loc>P.goles_vis));
SELECT INTO puntos partidos_emp*1 + partidos_gana*3;
SELECT INTO goles_favor SUM(goles_loc) FROM partidos as P WHERE P.num_eqpo_loc=cont + (SELECT SUM(goles_vis) FROM partidos as P WHERE P.num_eqpo_vis=cont);
cont:= cont - 1;
END LOOP;
RETURN NEXT ;
END;
$BODY$ LANGUAGE plpgsql STABLE;
I want the function to show my desired Output & I also want the table 'General' to have the same values from the desired output.
With this function I just get:
I don't know how to see the desired content as I just get the first row of data.
I also wonder how to Insert from the table returned by the fuction to the existing table called General.
Edit: I have also tried with:
CREATE OR REPLACE FUNCTION sp_tablageneral () RETURNS TABLE (
equipo character varying(30)
, partidos_jug int
, partidos_gana int
, partidos_emp int
, partidos_perd int
, puntos int
, goles_favor int) AS
$BODY$
DECLARE cont int:= (SELECT count(num_eqpo)FROM equipos);
r partidos%ROWTYPE;
BEGIN
while cont>0
LOOP
SELECT INTO equipo nom_equipo FROM equipos AS E WHERE E.num_eqpo=cont;
SELECT INTO partidos_jug COUNT(*) FROM partidos as P WHERE (P.num_eqpo_loc=cont OR P.num_eqpo_vis=cont);
SELECT INTO partidos_gana COUNT(*) FROM partidos AS P WHERE (P.num_eqpo_loc=cont AND P.goles_loc>P.goles_vis OR P.num_eqpo_vis=cont AND P.goles_vis>P.goles_loc);
SELECT INTO partidos_emp COUNT(*) FROM partidos AS P WHERE (P.num_eqpo_loc=cont AND P.goles_loc=P.goles_vis OR P.num_eqpo_vis=cont AND P.goles_loc=P.goles_vis);
SELECT INTO partidos_perd COUNT(*) FROM partidos as P WHERE ( (P.num_eqpo_loc=cont AND P.goles_loc<P.goles_vis) OR (P.num_eqpo_vis=cont AND P.goles_loc>P.goles_vis));
SELECT INTO puntos partidos_emp*1 + partidos_gana*3;
SELECT INTO goles_favor SUM(goles_loc) FROM partidos as P WHERE P.num_eqpo_loc=cont + (SELECT SUM(goles_vis) FROM partidos as P WHERE P.num_eqpo_vis=cont);
SELECT equipo, partidos_jug , partidos_gana, partidos_emp , partidos_perd , puntos , goles_favor INTO equipo,partidos_jug,partidos_gana,partidos_emp,partidos_perd,puntos,goles_favor FROM general;
cont:= cont - 1;
END LOOP;
RETURN NEXT ;
END;
$BODY$ LANGUAGE plpgsql STABLE;
But I get:
ERROR: the reference to the column "equipo" is ambiguous
LINE 1: SELECT equipo , partidos_jug, partidos_gana, partidos_emp ...
            ^
********** Error **********
ERROR: the reference to the column "equipo" is ambiguous
SQL state: 42702
Detail: It could refer either to a variable PL / pgSQL as a column in a table.
Context: PL / pgSQL sp_tablageneral () function on line 17 in SQL statement
Any help would be amazing.
Thanks in advance!

You can solve this issue in pure SQL, you don't need a function for this.
The best thing is to break the collection of statistics into two distinct queries, one for when the team plays at home, one when they play away. For each game calculate the points and the goals scored. Then UNION those two queries and use that as a sub-query to calculate the overall stats:
SELECT
eq.nom_equipo AS equipo,
COUNT(p.*) AS partidos_jug,
SUM(CASE WHEN p.puntos = 3 THEN 1 ELSE 0 END) partidos_gana,
SUM(CASE WHEN p.puntos = 1 THEN 1 ELSE 0 END) partidos_emp,
SUM(CASE WHEN p.puntos = 0 THEN 1 ELSE 0 END) partidos_perd,
SUM(p.puntos) AS puntos,
SUM(p.goles) AS goles_favor
FROM equipos eq
JOIN (
-- Playing at home
SELECT
num_eqpo_loc AS eqpo,
CASE WHEN (goles_loc > goles_vis) THEN 3
WHEN (goles_loc = goles_vis) THEN 1
ELSE 0
END AS puntos,
goles_loc AS goles
FROM partidos
UNION
-- Playing away
SELECT
num_eqpo_vis AS eqpo,
CASE WHEN (goles_vis > goles_loc) THEN 3
WHEN (goles_vis = goles_loc) THEN 1
ELSE 0
END AS puntos,
goles_vis AS goles
FROM partidos) AS p ON p.eqpo = eq.num_eqpo
GROUP BY equipo
ORDER BY puntos DESC, partidos_jug ASC, goles_favor DESC;
This is not particularly fast due to the CASE statements, but it will be faster than using a procedure and a loop.
Instead of putting the result of this query into a table, I would suggest that you CREATE VIEW general AS ... with the above query. In that case you always get the latest results when you SELECT * FROM general and you don't have to TRUNCATE the general table before running the query (adding new results with data in the table will violate the PK constraint). If you really need the table then use SELECT ... INTO general FROM ... in the query above.

Related

Is it possible to find duplicating records in two columns simultaneously in PostgreSQL?

I have the following database schema (oversimplified):
create sequence partners_partner_id_seq;
create table partners
(
partner_id integer default nextval('partners_partner_id_seq'::regclass) not null primary key,
name varchar(255) default NULL::character varying,
company_id varchar(20) default NULL::character varying,
vat_id varchar(50) default NULL::character varying,
is_deleted boolean default false not null
);
INSERT INTO partners(name, company_id, vat_id) VALUES('test1','1010109191191', 'BG1010109191192');
INSERT INTO partners(name, company_id, vat_id) VALUES('test2','1010109191191', 'BG1010109191192');
INSERT INTO partners(name, company_id, vat_id) VALUES('test3','3214567890102', 'BG1010109191192');
INSERT INTO partners(name, company_id, vat_id) VALUES('test4','9999999999999', 'GE9999999999999');
I am trying to figure out how to return test1, test2 (because the company_id column value duplicates vertically) and test3 (because the vat_id column value duplicates vertically as well).
To put it in other words - I need to find duplicating company_id and vat_id records and group them together, so that test1, test2 and test3 would be together, because they duplicate by company_id and vat_id.
So far I have the following query:
SELECT *
FROM (
SELECT *, LEAD(row, 1) OVER () AS nextrow
FROM (
SELECT *, ROW_NUMBER() OVER (w) AS row
FROM partners
WHERE is_deleted = false
AND ((company_id != '' AND company_id IS NOT null) OR (vat_id != '' AND vat_id IS NOT NULL))
WINDOW w AS (PARTITION BY company_id, vat_id ORDER BY partner_id DESC)
) x
) y
WHERE (row > 1 OR nextrow > 1)
AND is_deleted = false
This successfully shows all company_id duplicates, but does not appear to show vat_id ones - test3 row is missing. Is this possible to be done within one query?
Here is a db-fiddle with the schema, data and predefined query reproducing my result.
You can do this with recursion, but depending on the size of your data you may want to iterate, instead.
The trick is to make the name just another match key instead of treating it differently than the company_id and vat_id:
create table partners (
partner_id integer generated always as identity primary key,
name text,
company_id text,
vat_id text,
is_deleted boolean not null default false
);
insert into partners (name, company_id, vat_id) values
('test1','1010109191191', 'BG1010109191192'),
('test2','1010109191191', 'BG1010109191192'),
('test3','3214567890102', 'BG1010109191192'),
('test4','9999999999999', 'GE9999999999999'),
('test5','3214567890102', 'BG8888888888888'),
('test6','2983489023408', 'BG8888888888888')
;
I added a couple of test cases and left in the lone partner.
with recursive keys as (
select partner_id,
array['n_'||name, 'c_'||company_id, 'v_'||vat_id] as matcher,
array[partner_id] as matchlist,
1 as size
from partners
), matchers as (
select *
from keys
union all
select p.partner_id, c.matcher,
p.matchlist||c.partner_id as matchlist,
p.size + 1
from matchers p
join keys c
on c.matcher && p.matcher
and not p.matchlist #> array[c.partner_id]
), largest as (
select distinct sort(matchlist) as matchlist
from matchers m
where not exists (select 1
from matchers
where matchlist #> m.matchlist
and size > m.size)
-- and size > 1
)
select *
from largest
;
matchlist
{1,2,3,5,6}
{4}
fiddle
EDIT UPDATE
Since recursion did not perform, here is an iterative example in plpgsql that uses a temporary table:
create temporary table match1 (
partner_id int not null,
group_id int not null,
matchkey uuid not null
);
create index on match1 (matchkey);
create index on match1 (group_id);
insert into match1
select partner_id, partner_id, md5('n_'||name)::uuid from partners
union all
select partner_id, partner_id, md5('c_'||company_id)::uuid from partners
union all
select partner_id, partner_id, md5('v_'||vat_id)::uuid from partners;
do $$
declare _cnt bigint;
begin
loop
with consolidate as (
select group_id,
min(group_id) over (partition by matchkey) as new_group_id
from match1
), minimize as (
select group_id, min(new_group_id) as new_group_id
from consolidate
group by group_id
), doupdate as (
update match1
set group_id = m.new_group_id
from minimize m
where m.group_id = match1.group_id
and m.new_group_id != match1.group_id
returning *
)
select count(*) into _cnt from doupdate;
if _cnt = 0 then
exit;
end if;
end loop;
end;
$$;
updated fiddle

PgSQL function returning table and extra data computed in process

In PgSQL I make huge select, and then I want count it's size and apply some extra filters.
execute it twice sound dumm,
so I wrapped it in function
and then "cache" it and return union of filtered table and extra row at the end where in "id" column store size
with q as (select * from myFunc())
select * from q
where q.distance < 400
union all
select count(*) as id, null,null,null
from q
but it also doesn't look like proper solution...
and so the question: is in pg something like "generator function" or any other stuff that can properly solve this ?
postgreSQL 13
myFunc aka "selectItemsByRootTag"
CREATE OR REPLACE FUNCTION selectItemsByRootTag(
in tag_name VARCHAR(50)
)
RETURNS table(
id BIGINT,
name VARCHAR(50),
description TEXT,
/*info JSON,*/
distance INTEGER
)
AS $$
BEGIN
RETURN QUERY(
WITH RECURSIVE prod AS (
SELECT
tags.name, tags.id, tags.parent_tags
FROM
tags
WHERE tags.name = (tags_name)
UNION
SELECT c.name, c.id , c.parent_tags
FROM
tags as c
INNER JOIN prod as p
ON c.parent_tags = p.id
)
SELECT
points.id,
points.name,
points.description,
/*points.info,*/
points.distance
from points
left join tags on points.tag_id = tags.id
where tags.name in (select prod.name from prod)
);
END;
$$ LANGUAGE plpgsql;
as a result i want see maybe set of 2 table or generator function that yield some intermediate result not shure how exacltly it should look
demo
CREATE OR REPLACE FUNCTION pg_temp.selectitemsbyroottag(tag_name text, _distance numeric)
RETURNS TABLE(id bigint, name text, description text, distance numeric, count bigint)
LANGUAGE plpgsql
AS $function$
DECLARE _sql text;
BEGIN
_sql := $p1$WITH RECURSIVE prod AS (
SELECT
tags.name, tags.id, tags.parent_tags
FROM
tags
WHERE tags.name ilike '%$p1$ || tag_name || $p2$%'
UNION
SELECT c.name, c.id , c.parent_tags
FROM
tags as c
INNER JOIN prod as p
ON c.parent_tags = p.id
)
SELECT
points.id,
points.name,
points.description,
points.distance,
count(*) over ()
from points
left join tags on points.tag_id = tags.id
where tags.name in (select prod.name from prod)
and points.distance > $p2$ || _distance
;
raise notice '_sql: %', _sql;
return query execute _sql;
END;
$function$
You can call it throug following way
select * from pg_temp.selectItemsByRootTag('test',20);
select * from pg_temp.selectItemsByRootTag('test_8',20) with ORDINALITY;
The 1 way to call the function, will have a row of total count total number of rows. Second way call have number of rows plus a serial incremental number.
I also make where q.distance < 400 into function input argument.
selectItemsByRootTag('test',20); means that q.distance > 20 and tags.name ilike '%test%'.

Can I use PostgreSQL function in join?

Can I use PostgreSQL function in join? also can I use cursor function in join?
actually this is what I want
Select m.* from Medication m
Left Join public.GetResidentMedications(, , , , , ,) f on f.Id= m.Id
same with the cursor functions?
**Below is my function which is a cursor function and I want to join it **
CREATE OR REPLACE FUNCTION public."GetResidentMedications" (
ref refcursor,
_alfid integer,
_residentid integer,
_types varchar,
_limits integer,
_offsets integer
)
RETURNS refcursor AS
$body$
BEGIN
open ref for
-- select * from public."GetResidentMedications"('refcursor', 25, 331, '' , 20, 1)
with cte AS (
select m."Id"
from public."Medication" m
where m."AlfId" = _AlfId
and (m."ResidentId" = _ResidentId or coalesce (_ResidentId, 0) = 0)
and 1 = (
case when
'IsVerified' = ANY ('{IsVerifiedsss, IsVerifieds}') and m."IsVerified" = true then 1 else 0 end
)
)
select * from (
table cte order BY "Id" limit _limits offset _offsets
) sub
Left join (select count("Id") from cte) c(TotalRecords) on true;
return ref;
END;
$body$
LANGUAGE 'plpgsql'
VOLATILE
CALLED ON NULL INPUT
SECURITY INVOKER
PARALLEL UNSAFE
COST 100;
is it possible to do it?
Make your function a set-returning function by defining it as "returns table (...)". The ref cursor is not needed.
CREATE OR REPLACE FUNCTION public."GetResidentMedications" (
_alfid integer,
_residentid integer,
_types varchar,
_limits integer,
_offsets integer
)
RETURNS table (id integer, ... other columns ... )
as
$body$
with cte AS (
select m."Id"
from public."Medication" m
where m."AlfId" = _AlfId
and (m."ResidentId" = _ResidentId or coalesce (_ResidentId, 0) = 0)
and 1 = (
case when
'IsVerified' = ANY ('{IsVerifiedsss, IsVerifieds}') and m."IsVerified" = true then 1 else 0 end
)
)
select *
from (
table cte order BY "Id" limit _limits offset _offsets
) sub
Left join (select count("Id") from cte) c(TotalRecords) on true;
$body$
LANGUAGE sql
stable;
then you can use it like this:
select ...
from some_table
left join "GetResidentMedications"(1,2,'bla',10,200) as g on g.id = ...;

How to use a recursive query inside of a function?

I have a working recursive function down below that returns the hierarchy of a .Net Element, in this example 'ImageBrush'
WITH RECURSIVE r AS (
SELECT id, name, dependent_id, 1 AS level
FROM dotNetHierarchy
WHERE name = 'ImageBrush'
UNION ALL
SELECT g.id, lpad(' ', r.level) || g.name, g.dependent_id, r.level + 1
FROM dotNetHierarchy g JOIN r ON g.id = r.dependent_id
)
SELECT name FROM r;
Now I want to use it inside a function, where the input is a text like 'ImageBrush' which gets inserted in the WHERE statement of the recursive query.
The table dotNetHierarchy has 3 columns: id, name, dependent_id
The following didn´t work with PgAdmin just querying forever without an error:
CREATE OR REPLACE FUNCTION get_hierarchy(inp text) RETURNS
TABLE (id int, name text, id int, lev int) AS
$BODY$
DECLARE
BEGIN
WITH RECURSIVE r AS (
SELECT id, name, dependent_id, 1 AS level
FROM dotNetHierarchy
WHERE name = inp
UNION ALL
SELECT g.id, lpad(' ', r.level) || g.name, g.dependent_id, r.level + 1
FROM dotNetHierarchy g JOIN r ON g.id = r.dependent_id
)
SELECT name FROM r;
return;
END
$BODY$
LANGUAGE plpgsql;
I tried searching google for using recursive querys in functions, but the few results proved not to be working.
I would appreciate any help, thanks in advance.
Your function doesn't return anything. You would at least need a return query, but it's more efficient to use a language sql function to encapsulate a query:
CREATE OR REPLACE FUNCTION get_hierarchy(inp text)
RETURNS TABLE (id int, name text, id int, lev int) AS
$BODY$
WITH RECURSIVE r AS (
SELECT id, name, dependent_id, 1 AS level
FROM dotNetHierarchy
WHERE name = inp
UNION ALL
SELECT g.id, lpad(' ', r.level) || g.name, g.dependent_id, r.level + 1
FROM dotNetHierarchy g JOIN r ON g.id = r.dependent_id
)
SELECT name
FROM r;
$BODY$
LANGUAGE sql;

Select [COLUMN_NAME] AS, self referencing table

The following is my function get_reportees performed on the self referencing table emp_tabref1
CREATE OR REPLACE FUNCTION get_reportees4(IN id integer)
RETURNS TABLE(e_id integer, e_name character varying, e_manager integer, e_man_name character varying) AS
$$
BEGIN
RETURN QUERY
WITH RECURSIVE manger_hierarchy(e_id, e_name, m_id, m_name) AS
(
SELECT e.emp_id, e.emp_name, e.mgr_id, e.emp_name AS man_name
FROM emp_tabref1 e WHERE e.emp_id = id
UNION
SELECT rp.emp_id, rp.emp_name, rp.mgr_id, rp.emp_name AS man_name
FROM manger_hierarchy mh INNER JOIN emp_tabref1 rp ON mh.e_id = rp.mgr_id
)
SELECT * from manger_hierarchy;
END;
$$ LANGUAGE plpgsql VOLATILE
Table structure of emp_tabref1:
CREATE TABLE **emp_tabref1**
(
emp_id integer NOT NULL,
emp_name character varying(50) NOT NULL,
mgr_id integer,
CONSTRAINT emp_tabref_pkey PRIMARY KEY (emp_id),
CONSTRAINT emp_tabref_mgr_id_fkey FOREIGN KEY (mgr_id)
REFERENCES emp_tabref (emp_id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
)
What I want returned is the hierarchy (both above and below) of the id that we are passing which will have the emp_name, emp_id, mgr_id and mgr_name.
But my function is returning like this:
select * from get_reportees4(9)
e_id e_name e_manager e_man_name
1 9 "Emp9" 10 "Emp9"
2 5 "Emp5" 9 "Emp5"
3 6 "Emp6" 9 "Emp6"
where my expected output is
e_id e_name e_manager e_man_name
1 9 "Emp9" 10 "Emp10"
2 5 "Emp5" 9 "Emp9"
3 6 "Emp6" 9 "Emp9"
The function should return the manager name and not the employee name. Please help!
Found a solution! By creating a new join between the temporary manger_hierarchy table and the emp_tabref1 table using mgr_id and emp_id
CREATE OR REPLACE FUNCTION get_reportees4(IN id integer)
RETURNS TABLE(e_id integer, e_name character varying, e_manager integer, e_man_name character varying) AS
$$
BEGIN
RETURN QUERY
WITH RECURSIVE manger_hierarchy(e_id, e_name, m_id, m_name) AS
(
SELECT e.emp_id, e.emp_name, e.mgr_id, e.emp_name AS man_name
FROM emp_tabref1 e WHERE e.emp_id = id
UNION
SELECT rp.emp_id, rp.emp_name, rp.mgr_id, rp.emp_name AS man_name
FROM manger_hierarchy mh INNER JOIN emp_tabref1 rp ON mh.e_id = rp.mgr_id
)
SELECT manger_hierarchy.e_id, manger_hierarchy.e_name, manger_hierarchy.m_id, emp_tabref1.emp_name
FROM manger_hierarchy LEFT JOIN emp_tabref1 ON manger_hierarchy.m_id = emp_tabref1.emp_id;
END;
$$ LANGUAGE plpgsql VOLATILE
SELECT manger_hierarchy.e_id, manger_hierarchy.e_name, manger_hierarchy.m_id, emp_tabref1.emp_name
FROM manger_hierarchy LEFT JOIN emp_tabref1 ON manger_hierarchy.m_id = emp_tabref1.emp_id;