powered by simpleCommunicator - 2.0.61     © 2026 Programmizd 02
Целевая тема:
Создать новую тему:
Автор:
Закрыть
Цитировать
Форумы / PostgreSQL [игнор отключен] [закрыт для гостей] / RECURSIVE function
9 сообщений из 9, страница 1 из 1
RECURSIVE function
    #39165506
Asew
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
Собственно вопрос в следующем коллеги по цеху:

делаю рекурсивный запрос, в результате запроса на экран ничего не выводится. Т.е. курсор создается в контексте функции, но он пустой. Отдельно проверял tabledata, на экран данные какие-то выводятся...а значит я не так сформировал рекурсию. Прошу прокомментировать.
with recursive QW AS (
with tabledata as (
SELECT u.sn_index,
(CASE
WHEN coalesce(100,0)=0 then u.hi_sn_index
ELSE
(CASE WHEN u.sn_type=2 THEN null
ELSE u.hi_sn_index
END)
END) as hi_sn_index,
u.name,
u.nn,
a.cl_rezult
from
(SELECT sum(coalesce(a.cl_rezult,0)) as cl_rezult,
u.sn_index
from
((with recursive fg_tree as (
select sn_index,
sn_index as objindex
from n.usn
where coalesce(d,0) = 0
union all
select c.sn_index,
p.objindex
from n.usn as c
join fg_tree p
on p.sn_index = hi_sn_index
)
select *from fg_tree )as u
LEFT JOIN(select c.cl_objindex,sum(round(cast(c.cl_rezult as numeric),3)) as cl_rezult
from d.dc_calculate as c, D.dc_typecalculate as t
where coalesce(Cl_validate,0) =1 and t.tc_index = c.tc_index
and c.cl_date = timestamp '2015-08-25' and c.ta_index =1 and c.tp_index
and ((c.lpu_index = 100 ) or (coalesce(100,0) =0)) --iLPU_INDEX = 100
group by cl_objindex) as a ON (u.objindex =a.cl_objindex))


group by u.sn_index) as A, n.usn as u
where a.sn_index = u.sn_index and coalesce(u.d,0)=0
and ((u.lpu_index = 100 ) or (coalesce(100,0) =0))

union all

select t.tc_property*10000000000000+c.cl_objindex ,c.cl_objindex, g.gp_name,g.gp_nn,
sum(round(coalesce(cast (cl_rezult as numeric),0),3)) as cl_rezult
from d.dc_calculate as c, D.dc_typecalculate as t , n.gts_property as g
where coalesce(Cl_validate,0) =1 and t.tc_index = c.tc_index and g.gp_index = t.tc_property
and c.cl_date = timestamp '2015-08-25'
and c.ta_index =1 and c.tp_index =20
and ((c.lpu_index = 100 ) or (coalesce(100,0) =0))
group by cl_objindex,t.tc_property, g.gp_name,g.gp_nn
union all
select 2000000000000000*c.tc_index+t.tc_property*10000000000+c.cl_objindex,t.tc_property*10000000000000+c.cl_objindex , t.tc_name, tc_nn
, sum(round(coalesce(cast(cl_rezult as numeric),0),3)) as cl_rezult
from d.dc_calculate as c, D.dc_typecalculate as t
where coalesce(Cl_validate,0) =1 and t.tc_index = c.tc_index
and c.cl_date = timestamp '2015-08-25'
and 0 >=1 and c.ta_index =1 and c.tp_index =20
and ((c.lpu_index = 100 ) or (coalesce(100,0) =0))
group by cl_objindex,c.tc_index ,t.tc_property,t.tc_name, tc_nn)
(select 1 as level,
sn_index,
hi_sn_index,
WWW_REPORT.html_table(WWW_REPORT.html_trtd(to_char(cl_rezult,'9999999999999990.999'))) as q,
ARRAY[nn] as path

from tabledata
where cl_rezult <>0 and coalesce(hi_sn_index::text, '') = ''
order by nn)

UNION ALL

(select qw.level+1 as level,
Z.sn_index,
Z.hi_sn_index,
WWW_REPORT.html_table(WWW_REPORT.html_trtd(to_char(Z.cl_rezult,'9999999999999990.999'))) as q,
qw.path||z.nn
from tabledata as Z
JOIN QW ON QW.sn_index = Z.hi_sn_index
ORDER BY NN))

