-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcreate.sql
More file actions
68 lines (55 loc) · 1.58 KB
/
create.sql
File metadata and controls
68 lines (55 loc) · 1.58 KB
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
drop table budget;
drop table func;
drop table org;
drop table econ;
create table func (
id varchar(40) PRIMARY KEY not null,
parent_id varchar(40) references func(id),
value text not null,
tags text
);
create table econ (
id varchar(40) PRIMARY KEY not null,
parent_id varchar(40) references econ(id),
value text not null
);
create table org (
id varchar(40) PRIMARY KEY not null,
parent_id varchar(40) references org(id),
value text not null
);
create table budget(
id varchar(40) PRIMARY KEY not null,
func_id varchar(40) not null references func(id),
econ_id varchar(40) not null references econ(id),
org_id varchar(40) not null references org(id),
date_start date not null,
date_end date,
amount numeric not null,
comm text,
tags text
);
create view func_tree as
select
fff.value || ' (' || fff.id || ')' parent_parent_cofog_name,
ff.value || ' (' || ff.id || ')' parent_cofog_name ,
f.value || ' (' || f.id || ')' cofog_name ,
b.amount
from budget b
inner join func f on (f.id = b.func_id)
left outer join func ff on (ff.id = f.parent_id)
left outer join func fff on (fff.id = ff.parent_id)
where amount > 0
;
create view econ_tree as
select
eee.value || ' (' || eee.id || ')' parent_parent_econ_name,
ee.id ||' - '||ee.value || ' (' || ee.id || ')' parent_econ_name,
e.value || ' (' || e.id || ')' econ_name ,
b.amount
from budget b
inner join econ e on (e.id = b.econ_id)
left outer join econ ee on (ee.id = e.parent_id)
left outer join econ eee on (eee.id = ee.parent_id)
where amount > 0;
-- copy (select * from econ_tree) to '/tmp/postgres_export/econ_tree.csv' with csv