-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDDL.sql
More file actions
207 lines (184 loc) · 6.21 KB
/
DDL.sql
File metadata and controls
207 lines (184 loc) · 6.21 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
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
-- file: schema.sql
--
-- this is the original Gallagher Microsoft SQL schema, loaded with data and
-- then selected from to create our schema from it filling it out according the guidlines we
-- set in formaer stages
--
-- this file can be executed in psql:
-- \i schema.sql
--
-- Ben Guerrier & Zack O'Rourke
-- May 2024
--the original schema
DROP TABLE Animal;
CREATE TABLE Animal (
animal_id integer primary key,
lrid integer NOT NULL default 0,
tag varchar(16) NOT NULL default '',
rfid varchar(15) NOT NULL default '',
nlis varchar(16) NOT NULL default '',
is_new integer NOT NULL default 1,
draft varchar(20) NOT NULL default '',
sex varchar(20) NOT NULL default '',
dob timestamp,
sire varchar(16) NOT NULL default '',
dam varchar(16) NOT NULL default '',
breed varchar(20) NOT NULL default '',
colour varchar(20) NOT NULL default '',
weaned integer NOT NULL default 0 ,
prev_tag varchar(10) NOT NULL default '',
prev_pic varchar(20) NOT NULL default '',
note varchar(30) NOT NULL default '',
note_date timestamp,
is_exported integer NOT NULL default 0,
is_history integer NOT NULL default 0,
is_deleted integer NOT NULL default 0,
tag_sorter varchar(48) NOT NULL default '',
donordam varchar(16) NOT NULL default '',
whp timestamp,
esi timestamp,
status varchar(20) NOT NULL default '',
status_date timestamp,
overall_adg varchar(20) NOT NULL default '',
current_adg varchar(20) NOT NULL default '',
last_weight varchar(20) NOT NULL default '',
last_weight_date timestamp,
selected integer default 0,
animal_group varchar(20) NOT NULL default '',
current_farm varchar(20) NOT NULL default '',
current_property varchar(20) NOT NULL default '',
current_area varchar(20) NOT NULL default '',
current_farm_date timestamp,
current_property_date timestamp,
current_area_date timestamp,
animal_group_date timestamp,
sex_date timestamp,
breed_date timestamp,
dob_date timestamp,
colour_date timestamp,
prev_pic_date timestamp,
sire_date timestamp,
dam_date timestamp,
donordam_date timestamp,
prev_tag_date timestamp,
tag_date timestamp,
rfid_date timestamp,
nlis_date timestamp,
modified timestamp,
full_rfid varchar(16) default '',
full_rfid_date timestamp);
DROP TABLE NOTE1;
CREATE TABLE NOTE1 (
animal_id integer NOT NULL,
created timestamp,
note varchar(30) NOT NULL,
session_id integer NOT NULL,
is_deleted integer default 0,
is_alert integer default 0,
primary key( animal_id, created ));
DROP TABLE SessionAnimalActivity;
CREATE TABLE SessionAnimalActivity (
session_id integer NOT NULL,
animal_id integer NOT NULL,
activity_code integer NOT NULL,
when_measured timestamp NOT NULL,
latestForSessionAnimal integer default 1,
latestForAnimal integer default 1,
is_history integer NOT NULL default 0,
is_exported integer NOT NULL default 0,
is_deleted integer default 0,
primary key( session_id, animal_id, activity_code, when_measured ));
DROP TABLE SessionAnimalTrait;
CREATE TABLE SessionAnimalTrait (
session_id integer NOT NULL,
animal_id integer NOT NULL,
trait_code integer NOT NULL,
alpha_value varchar(20) NOT NULL default '',
alpha_units varchar(10) NOT NULL default '',
when_measured timestamp NOT NULL,
latestForSessionAnimal integer default 1,
latestForAnimal integer default 1,
is_history integer NOT NULL default 0,
is_exported integer NOT NULL default 0,
is_deleted integer default 0,
primary key(session_id, animal_id, trait_code, when_measured));
DROP TABLE PicklistValue;
CREATE TABLE PicklistValue (
picklistvalue_id integer primary key,
picklist_id integer,
value varchar(30));
-- read the CSV file into the table
\copy Animal from 'CSV/Animal.csv' WITH DELIMITER ',' CSV HEADER;
-- read the CSV file into the table
\copy Note from 'CSV/Note.csv' WITH DELIMITER ',' CSV HEADER;
-- read the CSV file into the table
\copy SessionAnimalTrait from 'CSV/SessionAnimalTrait.csv' WITH DELIMITER ',' CSV HEADER;
--drops all of our schema on load
DROP TABLE IF EXISTS NOTE;
DROP TABLE IF EXISTS WEIGH_IN;
DROP TABLE IF EXISTS GOAT_ACTIVITY;
DROP TABLE IF EXISTS GOAT;
--creates goat table from schema
CREATE TABLE GOAT AS
SELECT
animal_id AS Goat_id,
sex AS Gender,
dob AS Birth_date,
breed AS Breed
FROM Animal;
ALTER TABLE GOAT
ADD PRIMARY KEY (Goat_id);
--ADD CONSTRAINT fk_cohort_id FOREIGN KEY (Cohort_id) REFERENCES COHORT (Cohort_id);
--creats goat activity table from schema
CREATE TABLE GOAT_ACTIVITY AS
SELECT
session_id as Session_id,
animal_id as Goat_id,
activity_code as Activity_code,
when_measured as Activity_date,
is_exported,
is_deleted
FROM SessionAnimalActivity;
ALTER TABLE GOAT_ACTIVITY
ADD primary key(Session_id, Goat_id, Activity_code, Activity_date),
ADD CONSTRAINT fk_goat_id2 FOREIGN KEY (Goat_id) REFERENCES GOAT (Goat_id);
-- Copying data from SessionAnimalActivity table to WEIGH_IN table
--creates weigh in table from original schema
CREATE TABLE WEIGH_IN AS
SELECT DISTINCT ON (animal_id, when_measured)
when_measured AS Weigh_in_date,
alpha_value AS Weight,
animal_id AS Goat_id
FROM SessionAnimalTrait
WHERE (SessionAnimalTrait.trait_code = 53 or SessionAnimalTrait.trait_code = 357 or
SessionAnimalTrait.trait_code = 436 or SessionAnimalTrait.trait_code = 393
or SessionAnimalTrait.trait_code = 381 or SessionAnimalTrait.trait_code =
405) and SessionAnimalTrait.alpha_value != '0.0' and SessionAnimalTrait.alpha_value != '' and SessionAnimalTrait.alpha_value IS NOT NULL;
ALTER TABLE WEIGH_IN
ALTER COLUMN Weight TYPE FLOAT USING Weight::FLOAT;
ALTER TABLE WEIGH_IN
--ADD PRIMARY KEY (Weigh_in_date, Goat_id),
ADD CONSTRAINT fk_goat_id FOREIGN KEY (Goat_id) REFERENCES GOAT (Goat_id);
-- --new stuff
-- ALTER TABLE WEIGH_IN
-- ADD COLUMN Age INT;
-- UPDATE WEIGH_IN w
-- SET Age = DATEDIFF(day, g.Birth_date, w.Weigh_in_date)
-- FROM GOAT g
-- WHERE w.Goat_id = g.Goat_id;
-- creates not table from original note table
CREATE TABLE NOTE AS
SELECT
animal_id AS Goat_id,
note_date AS Date_of_note,
note AS Note
FROM Animal;
ALTER TABLE NOTE
--ADD PRIMARY KEY (Goat_id, Date_of_note),
ADD CONSTRAINT fk_goat_id FOREIGN KEY (Goat_id) REFERENCES GOAT (Goat_id);
--drops the original schemas tables
DROP TABLE PicklistValue;
DROP TABLE Animal;
DROP TABLE NOTE1;
DROP TABLE SessionAnimalActivity;
DROP TABLE SessionAnimalTrait;