Project

General

Profile

1
DROP VIEW organizations_view;
2
DROP VIEW organizations_simple_view;
3

    
4
DROP TABLE IF EXISTS other_ids;
5
DROP TABLE IF EXISTS other_names;
6
DROP TABLE IF EXISTS acronyms;
7
DROP TABLE IF EXISTS relationships;
8
DROP TABLE IF EXISTS types;
9
DROP TABLE IF EXISTS urls;
10
DROP TABLE IF EXISTS openaire_simrels;
11
DROP TABLE IF EXISTS organizations;
12
DROP TABLE IF EXISTS org_types;
13
DROP TABLE IF EXISTS id_types;
14
DROP TABLE IF EXISTS rel_types;
15
DROP TABLE IF EXISTS simrel_types;
16

    
17
CREATE TABLE org_types (val text PRIMARY KEY);
18
INSERT INTO org_types VALUES ('Archive'), ('Company'), ('Education'), ('Facility'), ('Government'), ('Healthcare'), ('Nonprofit'), ('Other');
19

    
20
CREATE TABLE id_types (val text PRIMARY KEY);
21
INSERT INTO id_types VALUES ('CNRS'), ('FundRef'), ('HESA'), ('ISNI'), ('OrgRef'), ('UCAS'), ('UKPRN'), ('Wikidata'), ('grid.ac');
22

    
23
CREATE TABLE rel_types (val text PRIMARY KEY);
24
INSERT INTO rel_types VALUES ('Child'), ('Parent'), ('Related'), ('Other');
25

    
26
CREATE TABLE simrel_types (val text PRIMARY KEY);
27
INSERT INTO simrel_types VALUES ('suggested'), ('is_similar'), ('is_different');
28

    
29

    
30
CREATE TABLE organizations (
31
    id                text PRIMARY KEY,
32
    name              text,
33
    lat               double precision,
34
	lng               double precision,
35
	city              text,
36
	country           text,
37
	created_by        text,
38
	creation_date     timestamp with time zone DEFAULT now(),
39
	modified_by       text,
40
	modification_date timestamp with time zone DEFAULT now()
41
);
42

    
43
CREATE TABLE other_ids (
44
	id      text REFERENCES organizations(id),
45
	otherid text,
46
	type    text REFERENCES id_types(val),
47
	PRIMARY KEY (id, otherid, type)
48
);
49

    
50
CREATE TABLE other_names (
51
	id    text REFERENCES organizations(id),
52
	name  text,
53
	lang  text DEFAULT '',
54
	PRIMARY KEY (id, name, lang)
55
);
56

    
57
CREATE TABLE acronyms (
58
	id text,
59
	acronym text,
60
	PRIMARY KEY (id, acronym)
61
);
62

    
63
CREATE TABLE relationships (
64
	id1     text REFERENCES organizations(id),
65
	reltype text REFERENCES rel_types(val),
66
	id2     text REFERENCES organizations(id),
67
    PRIMARY KEY (id1, reltype, id2)
68
);
69

    
70
CREATE TABLE types (
71
	id   text REFERENCES organizations(id),
72
	type text REFERENCES org_types(val),
73
	PRIMARY KEY (id, type)
74
);
75

    
76
CREATE TABLE urls (
77
	id  text REFERENCES organizations(id),
78
	url text,
79
	PRIMARY KEY (id, url)
80
);
81

    
82
CREATE TABLE openaire_simrels (
83
	local_id         text REFERENCES organizations(id),
84
	oa_id            text,
85
	oa_original_id   text,
86
	oa_name          text,
87
	oa_acronym       text,
88
	oa_country       text,
89
	oa_url           text,
90
	oa_collectedfrom text,
91
	reltype          text  NOT NULL DEFAULT 'suggested' REFERENCES simrel_types(val),
92
	PRIMARY KEY (local_id, oa_id)
93
);
94

    
95
CREATE VIEW organizations_view AS SELECT
96
    org.id,
97
    org.name,
98
    org.lat,
99
	org.lng,
100
	org.city,
101
	org.country,
102
	org.created_by,
103
	org.creation_date,
104
	org.modified_by,
105
	org.modification_date,
106
	COALESCE(jsonb_agg(DISTINCT jsonb_build_object('id', oid.otherid, 'type', oid.type)) FILTER (WHERE oid.otherid IS NOT NULL), '[]') AS other_ids,
107
	COALESCE(jsonb_agg(DISTINCT jsonb_build_object('name', n.name, 'lang', n.lang))      FILTER (WHERE n.name      IS NOT NULL), '[]') AS other_names,
108
	COALESCE(jsonb_agg(DISTINCT a.acronym)                                               FILTER (WHERE a.acronym   IS NOT NULL), '[]') AS acronyms,	
109
	COALESCE(jsonb_agg(DISTINCT t.type::text)                                            FILTER (WHERE t.type      IS NOT NULL), '[]') AS types,
110
	COALESCE(jsonb_agg(DISTINCT u.url)                                                   FILTER (WHERE u.url       IS NOT NULL), '[]') AS urls
111
FROM
112
    organizations org
113
    LEFT OUTER JOIN other_ids oid    ON (org.id = oid.id)
114
    LEFT OUTER JOIN other_names n    ON (org.id = n.id)
115
    LEFT OUTER JOIN acronyms a       ON (org.id = a.id)
116
    LEFT OUTER JOIN types t          ON (org.id = t.id)
117
    LEFT OUTER JOIN urls u           ON (org.id = u.id)
118
GROUP BY
119
    org.id,
120
    org.name,
121
    org.lat,
122
	org.lng,
123
	org.city,
124
	org.country,
125
	org.created_by,
126
	org.creation_date,
127
	org.modified_by,
128
	org.modification_date;
129
	
130
CREATE VIEW organizations_simple_view AS SELECT
131
    org.id,
132
    org.name,
133
	org.city,
134
	org.country,
135
	array_agg(DISTINCT a.acronym)    FILTER (WHERE a.acronym IS NOT NULL) AS acronyms,	
136
	array_agg(DISTINCT t.type::text) FILTER (WHERE t.type    IS NOT NULL) AS types
137
FROM
138
    organizations org
139
    LEFT OUTER JOIN acronyms a       ON (org.id = a.id)
140
    LEFT OUTER JOIN types t          ON (org.id = t.id)
141
GROUP BY
142
    org.id,
143
    org.name,
144
	org.city,
145
	org.country;
146
	
(3-3/3)