select row_number () over () as N_num, f.*
from (select WWW_REPORT.html_space((level-1)*5)||name as name,
qw.*
from qw, n.usn
order by path) as f;


END;

$BODY$
LANGUAGE plpgsql;
...
Рейтинг: 0 / 0
RECURSIVE function
    #39165567
qwwq
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
Asew,

"Ни мне, Сара, здравствуйте, ни тебе, Сара, до свидания" (сс)

а где шапка ф--ии, и что сказано "во первыз строках ея письма" ?
...
Рейтинг: 0 / 0
RECURSIVE function
    #39166221
mad_nazgul
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
Asew,

Читайте документацию по plpgsql
Судя по вашей функции PostgreSQL в принципе не должен что-то выводить.
...
Рейтинг: 0 / 0
RECURSIVE function
    #39166385
Asew
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
qwwqAsew,

"Ни мне, Сара, здравствуйте, ни тебе, Сара, до свидания" (сс)

а где шапка ф--ии, и что сказано "во первыз строках ея письма" ?

а зачем она ? ведь водится что-то или нет можно и простым селектом отдельно tabledata, а потом в составе рекурсивного запроса, где к ней идет обращение.Я сказал, что данные выводятся из tabledata, рекурсия неверно работает...
...
Рейтинг: 0 / 0
RECURSIVE function
    #39166390
Asew
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
mad_nazgulAsew,

Читайте документацию по plpgsql
Судя по вашей функции PostgreSQL в принципе не должен что-то выводить.

Спасибо кЭП=) я и написал, что ничего не выводит, неверна сама рекурсивная часть запроса.
...
Рейтинг: 0 / 0
RECURSIVE function
    #39166455
qwwq
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
AsewqwwqAsew,

"Ни мне, Сара, здравствуйте, ни тебе, Сара, до свидания" (сс)

а где шапка ф--ии, и что сказано "во первыХ строках ея письма" ?

а зачем она ? ведь водится что-то или нет можно и простым селектом отдельно tabledata, а потом в составе рекурсивного запроса, где к ней идет обращение.Я сказал, что данные выводятся из tabledata, рекурсия неверно работает...ну, мне например было бы интересно посмотреть, нет ли там конструкции RETURN QUERY; или вы, какабычна делают люди, метко охарактеризованные дипломатичнешим дипломатом всех времен и народов -- лаврушкой как "де'Биллы, ять" путаете "LANGUAGE plpgsql" с "LANGUAGE sql"

-- там разные структуры отвечают за вывод. Поверьте на слово.

а как работает рекурсия можно выяснить безо всякой ф--ии. если нужен параметр-другой -- PREPARE + EXECUTE -- наше всё.
...
Рейтинг: 0 / 0
RECURSIVE function
    #39166459
qwwq
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
AsewСобственно вопрос в следующем коллеги по цеху:

делаю рекурсивный запрос, в результате запроса на экран ничего не выводится. Т.е. курсор создается в контексте функции, но он пустой. Отдельно проверял tabledata, на экран данные какие-то выводятся...а значит я не так сформировал рекурсию. Прошу прокомментировать.

