1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| CREATE OR REPLACE FUNCTION public.doublon(
)
RETURNS TABLE(res_period_id integer, geo_referential_item_name text, Nb_Com integer)
LANGUAGE 'plpgsql'
COST 100
VOLATILE
ROWS 1000
AS $BODY$
declare
doublon refcursor ;
begin
drop table if exists doublon ;
create table doublon (res_period_id integer, geo_referential_item_name text, Nb_Com integer);
insert into doublon (res_period_id , geo_referential_item_name , Nb_Com)
select res_period.id, geo_referential_item.name, count(geo_referential_item.id) as Nb_Com
from sale_order
left join sale_order_item on sale_order_item.order_id = sale_order.id
left join geo_referential_item on sale_order_item.item_id = geo_referential_item.id
left join codeclient on sale_order.partner_id = codeclient.cc_id
left join regroupement on regroupement.res = codeclient.parent_id
left join res_period on res_period.id = sale_order.distrib_week_id
where regroupement.res in ('283','289','321','408','363')
and sale_order.state not in ('canceled','rejected')
and res_period.id > 105
--and geo_referential_item.name ='231044003'
and sale_order.name like 'V_DP%'
group by res_period.id, geo_referential_item.name
having count(geo_referential_item.id) > 1
order by 3 desc ;
open doublon for select * from doublon ;
--
À partir de ce moment là, je souhaiterais effectuer une requête avec en paramètre res_period_id et geo_referential_item_name
select *
from .......
where res_period_id = doublon.res_period_id
and geo_referential_item_name = doublon.geo_referential_item_name
return *
end;
$BODY$;
ALTER FUNCTION public.doublon()
OWNER TO pubaudit; |
Partager