-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypicalOut.sql
264 lines (223 loc) · 7.14 KB
/
typicalOut.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
--select c1, a1, sum(预算数) as 预算数 from codes group by c1, a1;
--select c2, a2, sum(预算数) as 预算数 from codes group by c2,a2;
select distinct(c2) from codes where c2 like '501%' order by c2;
select distinct(c3) from codes where c3 like '501205%' order by c3;
select a2, sum(预算数) as 预算数 from codes where c2='501201' group by a2
union all
select a2, sum(预算数) as 预算数 from codes where c2='501203' group by a2;
select a2, sum(预算数) as 预算数 from codes where c2='501201' group by a2
union all
select a3, sum(预算数) as 预算数 from codes where c3 like '501201%' group by a3;
--setof codes
-- http://stackoverflow.com/questions/27671140/
--error-return-type-mismatch-in-function-declared-to-return
--returns后边的类型必须完全和返回格式匹配,看看这儿stackOverflow上说的!
--table(a2 varchar,预算数 double precision)
create type level_out as (a2 varchar,预算数 double precision);
create or replace function level_data_2_3(level2 varchar(10))
returns setof level_out
as $body$
select a2, sum(预算数) as 预算数 from codes where c2=level2 group by a2
union all
select a3, sum(预算数) as 预算数 from codes where c3 like level2 || '%' group by a3;
$body$
language 'sql';
create or replace function union_data(level_1 varchar(10))
returns setof level_out
as $body$
declare
level_2 varchar(10);
set_data level_out;
flag integer := 1;
begin
for level_2 in (select distinct(c2) from codes where c2 like level_1 || '%' order by c2) loop
raise notice 'level_2 %',level_2;
-- if flag > 1 then
raise notice '----here is if statement,flag is %',flag;
raise notice 'set_data is %',set_data;
raise notice 'set_data is %',set_data;
--set_data = set_data union all
return query select * from level_data_2_3(level_2);
-- set_data = query(select * level_data_2_3(level_2));
-- else
-- select into set_data * from level_data_2_3(level_2);
-- end if;
-- flag = flag + 1;
end loop;
return;
/*
return query
select * from level_data_2_3(level_2) where level_2 in (select distinct(c2) from codes where c2 like level_1 || '%' order by c2);
*/
-- return;
end;
$body$
language 'plpgsql';
--==============================
create or replace function union_data(level_1 varchar(10))
returns setof level_out
as $body$
declare
level_2 varchar(10);
begin
for level_2 in (select distinct(c2) from codes where c2 like level_1 || '%' order by c2) loop
raise notice 'level_2 %',level_2;
return query select * from level_data_2_3(level_2);
end loop;
return;
end;
$body$
language 'plpgsql';
--===============================sql
-----------------------------------
create or replace function union_data(level_1 varchar(10))
returns setof level_out
as $body$
declare
level_2 varchar(10);
set_data level_out;
begin
return query select * from level_data_2_3(level_2);
return;
end;
$body$
language 'plpgsql' volatile;
select a1, sum(预算数) as 预算数 from codes group by a1
union all
select * from union_data('501');
--alter table codes alter column c1 type varchar(10);
--alter table codes alter column c2 type varchar(10);
--alter table codes alter column c3 type varchar(10);
--select c2,a2,sum(预算数) as 预算数 from codes where c2 in (select distinct(c2) from codes where c2 like '501%' order by c2) group by c2,a2 order by c2;
--返回结果集
-- returns setof table_name
-- returns recursor
--简洁的sql方式是这样的,plpgsql方式要不使用query要不就只能只用遍历的方式了!!!
create or replace function datas()
returns setof employee as
$BODY$
--begin
select * from employee ;
--end;
$BODY$
language 'sql';
--然后发现这样写是不行的,要这样写!!!
create or replace function datas() returns setof employee as $$
begin
return query select * from employee;
end;
$$ language 'plpgsql';
create or replace function datas() returns setof record as $$
declare
rec record;
begin
for rec in select * from employee loop
return next rec;
end loop;
-- return null;
end;
$$ language 'plpgsql';
--这种方式需要指定返回的table的格式
-- 两种调用方式:
--1. select * from datas();
--2. select datas();
---------------
CREATE OR REPLACE FUNCTION skytf.func_test_result_single ( in_id integer)
RETURNS SETOF varchar as
$$
DECLARE
v_name varchar;
BEGIN
for v_name in ( (select name from test_result1 where id = in_id) union (select name from test_result2 where id = in_id) )loop
RETURN NEXT v_name;
end loop;
return;
END;
$$
LANGUAGE PLPGSQL;
------------------==================
--http://www.codeweblog.com/postgresq
--l%E5%87%BD%E6%95%B0%E5%A6%82%E4%BD%95%E8%BF%94%E5%9B%9E%E6%95%B0%E6%8D%AE%E9%9B%86/
create type dept_salary as (departmentid int, totalsalary int);
create or replace function f_dept_salary()
returns setof dept_salary
as
$$
declare
rec dept_salary%rowtype;
begin
for rec in select departmentid, sum(salary) as totalsalary from f_get_employee() group by departmentid loop
return next rec;
end loop;
return;
end;
$$
language 'plpgsql';
--用Out传出的方式
create or replace function f_dept_salary_out(out o_dept text,out o_salary text)
returns setof record as
$$
declare
v_rec record;
begin
for v_rec in select departmentid as dept_id, sum(salary) as total_salary from f_get_employee() group by departmentid loop
o_dept:=v_rec.dept_id;
o_salary:=v_rec.total_salary;
return next;
end loop;
end;
$$
language plpgsql;
--执行结果:
--postgres=# select * from f_dept_salary();
--departmentid | totalsalary
---------------+-------------
-- 1 | 80000
-- 3 | 120000
-- 2 | 60000
--postgres=# select * from f_dept_salary_out();
--o_dept | o_salary
--------+----------
--1 | 80000
--3 | 120000
--2 | 60000
--根据执行函数变量不同返回不同数据集
create or replace function f_get_rows(text) returns setof record as
$$
declare
rec record;
begin
for rec in EXECUTE 'select * from ' || $1 loop
return next rec;
end loop;
return;
end
$$
language 'plpgsql';
--Version 1.0 succeed!
create type level_out as (a2 varchar,预算数 double precision);
create or replace function level_data_2_3(level2 varchar(10))
returns setof level_out
as $body$
select a2, sum(预算数) as 预算数 from codes where c2=level2 group by a2
union all
select a3, sum(预算数) as 预算数 from codes where c3 like level2 || '%' group by a3;
$body$
language 'sql';
create or replace function union_data(level_1 varchar(10))
returns setof level_out
as $body$
declare
level_2 varchar(10);
begin
for level_2 in (select distinct(c2) from codes where c2 like level_1 || '%' order by c2) loop
raise notice 'level_2 %',level_2;
return query select * from level_data_2_3(level_2);
end loop;
return;
end;
$body$
language 'plpgsql';
select a1, sum(预算数) as 预算数 from codes group by a1
union all
select * from union_data('501');