Код: sql
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.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
81.
82.
83.
84.
85.
86.
87.
88.
89.
90.
91.
92.
93.
with recursive QW AS ( 
with tabledata as (
SELECT u.sn_index, 
(CASE
WHEN coalesce(100,0)=0 then u.hi_sn_index
ELSE
(CASE WHEN u.sn_type=2 THEN null 
ELSE u.hi_sn_index 
END)
END) as hi_sn_index,
u.name,
u.nn,
a.cl_rezult
from
		(SELECT sum(coalesce(a.cl_rezult,0)) as cl_rezult, 
						         u.sn_index
		from
		((with recursive fg_tree as (
				select sn_index, 
				sn_index as objindex 
				from n.usn
				where coalesce(d,0) = 0 
				union all
				select c.sn_index, 
					p.objindex
				from n.usn as c 
				join fg_tree p
				on p.sn_index = hi_sn_index 
		) 
		select *from fg_tree )as u
		LEFT JOIN(select c.cl_objindex,sum(round(cast(c.cl_rezult as numeric),3)) as cl_rezult
		from d.dc_calculate as c, D.dc_typecalculate as t
		where coalesce(Cl_validate,0) =1 and t.tc_index = c.tc_index
		and c.cl_date = timestamp '2015-08-25' and c.ta_index =1 and c.tp_index
		and ((c.lpu_index = 100 ) or (coalesce(100,0) =0)) --iLPU_INDEX = 100
		group by  cl_objindex) as a ON (u.objindex =a.cl_objindex))
		

	group by u.sn_index) as A, n.usn as u
	where a.sn_index = u.sn_index and coalesce(u.d,0)=0
	and ((u.lpu_index = 100 ) or (coalesce(100,0) =0)) 
	
union all

	select t.tc_property*10000000000000+c.cl_objindex ,c.cl_objindex, g.gp_name,g.gp_nn,
          sum(round(coalesce(cast (cl_rezult as numeric),0),3))  as cl_rezult
	from d.dc_calculate as c, D.dc_typecalculate as t , n.gts_property as g
        where coalesce(Cl_validate,0) =1 and t.tc_index = c.tc_index and g.gp_index = t.tc_property
          and c.cl_date = timestamp '2015-08-25'
          and c.ta_index =1 and c.tp_index =20
        and ((c.lpu_index = 100 ) or (coalesce(100,0) =0))
         group by  cl_objindex,t.tc_property, g.gp_name,g.gp_nn
union all
select 2000000000000000*c.tc_index+t.tc_property*10000000000+c.cl_objindex,t.tc_property*10000000000000+c.cl_objindex  , t.tc_name, tc_nn
     , sum(round(coalesce(cast(cl_rezult as numeric),0),3)) as cl_rezult
from d.dc_calculate as c, D.dc_typecalculate as t
        where coalesce(Cl_validate,0) =1 and t.tc_index = c.tc_index
       and c.cl_date = timestamp '2015-08-25'
        and  0 >=1 and c.ta_index =1 and c.tp_index =20
        and ((c.lpu_index = 100 ) or (coalesce(100,0) =0))
         group by  cl_objindex,c.tc_index ,t.tc_property,t.tc_name, tc_nn)
(select  				1 as level,
                                        sn_index, 
	                                hi_sn_index,
	                                WWW_REPORT.html_table(WWW_REPORT.html_trtd(to_char(cl_rezult,'9999999999999990.999'))) as q,
	                                ARRAY[nn] as path

from tabledata
where cl_rezult <>0 and coalesce(hi_sn_index::text, '') = ''
order by nn)

UNION ALL 

(select qw.level+1 as level,
        Z.sn_index, 
	Z.hi_sn_index,
	WWW_REPORT.html_table(WWW_REPORT.html_trtd(to_char(Z.cl_rezult,'9999999999999990.999'))) as q,
	qw.path||z.nn
from tabledata as Z
JOIN QW ON QW.sn_index = Z.hi_sn_index
ORDER BY NN))

select row_number () over () as N_num, f.*
from (select WWW_REPORT.html_space((level-1)*5)||name as name,
	     qw.*
from qw, n.usn
order by path) as f;


END;

$BODY$
LANGUAGE plpgsql;




-- отформатировал
...
Рейтинг: 0 / 0
RECURSIVE function
    #39166514
dreindeimos
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
qwwqAsew,

"Ни мне, Сара, здравствуйте, ни тебе, Сара, до свидания" (сс)

а где шапка ф--ии, и что сказано "во первыз строках ея письма" ?
)))
...
Рейтинг: 0 / 0
RECURSIVE function
    #39166567
Lonepsycho
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
qwwqили вы, какабычна делают люди, метко охарактеризованные дипломатичнешим дипломатом всех времен и народов -- лаврушкой как "де'Биллы, ять" :)) rofl
...
Рейтинг: 0 / 0
9 сообщений из 9, страница 1 из 1
Форумы / PostgreSQL [игнор отключен] [закрыт для гостей] / RECURSIVE function
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


Просмотр
0 / 0
Close
Debug Console [Select Text]