Revision 12061
Added by Aaron Marcuse-Kubitza almost 11 years ago
trunk/validation/aggregating/plots/bien3_validations_salvias_vegbien.by=Brad@iPlant,Aaron@UCSB.sql | ||
---|---|---|
1 |
-- ----------------------------------------------------------------------------- |
|
2 |
-- Quantitative validation queries on the BIEN database |
|
3 |
-- |
|
4 |
-- Applies to SALVIAS, or any datasource with all of the following attributes: |
|
5 |
-- - Plots |
|
6 |
-- - Comprised of multiple datasets organized as "projects" |
|
7 |
-- - Maximum 2-level nesting (subplots within plots) |
|
8 |
-- - One or more plots contain individual observations with stem measurements |
|
9 |
-- - One or more plots contain aggregate observations of counts of individuals within species |
|
10 |
-- - One or more plots contain aggregate observations of cover by species |
|
11 |
-- |
|
12 |
-- Requires list of projects from source db manager, in three groups: (1) individual observation |
|
13 |
-- plot, (2) aggregate individual count plots, (3) aggregate percent cover plots |
|
14 |
-- ------------------------------------------------------------------------- |
|
15 |
|
|
16 |
-- ------------------ |
|
17 |
-- Set variables specific to this source |
|
18 |
-- Currently configured for SALVIAS |
|
19 |
-- ------------------ |
|
20 |
|
|
21 |
-- datasource |
|
22 |
\set ds '\'SALVIAS\'' |
|
23 |
|
|
24 |
-- ------------------ |
|
25 |
-- 1. Count of projects |
|
26 |
-- Check: identical count in source db |
|
27 |
-- ------------------ |
|
28 |
SELECT COUNT(*) AS projects |
|
29 |
FROM project p JOIN source s |
|
30 |
ON p.source_id=s.source_id |
|
31 |
WHERE s.shortname=:ds; |
|
32 |
|
|
33 |
-- ------------------ |
|
34 |
-- 2. List of project names |
|
35 |
-- Check: join to source db returns same number of rows |
|
36 |
-- ------------------ |
|
37 |
SELECT p.projectname |
|
38 |
FROM project p JOIN source s |
|
39 |
ON p.source_id=s.source_id |
|
40 |
WHERE s.shortname=:ds; |
|
41 |
|
|
42 |
-- ------------------ |
|
43 |
-- 3. Count of all plots in this source |
|
44 |
-- Check: identical count in source db |
|
45 |
-- ------------------ |
|
46 |
SELECT COUNT(*) AS plots |
|
47 |
FROM location l JOIN locationevent le |
|
48 |
ON l.location_id=le.location_id |
|
49 |
JOIN project p |
|
50 |
ON p.project_id=le.project_id |
|
51 |
JOIN source s |
|
52 |
ON p.source_id=s.source_id |
|
53 |
WHERE s.shortname=:ds; |
|
54 |
|
|
55 |
-- ------------------ |
|
56 |
-- 4. Count of plots in each project in this source |
|
57 |
-- ------------------ |
|
58 |
SELECT p.projectname, COUNT(DISTINCT l.location_id) AS plots |
|
59 |
FROM location l JOIN locationevent le |
|
60 |
ON l.location_id=le.location_id |
|
61 |
JOIN project p |
|
62 |
ON p.project_id=le.project_id |
|
63 |
JOIN source s |
|
64 |
ON p.source_id=s.source_id |
|
65 |
WHERE s.shortname=:ds |
|
66 |
GROUP BY p.projectname; |
|
67 |
|
|
68 |
-- ------------------ |
|
69 |
-- 5.List of plot codes by project |
|
70 |
-- Check: join to source db by all columns returns same number of rows |
|
71 |
-- ------------------ |
|
72 |
SELECT p.projectname, l.authorlocationcode AS plotCode |
|
73 |
FROM location l JOIN locationevent le |
|
74 |
ON l.location_id=le.location_id |
|
75 |
JOIN project p |
|
76 |
ON p.project_id=le.project_id |
|
77 |
JOIN source s |
|
78 |
ON p.source_id=s.source_id |
|
79 |
WHERE s.shortname=:ds; |
|
80 |
|
|
81 |
-- ------------------ |
|
82 |
-- 6. List of plots with stem measurements |
|
83 |
-- ------------------ |
|
84 |
|
|
85 |
SELECT authorlocationcode |
|
86 |
FROM top_plot |
|
87 |
WHERE source_id = source_by_shortname(:ds) |
|
88 |
AND EXISTS( |
|
89 |
SELECT NULL |
|
90 |
FROM location |
|
91 |
JOIN locationevent USING (location_id) |
|
92 |
JOIN taxonoccurrence USING (locationevent_id) |
|
93 |
JOIN aggregateoccurrence USING (taxonoccurrence_id) |
|
94 |
JOIN plantobservation USING (aggregateoccurrence_id) |
|
95 |
JOIN stemobservation USING (plantobservation_id) |
|
96 |
WHERE location.top_plot = top_plot.location_id |
|
97 |
LIMIT 1 |
|
98 |
) |
|
99 |
ORDER BY authorlocationcode |
|
100 |
; |
|
101 |
|
|
102 |
-- ------------------ |
|
103 |
-- 7.List of plots with counts of individuals per species |
|
104 |
-- ------------------ |
|
105 |
|
|
106 |
|
|
107 |
|
|
108 |
-- ------------------ |
|
109 |
-- 8.List of plots which use percent cover |
|
110 |
-- ------------------ |
|
111 |
|
|
112 |
|
|
113 |
|
|
114 |
|
|
115 |
-- ------------------ |
|
116 |
-- 9.List of plots which use line-intercept |
|
117 |
-- ------------------ |
|
118 |
|
|
119 |
|
|
120 |
-- ------------------ |
|
121 |
-- 10. Count of individuals per plot in each project |
|
122 |
-- |
|
123 |
-- Method: count taxonObservations |
|
124 |
-- Note 1: `individuals` should be NULL for plots which use percent cover |
|
125 |
-- or line-intercept methods. |
|
126 |
-- Note 2: Does this method requires that plots have 2-level nesting of |
|
127 |
-- subplots within plots? |
|
128 |
-- ------------------ |
|
129 |
select p.projectname, l.authorlocationcode as plotcode, sum(ao.count) as individuals |
|
130 |
from project p join source s |
|
131 |
on p.source_id=s.source_id |
|
132 |
join locationevent le on p.project_id=le.project_id |
|
133 |
join locationevent sub_locationevent on sub_locationevent.parent_id=le.locationevent_id |
|
134 |
join location l on le.location_id=l.location_id |
|
135 |
join taxonoccurrence o on sub_locationevent.locationevent_id=o.locationevent_id |
|
136 |
join aggregateoccurrence ao on o.taxonoccurrence_id=ao.taxonoccurrence_id |
|
137 |
where s.shortname=:ds |
|
138 |
group by p.projectname, l.authorlocationcode |
|
139 |
order by p.projectname, l.authorlocationcode; |
|
140 |
|
|
141 |
-- ------------------ |
|
142 |
-- 11. Count of stems per plot in each project |
|
143 |
-- |
|
144 |
-- Method: count records in stemobservation table |
|
145 |
-- ------------------ |
|
146 |
select p.projectname, l.authorlocationcode as plotcode, count(distinct stemobservation_id) as stems |
|
147 |
from project p join source s |
|
148 |
on p.source_id=s.source_id |
|
149 |
join locationevent le on p.project_id=le.project_id |
|
150 |
join locationevent sub_locationevent on sub_locationevent.parent_id=le.locationevent_id |
|
151 |
join location l on le.location_id=l.location_id |
|
152 |
join taxonoccurrence o on sub_locationevent.locationevent_id=o.locationevent_id |
|
153 |
join aggregateoccurrence ao on o.taxonoccurrence_id=ao.taxonoccurrence_id |
|
154 |
join plantobservation po on po.aggregateoccurrence_id=ao.aggregateoccurrence_id |
|
155 |
join stemobservation so on so.plantobservation_id=po.plantobservation_id |
|
156 |
where s.shortname=:ds |
|
157 |
group by p.projectname, l.authorlocationcode |
|
158 |
order by p.projectname, l.authorlocationcode; |
|
159 |
|
|
160 |
-- ------------------ |
|
161 |
-- 12. Count of verbatim taxa per plot in each project |
|
162 |
-- Check: join to source db by all columns, returns same number of rows |
|
163 |
-- Note: Must perform equivalent concatenation of taxonomic field in source db. |
|
164 |
-- NOT YET READY, NEED TO INCLUDE SUBSPECIES, IF ANY |
|
165 |
-- ------------------ |
|
166 |
select p.projectname, l.authorlocationcode as plotcode, |
|
167 |
count(distinct trim('' || coalesce(family::text,'') || ' ' || coalesce(genus::text,'') || ' ' || coalesce(specific_epithet::text,'') || ' ' || coalesce(morphospecies::text,''))) |
|
168 |
as taxa |
|
169 |
from project p join source s |
|
170 |
on p.source_id=s.source_id |
|
171 |
join locationevent le on p.project_id=le.project_id |
|
172 |
join locationevent sub_locationevent on sub_locationevent.parent_id=le.locationevent_id |
|
173 |
join location l on le.location_id=l.location_id |
|
174 |
join taxonoccurrence o on sub_locationevent.locationevent_id=o.locationevent_id |
|
175 |
join taxondetermination td on td.taxonoccurrence_id=o.taxonoccurrence_id |
|
176 |
join taxonverbatim tv on td.taxonverbatim_id=tv.taxonverbatim_id |
|
177 |
where s.shortname=:ds and td.isoriginal='t' |
|
178 |
group by p.projectname, l.authorlocationcode |
|
179 |
order by p.projectname, l.authorlocationcode 12; |
|
180 |
|
|
181 |
-- ------------------ |
|
182 |
-- 13. List of distinct verbatim taxa in each plot in each project |
|
183 |
-- |
|
184 |
-- Note: Must perform equivalent concatenation of taxonomic field in source db. |
|
185 |
-- ------------------ |
|
186 |
select distinct p.projectname, l.authorlocationcode as plotcode, |
|
187 |
trim('' || coalesce(family::text,'') || ' ' || coalesce(genus::text,'') || ' ' || coalesce(specific_epithet::text,'') || ' ' || coalesce(morphospecies::text,'')) as taxon |
|
188 |
from project p join source s |
|
189 |
on p.source_id=s.source_id |
|
190 |
join locationevent le on p.project_id=le.project_id |
|
191 |
join locationevent sub_locationevent on sub_locationevent.parent_id=le.locationevent_id |
|
192 |
join location l on le.location_id=l.location_id |
|
193 |
join taxonoccurrence o on sub_locationevent.locationevent_id=o.locationevent_id |
|
194 |
join taxondetermination td on td.taxonoccurrence_id=o.taxonoccurrence_id |
|
195 |
join taxonverbatim tv on td.taxonverbatim_id=tv.taxonverbatim_id |
|
196 |
where s.shortname=:ds and td.isoriginal='t' |
|
197 |
order by p.projectname, l.authorlocationcode, taxon; |
|
198 |
|
|
199 |
-- ------------------ |
|
200 |
-- 14. Count of individuals per (verbatim) taxon per plot in each project |
|
201 |
-- |
|
202 |
-- Note: Must do equivalent concatenation of taxonomic field in source db. |
|
203 |
-- ------------------ |
|
204 |
select distinct p.projectname, l.authorlocationcode as plotcode, |
|
205 |
trim('' || coalesce(family::text,'') || ' ' || coalesce(genus::text,'') || ' ' || coalesce(specific_epithet::text,'') || ' ' || coalesce(morphospecies::text,'')) as taxon, |
|
206 |
sum(ao.count) as individuals |
|
207 |
from project p join source s |
|
208 |
on p.source_id=s.source_id |
|
209 |
join locationevent le on p.project_id=le.project_id |
|
210 |
join locationevent sub_locationevent on sub_locationevent.parent_id=le.locationevent_id |
|
211 |
join location l on le.location_id=l.location_id |
|
212 |
join taxonoccurrence o on sub_locationevent.locationevent_id=o.locationevent_id |
|
213 |
join taxondetermination td on td.taxonoccurrence_id=o.taxonoccurrence_id |
|
214 |
join taxonverbatim tv on td.taxonverbatim_id=tv.taxonverbatim_id |
|
215 |
join aggregateoccurrence ao on o.taxonoccurrence_id=ao.taxonoccurrence_id |
|
216 |
where s.shortname=:ds and td.isoriginal='t' |
|
217 |
group by p.projectname, l.authorlocationcode, taxon |
|
218 |
order by p.projectname, l.authorlocationcode, taxon; |
|
219 |
|
|
220 |
-- ------------------ |
|
221 |
-- 15. Percent cover of each (verbatim) taxon in each plot in each project |
|
222 |
-- Applies to: aggregate-cover plots only |
|
223 |
-- Method: sums percent cover in aggregateoccurrence (as recorded in coverindex) |
|
224 |
-- Check: join to source db by all columns, returns same number of rows |
|
225 |
-- Note: Must do equivalent concatenation of taxonomic field in source db. |
|
226 |
-- ------------------ |
|
227 |
select distinct p.projectname, l.authorlocationcode as plotcode, |
|
228 |
trim('' || coalesce(family::text,'') || ' ' || coalesce(genus::text,'') || ' ' || coalesce(specific_epithet::text,'') || ' ' || coalesce(morphospecies::text,'')) as taxon, |
|
229 |
sum(ci.coverpercent) as totalpercentcover |
|
230 |
from project p join source s |
|
231 |
on p.source_id=s.source_id |
|
232 |
join locationevent le on p.project_id=le.project_id |
|
233 |
join locationevent sub_locationevent on sub_locationevent.parent_id=le.locationevent_id |
|
234 |
join location l on le.location_id=l.location_id |
|
235 |
join taxonoccurrence o on sub_locationevent.locationevent_id=o.locationevent_id |
|
236 |
join taxondetermination td on td.taxonoccurrence_id=o.taxonoccurrence_id |
|
237 |
join taxonverbatim tv on td.taxonverbatim_id=tv.taxonverbatim_id |
|
238 |
join aggregateoccurrence ao on o.taxonoccurrence_id=ao.taxonoccurrence_id |
|
239 |
join coverindex ci on ao.coverindex_id=ci.coverindex_id |
|
240 |
where s.shortname=:ds and td.isoriginal='t' |
|
241 |
and ci.coverpercent is not null |
|
242 |
group by p.projectname, l.authorlocationcode, taxon |
|
243 |
order by p.projectname, l.authorlocationcode, taxon; |
|
244 |
|
|
245 |
-- ------------------ |
|
246 |
-- 16. Intercept values for each (verbatim) taxon in each plot in each project |
|
247 |
-- where line-intercept values are recorded |
|
248 |
-- |
|
249 |
-- Note 1: Assumes identical concatenation of taxonomic fields |
|
250 |
-- to form verbatim taxon name in origina db |
|
251 |
-- Note 2: Grouping mean cover for entire plot combines plots with |
|
252 |
-- subplots (separate cover measure for each species in each subplot) with |
|
253 |
-- plots with strata (separate cover measures for each species in each |
|
254 |
-- stratum) with plots without subplots or strata (one cover measure |
|
255 |
-- per species per plot. |
|
256 |
-- ------------------ |
|
257 |
|
|
258 |
|
|
259 |
|
|
260 |
|
|
261 |
|
|
262 |
|
|
263 |
-- ------------------ |
|
264 |
-- 17. Count of subplots per plot, for each project |
|
265 |
-- ------------------ |
|
266 |
select p.projectname, l.authorlocationcode as plotcode, |
|
267 |
count(distinct sub_locationevent.locationevent_id) as subplots |
|
268 |
from project p join source s |
|
269 |
on p.source_id=s.source_id |
|
270 |
join locationevent le on p.project_id=le.project_id |
|
271 |
join locationevent sub_locationevent on sub_locationevent.parent_id=le.locationevent_id |
|
272 |
join location l on le.location_id=l.location_id |
|
273 |
where s.shortname=:ds |
|
274 |
group by p.projectname, l.authorlocationcode |
|
275 |
order by p.projectname, l.authorlocationcode; |
|
276 |
|
|
277 |
-- ------------------ |
|
278 |
-- 18. List of subplots codes for each plot for each project |
|
279 |
-- DOESN'T YET WORK PROPERLY |
|
280 |
-- ------------------ |
|
281 |
select p.projectname, |
|
282 |
l.authorlocationcode as plotcode, |
|
283 |
sub_locationevent.authoreventcode as subplotCode |
|
284 |
from project p join source s |
|
285 |
on p.source_id=s.source_id |
|
286 |
join locationevent le on p.project_id=le.project_id |
|
287 |
join locationevent sub_locationevent on sub_locationevent.parent_id=le.locationevent_id |
|
288 |
join location l on sub_locationevent.location_id=l.location_id |
|
289 |
where s.shortname=:ds |
|
290 |
order by p.projectname, l.authorlocationcode; |
trunk/validation/aggregating/plots/_archive/bien3_validations_salvias_and_similar.by=Brad@iPlant.sql | ||
---|---|---|
1 |
-- ----------------------------------------------------------------------------- |
|
2 |
-- Quantitative validation queries on the BIEN database |
|
3 |
-- |
|
4 |
-- Applies to SALVIAS, or any datasource with all of the following attributes: |
|
5 |
-- - Plots |
|
6 |
-- - Comprised of multiple datasets organized as "projects" |
|
7 |
-- - Maximum 2-level nesting (subplots within plots) |
|
8 |
-- - One or more plots contain individual observations with stem measurements |
|
9 |
-- - One or more plots contain aggregate observations of counts of individuals within species |
|
10 |
-- - One or more plots contain aggregate observations of cover by species |
|
11 |
-- |
|
12 |
-- Requires list of projects from source db manager, in three groups: (1) individual observation |
|
13 |
-- plot, (2) aggregate individual count plots, (3) aggregate percent cover plots |
|
14 |
-- ------------------------------------------------------------------------- |
|
15 |
|
|
16 |
-- ------------------ |
|
17 |
-- Set variables specific to this source |
|
18 |
-- Currently configured for SALVIAS |
|
19 |
-- ------------------ |
|
20 |
|
|
21 |
-- datasource |
|
22 |
\set ds '\'SALVIAS\'' |
|
23 |
|
|
24 |
-- list of projects with individual observations |
|
25 |
\set pio '\'Boyle Transects\',\'Enquist Lab Transect Dataset\',\'ACA Amazon Forest Inventories\',\'Bonifacino Forest Transects\',\'Cam Webb Borneo Plots\',\'DeWalt Bolivia forest plots\',\'La Selva Secondary Forest Plots\',\'Noel Kempff Forest Plots\',\'OTS Transects\',\'Pilon Lajas Treeplots Bolivia\',\'RAINFOR - 0.1 ha Madre de Dios, Peru\',\'RAINFOR - 1 ha Peru\'' |
|
26 |
|
|
27 |
-- list of projects with aggregate individual counts |
|
28 |
\set pai '\'Gentry Transect Dataset\'' |
|
29 |
|
|
30 |
-- list of projects with aggregate cover counts |
|
31 |
\set pac '\'Noel Kempff Savanna Plots\'' |
|
32 |
|
|
33 |
-- ------------------ |
|
34 |
-- 1. Count of projects |
|
35 |
-- Check: identical count in source db |
|
36 |
-- ------------------ |
|
37 |
SELECT COUNT(*) AS projects |
|
38 |
FROM project p JOIN source s |
|
39 |
ON p.source_id=s.source_id |
|
40 |
WHERE s.shortname=:ds; |
|
41 |
|
|
42 |
-- ------------------ |
|
43 |
-- 2. List of project names |
|
44 |
-- Check: join to source db returns same number of rows |
|
45 |
-- ------------------ |
|
46 |
SELECT p.projectname |
|
47 |
FROM project p JOIN source s |
|
48 |
ON p.source_id=s.source_id |
|
49 |
WHERE s.shortname=:ds; |
|
50 |
|
|
51 |
-- ------------------ |
|
52 |
-- 3. Count of all plots in this source |
|
53 |
-- Check: identical count in source db |
|
54 |
-- ------------------ |
|
55 |
SELECT COUNT(*) AS plots |
|
56 |
FROM source s JOIN location l |
|
57 |
ON s.source_id=l.source_id |
|
58 |
WHERE s.shortname=:ds; |
|
59 |
|
|
60 |
-- ------------------ |
|
61 |
-- 4. Count of plots in each project in this source |
|
62 |
-- Check: join to source db by all columns returns same number of rows |
|
63 |
-- ------------------ |
|
64 |
SELECT p.projectname, COUNT(DISTINCT l.location_id) AS plots |
|
65 |
FROM location l JOIN locationevent le |
|
66 |
ON l.location_id=le.location_id |
|
67 |
JOIN project p |
|
68 |
ON p.project_id=le.project_id |
|
69 |
JOIN source s |
|
70 |
ON p.source_id=s.source_id |
|
71 |
WHERE s.shortname=:ds |
|
72 |
AND p.projectname IN (:pio) |
|
73 |
GROUP BY p.projectname; |
|
74 |
|
|
75 |
-- ------------------ |
|
76 |
-- 5.List of plot codes by project |
|
77 |
-- Check: join to source db by all columns returns same number of rows |
|
78 |
-- ------------------ |
|
79 |
SELECT p.projectname, l.authorlocationcode AS plotCode |
|
80 |
FROM location l JOIN locationevent le |
|
81 |
ON l.location_id=le.location_id |
|
82 |
JOIN project p |
|
83 |
ON p.project_id=le.project_id |
|
84 |
JOIN source s |
|
85 |
ON p.source_id=s.source_id |
|
86 |
WHERE s.shortname=:ds |
|
87 |
AND p.projectname IN (:pio); |
|
88 |
|
|
89 |
-- ------------------ |
|
90 |
-- 6. Count of individuals per plot in each project |
|
91 |
-- Applies to: individual observation plots only |
|
92 |
-- Method: count taxonObservations |
|
93 |
-- Check: join to source db by all columns, returns same number of rows |
|
94 |
-- Note: I believe this method requires that plots have 2-level nesting of |
|
95 |
-- subplots within plots? |
|
96 |
-- ------------------ |
|
97 |
select p.projectname, l.authorlocationcode as plotcode, count(*) as individuals |
|
98 |
from project p join source s |
|
99 |
on p.source_id=s.source_id |
|
100 |
join locationevent le on p.project_id=le.project_id |
|
101 |
join locationevent sub_locationevent on sub_locationevent.parent_id=le.locationevent_id |
|
102 |
join location l on le.location_id=l.location_id |
|
103 |
join taxonoccurrence o on sub_locationevent.locationevent_id=o.locationevent_id |
|
104 |
where s.shortname=:ds and p.projectname IN (:pio) |
|
105 |
group by p.projectname, l.authorlocationcode |
|
106 |
order by p.projectname, l.authorlocationcode; |
|
107 |
|
|
108 |
-- ------------------ |
|
109 |
-- 7. Count of individuals per plot in each project |
|
110 |
-- Applies to: aggregate-individual plots only |
|
111 |
-- Method: sum column `count` in table aggregateoccurrence |
|
112 |
-- Check: join to source db by all columns, returns same number of rows |
|
113 |
-- Note: I believe this method requires that plots have 2-level nesting of |
|
114 |
-- subplots within plots? |
|
115 |
-- ------------------ |
|
116 |
select p.projectname, l.authorlocationcode as plotcode, sum(ao.count) as individuals |
|
117 |
from project p join source s |
|
118 |
on p.source_id=s.source_id |
|
119 |
join locationevent le on p.project_id=le.project_id |
|
120 |
join locationevent sub_locationevent on sub_locationevent.parent_id=le.locationevent_id |
|
121 |
join location l on le.location_id=l.location_id |
|
122 |
join taxonoccurrence o on sub_locationevent.locationevent_id=o.locationevent_id |
|
123 |
join aggregateoccurrence ao on o.taxonoccurrence_id=ao.taxonoccurrence_id |
|
124 |
where s.shortname=:ds and p.projectname IN (:pai) |
|
125 |
group by p.projectname, l.authorlocationcode |
|
126 |
order by p.projectname, l.authorlocationcode; |
|
127 |
|
|
128 |
-- ------------------ |
|
129 |
-- 8. Count of stems per plot in each project |
|
130 |
-- Applies to: individual observation and aggregate individual count plots with |
|
131 |
-- stem counts |
|
132 |
-- Method: count records in stemobservation table |
|
133 |
-- Check: join to source db by all columns, returns same number of rows |
|
134 |
-- Note: I believe this method requires that plots have 2-level nesting of |
|
135 |
-- subplots within plots? |
|
136 |
-- ------------------ |
|
137 |
select p.projectname, l.authorlocationcode as plotcode, count(distinct stemobservation_id) as stems |
|
138 |
from project p join source s |
|
139 |
on p.source_id=s.source_id |
|
140 |
join locationevent le on p.project_id=le.project_id |
|
141 |
join locationevent sub_locationevent on sub_locationevent.parent_id=le.locationevent_id |
|
142 |
join location l on le.location_id=l.location_id |
|
143 |
join taxonoccurrence o on sub_locationevent.locationevent_id=o.locationevent_id |
|
144 |
join aggregateoccurrence ao on o.taxonoccurrence_id=ao.taxonoccurrence_id |
|
145 |
join plantobservation po on po.aggregateoccurrence_id=ao.aggregateoccurrence_id |
|
146 |
join stemobservation so on so.plantobservation_id=po.plantobservation_id |
|
147 |
where s.shortname=:ds and (p.projectname IN (:pio) or p.projectname IN (:pai)) |
|
148 |
group by p.projectname, l.authorlocationcode |
|
149 |
order by p.projectname, l.authorlocationcode; |
|
150 |
|
|
151 |
-- ------------------ |
|
152 |
-- 9. Count of verbatim taxa per plot in each project |
|
153 |
-- Check: join to source db by all columns, returns same number of rows |
|
154 |
-- Note: Must perform equivalent concatenation of taxonomic field in source db. |
|
155 |
-- ------------------ |
|
156 |
select p.projectname, l.authorlocationcode as plotcode, |
|
157 |
count(distinct trim('' || coalesce(family::text,'') || ' ' || coalesce(genus::text,'') || ' ' || coalesce(specific_epithet::text,'') || ' ' || coalesce(morphospecies::text,''))) |
|
158 |
as taxa |
|
159 |
from project p join source s |
|
160 |
on p.source_id=s.source_id |
|
161 |
join locationevent le on p.project_id=le.project_id |
|
162 |
join locationevent sub_locationevent on sub_locationevent.parent_id=le.locationevent_id |
|
163 |
join location l on le.location_id=l.location_id |
|
164 |
join taxonoccurrence o on sub_locationevent.locationevent_id=o.locationevent_id |
|
165 |
join taxondetermination td on td.taxonoccurrence_id=o.taxonoccurrence_id |
|
166 |
join taxonverbatim tv on td.taxonverbatim_id=tv.taxonverbatim_id |
|
167 |
where s.shortname=:ds and td.isoriginal='t' |
|
168 |
group by p.projectname, l.authorlocationcode |
|
169 |
order by p.projectname, l.authorlocationcode; |
|
170 |
|
|
171 |
-- ------------------ |
|
172 |
-- 10. List of verbatim taxa in each plot in each project |
|
173 |
-- Check: join to source db by all columns, returns same number of rows. |
|
174 |
-- Note: Must perform equivalent concatenation of taxonomic field in source db. |
|
175 |
-- ------------------ |
|
176 |
select distinct p.projectname, l.authorlocationcode as plotcode, |
|
177 |
trim('' || coalesce(family::text,'') || ' ' || coalesce(genus::text,'') || ' ' || coalesce(specific_epithet::text,'') || ' ' || coalesce(morphospecies::text,'')) as taxon |
|
178 |
from project p join source s |
|
179 |
on p.source_id=s.source_id |
|
180 |
join locationevent le on p.project_id=le.project_id |
|
181 |
join locationevent sub_locationevent on sub_locationevent.parent_id=le.locationevent_id |
|
182 |
join location l on le.location_id=l.location_id |
|
183 |
join taxonoccurrence o on sub_locationevent.locationevent_id=o.locationevent_id |
|
184 |
join taxondetermination td on td.taxonoccurrence_id=o.taxonoccurrence_id |
|
185 |
join taxonverbatim tv on td.taxonverbatim_id=tv.taxonverbatim_id |
|
186 |
where s.shortname=:ds and td.isoriginal='t' |
|
187 |
order by p.projectname, l.authorlocationcode, taxon; |
|
188 |
|
|
189 |
-- ------------------ |
|
190 |
-- 11. Count of individuals per (verbatim) taxon for each plot in each project |
|
191 |
-- Applies to: individual observation plots only |
|
192 |
-- Method: counts taxonoccurrence records |
|
193 |
-- Check: join to source db by all columns, returns same number of rows |
|
194 |
-- Note: Must do equivalent concatenation of taxonomic field in source db. |
|
195 |
-- ------------------ |
|
196 |
select distinct p.projectname, l.authorlocationcode as plotcode, |
|
197 |
trim('' || coalesce(family::text,'') || ' ' || coalesce(genus::text,'') || ' ' || coalesce(specific_epithet::text,'') || ' ' || coalesce(morphospecies::text,'')) as taxon, |
|
198 |
count(distinct o.taxonoccurrence_id) as individuals |
|
199 |
from project p join source s |
|
200 |
on p.source_id=s.source_id |
|
201 |
join locationevent le on p.project_id=le.project_id |
|
202 |
join locationevent sub_locationevent on sub_locationevent.parent_id=le.locationevent_id |
|
203 |
join location l on le.location_id=l.location_id |
|
204 |
join taxonoccurrence o on sub_locationevent.locationevent_id=o.locationevent_id |
|
205 |
join taxondetermination td on td.taxonoccurrence_id=o.taxonoccurrence_id |
|
206 |
join taxonverbatim tv on td.taxonverbatim_id=tv.taxonverbatim_id |
|
207 |
where s.shortname=:ds and td.isoriginal='t' |
|
208 |
and p.projectname IN (:pio) |
|
209 |
group by p.projectname, l.authorlocationcode, taxon |
|
210 |
order by p.projectname, l.authorlocationcode, taxon; |
|
211 |
|
|
212 |
-- ------------------ |
|
213 |
-- 12. Count of individuals per (verbatim) taxon for each plot in each project |
|
214 |
-- Applies to: aggregate-individual count plots only |
|
215 |
-- Method: sums column `count` in aggregateoccurrence |
|
216 |
-- Check: join to source db by all columns, returns same number of rows |
|
217 |
-- Note: Must do equivalent concatenation of taxonomic field in source db. |
|
218 |
-- DOESN'T WORK YET |
|
219 |
-- ------------------ |
|
220 |
select distinct p.projectname, l.authorlocationcode as plotcode, |
|
221 |
trim('' || coalesce(family::text,'') || ' ' || coalesce(genus::text,'') || ' ' || coalesce(specific_epithet::text,'') || ' ' || coalesce(morphospecies::text,'')) as taxon, |
|
222 |
sum(ao.count) as individuals |
|
223 |
from project p join source s |
|
224 |
on p.source_id=s.source_id |
|
225 |
join locationevent le on p.project_id=le.project_id |
|
226 |
join locationevent sub_locationevent on sub_locationevent.parent_id=le.locationevent_id |
|
227 |
join location l on le.location_id=l.location_id |
|
228 |
join taxonoccurrence o on sub_locationevent.locationevent_id=o.locationevent_id |
|
229 |
join taxondetermination td on td.taxonoccurrence_id=o.taxonoccurrence_id |
|
230 |
join taxonverbatim tv on td.taxonverbatim_id=tv.taxonverbatim_id |
|
231 |
join aggregateoccurrence ao on o.taxonoccurrence_id=ao.taxonoccurrence_id |
|
232 |
where s.shortname=:ds and td.isoriginal='t' |
|
233 |
and p.projectname IN (:pai) |
|
234 |
group by p.projectname, l.authorlocationcode, taxon |
|
235 |
order by p.projectname, l.authorlocationcode, taxon; |
|
236 |
|
|
237 |
-- ------------------ |
|
238 |
-- 13. Percent cover of each (verbatim) taxon in each plot in each project |
|
239 |
-- Applies to: aggregate-cover plots only |
|
240 |
-- Method: sums percent cover in aggregateoccurrence (as recorded in coverindex) |
|
241 |
-- Check: join to source db by all columns, returns same number of rows |
|
242 |
-- Note: Must do equivalent concatenation of taxonomic field in source db. |
|
243 |
-- DOESN'T WORK YET, ALSO MAY NEED LEFT JOINS |
|
244 |
-- ------------------ |
|
245 |
select distinct p.projectname, l.authorlocationcode as plotcode, |
|
246 |
trim('' || coalesce(family::text,'') || ' ' || coalesce(genus::text,'') || ' ' || coalesce(specific_epithet::text,'') || ' ' || coalesce(morphospecies::text,'')) as taxon, |
|
247 |
sum(ci.coverpercent) as totalpercentcover |
|
248 |
from project p join source s |
|
249 |
on p.source_id=s.source_id |
|
250 |
join locationevent le on p.project_id=le.project_id |
|
251 |
join locationevent sub_locationevent on sub_locationevent.parent_id=le.locationevent_id |
|
252 |
join location l on le.location_id=l.location_id |
|
253 |
join taxonoccurrence o on sub_locationevent.locationevent_id=o.locationevent_id |
|
254 |
join taxondetermination td on td.taxonoccurrence_id=o.taxonoccurrence_id |
|
255 |
join taxonverbatim tv on td.taxonverbatim_id=tv.taxonverbatim_id |
|
256 |
join aggregateoccurrence ao on o.taxonoccurrence_id=ao.taxonoccurrence_id |
|
257 |
join coverindex ci on ao.coverindex_id=ci.coverindex_id |
|
258 |
where s.shortname=:ds and td.isoriginal='t' |
|
259 |
and p.projectname IN (:pac) |
|
260 |
group by p.projectname, l.authorlocationcode, taxon |
|
261 |
order by p.projectname, l.authorlocationcode, taxon; |
|
262 |
|
|
263 |
-- ------------------ |
|
264 |
-- 14. Count of subplots per plot, for each project |
|
265 |
-- Check: identical count in source db |
|
266 |
-- ------------------ |
|
267 |
select p.projectname, l.authorlocationcode as plotcode, |
|
268 |
count(distinct sub_locationevent.locationevent_id) as subplots |
|
269 |
from project p join source s |
|
270 |
on p.source_id=s.source_id |
|
271 |
join locationevent le on p.project_id=le.project_id |
|
272 |
join locationevent sub_locationevent on sub_locationevent.parent_id=le.locationevent_id |
|
273 |
join location l on le.location_id=l.location_id |
|
274 |
where s.shortname=:ds |
|
275 |
group by p.projectname, l.authorlocationcode |
|
276 |
order by p.projectname, l.authorlocationcode; |
|
277 |
|
|
278 |
-- ------------------ |
|
279 |
-- 15. List of subplots codes for each plot for each project |
|
280 |
-- Check: join to source db by all columns, returns same number of rows |
|
281 |
-- DOESN'T WORK YET, NOT SURE HOW TO DISPLAY SUBPLOT CODES |
|
282 |
-- ------------------ |
|
283 |
select p.projectname, l.authorlocationcode as plotcode, |
|
284 |
sub_locationevent.authoreventcode as subplotCode |
|
285 |
from project p join source s |
|
286 |
on p.source_id=s.source_id |
|
287 |
join locationevent le on p.project_id=le.project_id |
|
288 |
join locationevent sub_locationevent on sub_locationevent.parent_id=le.locationevent_id |
|
289 |
join location l on le.location_id=l.location_id |
|
290 |
where s.shortname=:ds and (p.projectname IN (:pio) or p.projectname IN (:pai)) |
|
291 |
order by p.projectname, l.authorlocationcode; |
trunk/validation/aggregating/plots/_archive/bien3_validations_salvias_vegbien.by=Brad@iPlant.sql | ||
---|---|---|
1 |
-- ----------------------------------------------------------------------------- |
|
2 |
-- Quantitative validation queries on the BIEN database |
|
3 |
-- |
|
4 |
-- Applies to SALVIAS, or any datasource with all of the following attributes: |
|
5 |
-- - Plots |
|
6 |
-- - Comprised of multiple datasets organized as "projects" |
|
7 |
-- - Maximum 2-level nesting (subplots within plots) |
|
8 |
-- - One or more plots contain individual observations with stem measurements |
|
9 |
-- - One or more plots contain aggregate observations of counts of individuals within species |
|
10 |
-- - One or more plots contain aggregate observations of cover by species |
|
11 |
-- |
|
12 |
-- Requires list of projects from source db manager, in three groups: (1) individual observation |
|
13 |
-- plot, (2) aggregate individual count plots, (3) aggregate percent cover plots |
|
14 |
-- ------------------------------------------------------------------------- |
|
15 |
|
|
16 |
-- ------------------ |
|
17 |
-- Set variables specific to this source |
|
18 |
-- Currently configured for SALVIAS |
|
19 |
-- ------------------ |
|
20 |
|
|
21 |
-- datasource |
|
22 |
\set ds '\'SALVIAS\'' |
|
23 |
|
|
24 |
-- ------------------ |
|
25 |
-- 1. Count of projects |
|
26 |
-- Check: identical count in source db |
|
27 |
-- ------------------ |
|
28 |
SELECT COUNT(*) AS projects |
|
29 |
FROM project p JOIN source s |
|
30 |
ON p.source_id=s.source_id |
|
31 |
WHERE s.shortname=:ds; |
|
32 |
|
|
33 |
-- ------------------ |
|
34 |
-- 2. List of project names |
|
35 |
-- Check: join to source db returns same number of rows |
|
36 |
-- ------------------ |
|
37 |
SELECT p.projectname |
|
38 |
FROM project p JOIN source s |
|
39 |
ON p.source_id=s.source_id |
|
40 |
WHERE s.shortname=:ds; |
|
41 |
|
|
42 |
-- ------------------ |
|
43 |
-- 3. Count of all plots in this source |
|
44 |
-- Check: identical count in source db |
|
45 |
-- ------------------ |
|
46 |
SELECT COUNT(*) AS plots |
|
47 |
FROM location l JOIN locationevent le |
|
48 |
ON l.location_id=le.location_id |
|
49 |
JOIN project p |
|
50 |
ON p.project_id=le.project_id |
|
51 |
JOIN source s |
|
52 |
ON p.source_id=s.source_id |
|
53 |
WHERE s.shortname=:ds; |
|
54 |
|
|
55 |
-- ------------------ |
|
56 |
-- 4. Count of plots in each project in this source |
|
57 |
-- ------------------ |
|
58 |
SELECT p.projectname, COUNT(DISTINCT l.location_id) AS plots |
|
59 |
FROM location l JOIN locationevent le |
|
60 |
ON l.location_id=le.location_id |
|
61 |
JOIN project p |
|
62 |
ON p.project_id=le.project_id |
|
63 |
JOIN source s |
|
64 |
ON p.source_id=s.source_id |
|
65 |
WHERE s.shortname=:ds |
|
66 |
GROUP BY p.projectname; |
|
67 |
|
|
68 |
-- ------------------ |
|
69 |
-- 5.List of plot codes by project |
|
70 |
-- Check: join to source db by all columns returns same number of rows |
|
71 |
-- ------------------ |
|
72 |
SELECT p.projectname, l.authorlocationcode AS plotCode |
|
73 |
FROM location l JOIN locationevent le |
|
74 |
ON l.location_id=le.location_id |
|
75 |
JOIN project p |
|
76 |
ON p.project_id=le.project_id |
|
77 |
JOIN source s |
|
78 |
ON p.source_id=s.source_id |
|
79 |
WHERE s.shortname=:ds; |
|
80 |
|
|
81 |
-- ------------------ |
|
82 |
-- 6. List of plots with stem measurements |
|
83 |
-- ------------------ |
|
84 |
|
|
85 |
SET search_path TO "SALVIAS_VegBIEN"; |
|
86 |
SELECT |
|
87 |
authorlocationcode |
|
88 |
FROM (SELECT * FROM location WHERE parent_id IS NULL) top_plot |
|
89 |
WHERE source_id = source_by_shortname('SALVIAS') |
|
90 |
AND EXISTS( |
|
91 |
SELECT NULL |
|
92 |
FROM location |
|
93 |
JOIN locationevent USING (location_id) |
|
94 |
JOIN taxonoccurrence USING (locationevent_id) |
|
95 |
JOIN aggregateoccurrence USING (taxonoccurrence_id) |
|
96 |
JOIN plantobservation USING (aggregateoccurrence_id) |
|
97 |
JOIN stemobservation USING (plantobservation_id) |
|
98 |
WHERE location.top_plot = top_plot.location_id |
|
99 |
LIMIT 1 |
|
100 |
) |
|
101 |
ORDER BY authorlocationcode |
|
102 |
; |
|
103 |
|
|
104 |
-- ------------------ |
|
105 |
-- 7.List of plots with counts of individuals per species |
|
106 |
-- ------------------ |
|
107 |
|
|
108 |
|
|
109 |
|
|
110 |
-- ------------------ |
|
111 |
-- 8.List of plots which use percent cover |
|
112 |
-- ------------------ |
|
113 |
|
|
114 |
|
|
115 |
|
|
116 |
|
|
117 |
-- ------------------ |
|
118 |
-- 9.List of plots which use line-intercept |
|
119 |
-- ------------------ |
|
120 |
|
|
121 |
|
|
122 |
-- ------------------ |
|
123 |
-- 10. Count of individuals per plot in each project |
|
124 |
-- |
|
125 |
-- Method: count taxonObservations |
|
126 |
-- Note 1: `individuals` should be NULL for plots which use percent cover |
|
127 |
-- or line-intercept methods. |
|
128 |
-- Note 2: Does this method requires that plots have 2-level nesting of |
|
129 |
-- subplots within plots? |
|
130 |
-- ------------------ |
|
131 |
select p.projectname, l.authorlocationcode as plotcode, sum(ao.count) as individuals |
|
132 |
from project p join source s |
|
133 |
on p.source_id=s.source_id |
|
134 |
join locationevent le on p.project_id=le.project_id |
|
135 |
join locationevent sub_locationevent on sub_locationevent.parent_id=le.locationevent_id |
|
136 |
join location l on le.location_id=l.location_id |
|
137 |
join taxonoccurrence o on sub_locationevent.locationevent_id=o.locationevent_id |
|
138 |
join aggregateoccurrence ao on o.taxonoccurrence_id=ao.taxonoccurrence_id |
|
139 |
where s.shortname=:ds |
|
140 |
group by p.projectname, l.authorlocationcode |
|
141 |
order by p.projectname, l.authorlocationcode; |
|
142 |
|
|
143 |
-- ------------------ |
|
144 |
-- 11. Count of stems per plot in each project |
|
145 |
-- |
|
146 |
-- Method: count records in stemobservation table |
|
147 |
-- ------------------ |
|
148 |
select p.projectname, l.authorlocationcode as plotcode, count(distinct stemobservation_id) as stems |
|
149 |
from project p join source s |
|
150 |
on p.source_id=s.source_id |
|
151 |
join locationevent le on p.project_id=le.project_id |
|
152 |
join locationevent sub_locationevent on sub_locationevent.parent_id=le.locationevent_id |
|
153 |
join location l on le.location_id=l.location_id |
|
154 |
join taxonoccurrence o on sub_locationevent.locationevent_id=o.locationevent_id |
|
155 |
join aggregateoccurrence ao on o.taxonoccurrence_id=ao.taxonoccurrence_id |
|
156 |
join plantobservation po on po.aggregateoccurrence_id=ao.aggregateoccurrence_id |
|
157 |
join stemobservation so on so.plantobservation_id=po.plantobservation_id |
|
158 |
where s.shortname=:ds |
|
159 |
group by p.projectname, l.authorlocationcode |
|
160 |
order by p.projectname, l.authorlocationcode; |
|
161 |
|
|
162 |
-- ------------------ |
|
163 |
-- 12. Count of verbatim taxa per plot in each project |
|
164 |
-- Check: join to source db by all columns, returns same number of rows |
|
165 |
-- Note: Must perform equivalent concatenation of taxonomic field in source db. |
|
166 |
-- NOT YET READY, NEED TO INCLUDE SUBSPECIES, IF ANY |
|
167 |
-- ------------------ |
|
168 |
select p.projectname, l.authorlocationcode as plotcode, |
|
169 |
count(distinct trim('' || coalesce(family::text,'') || ' ' || coalesce(genus::text,'') || ' ' || coalesce(specific_epithet::text,'') || ' ' || coalesce(morphospecies::text,''))) |
|
170 |
as taxa |
|
171 |
from project p join source s |
|
172 |
on p.source_id=s.source_id |
|
173 |
join locationevent le on p.project_id=le.project_id |
|
174 |
join locationevent sub_locationevent on sub_locationevent.parent_id=le.locationevent_id |
|
175 |
join location l on le.location_id=l.location_id |
|
176 |
join taxonoccurrence o on sub_locationevent.locationevent_id=o.locationevent_id |
|
177 |
join taxondetermination td on td.taxonoccurrence_id=o.taxonoccurrence_id |
|
178 |
join taxonverbatim tv on td.taxonverbatim_id=tv.taxonverbatim_id |
|
179 |
where s.shortname=:ds and td.isoriginal='t' |
|
180 |
group by p.projectname, l.authorlocationcode |
|
181 |
order by p.projectname, l.authorlocationcode 12; |
|
182 |
|
|
183 |
-- ------------------ |
|
184 |
-- 13. List of distinct verbatim taxa in each plot in each project |
|
185 |
-- |
|
186 |
-- Note: Must perform equivalent concatenation of taxonomic field in source db. |
|
187 |
-- ------------------ |
|
188 |
select distinct p.projectname, l.authorlocationcode as plotcode, |
|
189 |
trim('' || coalesce(family::text,'') || ' ' || coalesce(genus::text,'') || ' ' || coalesce(specific_epithet::text,'') || ' ' || coalesce(morphospecies::text,'')) as taxon |
|
190 |
from project p join source s |
|
191 |
on p.source_id=s.source_id |
|
192 |
join locationevent le on p.project_id=le.project_id |
|
193 |
join locationevent sub_locationevent on sub_locationevent.parent_id=le.locationevent_id |
|
194 |
join location l on le.location_id=l.location_id |
|
195 |
join taxonoccurrence o on sub_locationevent.locationevent_id=o.locationevent_id |
|
196 |
join taxondetermination td on td.taxonoccurrence_id=o.taxonoccurrence_id |
|
197 |
join taxonverbatim tv on td.taxonverbatim_id=tv.taxonverbatim_id |
|
198 |
where s.shortname=:ds and td.isoriginal='t' |
|
199 |
order by p.projectname, l.authorlocationcode, taxon; |
|
200 |
|
|
201 |
-- ------------------ |
|
202 |
-- 14. Count of individuals per (verbatim) taxon per plot in each project |
|
203 |
-- |
|
204 |
-- Note: Must do equivalent concatenation of taxonomic field in source db. |
|
205 |
-- ------------------ |
|
206 |
select distinct p.projectname, l.authorlocationcode as plotcode, |
|
207 |
trim('' || coalesce(family::text,'') || ' ' || coalesce(genus::text,'') || ' ' || coalesce(specific_epithet::text,'') || ' ' || coalesce(morphospecies::text,'')) as taxon, |
|
208 |
sum(ao.count) as individuals |
|
209 |
from project p join source s |
|
210 |
on p.source_id=s.source_id |
|
211 |
join locationevent le on p.project_id=le.project_id |
|
212 |
join locationevent sub_locationevent on sub_locationevent.parent_id=le.locationevent_id |
|
213 |
join location l on le.location_id=l.location_id |
|
214 |
join taxonoccurrence o on sub_locationevent.locationevent_id=o.locationevent_id |
|
215 |
join taxondetermination td on td.taxonoccurrence_id=o.taxonoccurrence_id |
|
216 |
join taxonverbatim tv on td.taxonverbatim_id=tv.taxonverbatim_id |
|
217 |
join aggregateoccurrence ao on o.taxonoccurrence_id=ao.taxonoccurrence_id |
|
218 |
where s.shortname=:ds and td.isoriginal='t' |
|
219 |
group by p.projectname, l.authorlocationcode, taxon |
|
220 |
order by p.projectname, l.authorlocationcode, taxon; |
|
221 |
|
|
222 |
-- ------------------ |
|
223 |
-- 15. Percent cover of each (verbatim) taxon in each plot in each project |
|
224 |
-- Applies to: aggregate-cover plots only |
|
225 |
-- Method: sums percent cover in aggregateoccurrence (as recorded in coverindex) |
|
226 |
-- Check: join to source db by all columns, returns same number of rows |
|
227 |
-- Note: Must do equivalent concatenation of taxonomic field in source db. |
|
228 |
-- ------------------ |
|
229 |
select distinct p.projectname, l.authorlocationcode as plotcode, |
|
230 |
trim('' || coalesce(family::text,'') || ' ' || coalesce(genus::text,'') || ' ' || coalesce(specific_epithet::text,'') || ' ' || coalesce(morphospecies::text,'')) as taxon, |
|
231 |
sum(ci.coverpercent) as totalpercentcover |
|
232 |
from project p join source s |
|
233 |
on p.source_id=s.source_id |
|
234 |
join locationevent le on p.project_id=le.project_id |
|
235 |
join locationevent sub_locationevent on sub_locationevent.parent_id=le.locationevent_id |
|
236 |
join location l on le.location_id=l.location_id |
|
237 |
join taxonoccurrence o on sub_locationevent.locationevent_id=o.locationevent_id |
|
238 |
join taxondetermination td on td.taxonoccurrence_id=o.taxonoccurrence_id |
|
239 |
join taxonverbatim tv on td.taxonverbatim_id=tv.taxonverbatim_id |
|
240 |
join aggregateoccurrence ao on o.taxonoccurrence_id=ao.taxonoccurrence_id |
|
241 |
join coverindex ci on ao.coverindex_id=ci.coverindex_id |
|
242 |
where s.shortname=:ds and td.isoriginal='t' |
|
243 |
and ci.coverpercent is not null |
|
244 |
group by p.projectname, l.authorlocationcode, taxon |
|
245 |
order by p.projectname, l.authorlocationcode, taxon; |
|
246 |
|
|
247 |
-- ------------------ |
|
248 |
-- 16. Intercept values for each (verbatim) taxon in each plot in each project |
|
249 |
-- where line-intercept values are recorded |
|
250 |
-- |
|
251 |
-- Note 1: Assumes identical concatenation of taxonomic fields |
|
252 |
-- to form verbatim taxon name in origina db |
|
253 |
-- Note 2: Grouping mean cover for entire plot combines plots with |
|
254 |
-- subplots (separate cover measure for each species in each subplot) with |
|
255 |
-- plots with strata (separate cover measures for each species in each |
|
256 |
-- stratum) with plots without subplots or strata (one cover measure |
|
257 |
-- per species per plot. |
|
258 |
-- ------------------ |
|
259 |
|
|
260 |
|
|
261 |
|
|
262 |
|
|
263 |
|
|
264 |
|
|
265 |
-- ------------------ |
|
266 |
-- 17. Count of subplots per plot, for each project |
|
267 |
-- ------------------ |
|
268 |
select p.projectname, l.authorlocationcode as plotcode, |
|
269 |
count(distinct sub_locationevent.locationevent_id) as subplots |
|
270 |
from project p join source s |
|
271 |
on p.source_id=s.source_id |
|
272 |
join locationevent le on p.project_id=le.project_id |
|
273 |
join locationevent sub_locationevent on sub_locationevent.parent_id=le.locationevent_id |
|
274 |
join location l on le.location_id=l.location_id |
|
275 |
where s.shortname=:ds |
|
276 |
group by p.projectname, l.authorlocationcode |
|
277 |
order by p.projectname, l.authorlocationcode; |
|
278 |
|
|
279 |
-- ------------------ |
|
280 |
-- 18. List of subplots codes for each plot for each project |
|
281 |
-- DOESN'T YET WORK PROPERLY |
|
282 |
-- ------------------ |
|
283 |
select p.projectname, |
|
284 |
l.authorlocationcode as plotcode, |
|
285 |
sub_locationevent.authoreventcode as subplotCode |
|
286 |
from project p join source s |
|
287 |
on p.source_id=s.source_id |
|
288 |
join locationevent le on p.project_id=le.project_id |
|
289 |
join locationevent sub_locationevent on sub_locationevent.parent_id=le.locationevent_id |
|
290 |
join location l on sub_locationevent.location_id=l.location_id |
|
291 |
where s.shortname=:ds |
|
292 |
order by p.projectname, l.authorlocationcode; |
trunk/validation/aggregating/plots/_archive/bien3_validations_salvias_and_similar.by=Brad@iPlant.sql.url | ||
---|---|---|
1 |
mailto:bboyle@email.arizona.edu?Brad_Boyle.2013-12-10-14:57.Quantative+validations+for+SALVIAS |
trunk/validation/aggregating/plots/_archive/bien3_validations_salvias_vegbien.by=Brad@iPlant.sql.url | ||
---|---|---|
1 |
mailto:bboyle@email.arizona.edu?Brad_Boyle.2014-1-17-13:28.SALVIAS+quantitative+validation+scripts.(bien3_validations_salvias_vegbien.sql) |
trunk/validation/aggregating/plots/SALVIAS/bien3_validations_salvias_db_original.by=Brad@iPlant,Aaron@UCSB.sql | ||
---|---|---|
1 |
-- ----------------------------------------------------------------------------- |
|
2 |
-- Quantitative validation queries against the original SALVIAS database |
|
3 |
-- ------------------------------------------------------------------------- |
|
4 |
|
|
5 |
-- ------------------ |
|
6 |
-- Set variables specific to this source |
|
7 |
-- Currently configured for SALVIAS |
|
8 |
-- ------------------ |
|
9 |
|
|
10 |
USE salvias_plots; |
|
11 |
|
|
12 |
-- SALVIAS projects in BIEN |
|
13 |
-- Must filter out all others as not all SALVIAS plots exported to BIEN |
|
14 |
SET @p = "'Gentry Transect Dataset','Boyle Transects','Enquist Lab Transect Dataset','ACA Amazon Forest Inventories','Bonifacino Forest Transects','Cam Webb Borneo Plots','DeWalt Bolivia forest plots','La Selva Secondary Forest Plots','Noel Kempff Forest Plots','Noel Kempff Savanna Plots','OTS Transects','Pilon Lajas Treeplots Bolivia','RAINFOR - 0.1 ha Madre de Dios, Peru','RAINFOR - 1 ha Peru'"; |
|
15 |
|
|
16 |
-- ------------------ |
|
17 |
-- 1. Count of projects |
|
18 |
-- ------------------ |
|
19 |
SET @sql= CONCAT(' |
|
20 |
SELECT COUNT(*) AS projects |
|
21 |
FROM projects |
|
22 |
WHERE project_name IN (',@p,') |
|
23 |
LIMIT 10 |
|
24 |
'); |
|
25 |
PREPARE stmt FROM @sql; |
|
26 |
EXECUTE stmt; |
|
27 |
|
|
28 |
-- ------------------ |
|
29 |
-- 2. List of project names |
|
30 |
-- ------------------ |
|
31 |
SET @sql= CONCAT(' |
|
32 |
SELECT project_name AS projectname |
|
33 |
FROM projects |
|
34 |
WHERE project_name IN (',@p,') |
|
35 |
LIMIT 10 |
|
36 |
'); |
|
37 |
PREPARE stmt FROM @sql; |
|
38 |
EXECUTE stmt; |
|
39 |
|
|
40 |
-- ------------------ |
|
41 |
-- 3. Count of all plots in this source |
|
42 |
-- ------------------ |
|
43 |
SET @sql= CONCAT(' |
|
44 |
SELECT COUNT(DISTINCT SiteCode) AS plots |
|
45 |
FROM projects p JOIN PlotMetadata pm |
|
46 |
ON p.project_id=pm.project_id |
|
47 |
WHERE p.project_name IN (',@p,') |
|
48 |
LIMIT 10 |
|
49 |
'); |
|
50 |
PREPARE stmt FROM @sql; |
|
51 |
EXECUTE stmt; |
|
52 |
|
|
53 |
-- ------------------ |
|
54 |
-- 4. Count of plots in each project in this source |
|
55 |
-- ------------------ |
|
56 |
SET @sql= CONCAT(' |
|
57 |
SELECT p.project_name, COUNT(*) AS plots |
|
58 |
FROM projects p JOIN PlotMetadata pm |
|
59 |
ON p.project_id=pm.project_id |
|
60 |
WHERE p.project_name IN (',@p,') |
|
61 |
GROUP BY p.project_name |
|
62 |
LIMIT 10 |
|
63 |
'); |
|
64 |
PREPARE stmt FROM @sql; |
|
65 |
EXECUTE stmt; |
|
66 |
|
|
67 |
-- ------------------ |
|
68 |
-- 5.List of plot codes by project |
|
69 |
-- ------------------ |
|
70 |
SET @sql= CONCAT(' |
|
71 |
SELECT p.project_name, pm.SiteCode |
|
72 |
FROM projects p JOIN PlotMetadata pm |
|
73 |
ON p.project_id=pm.project_id |
|
74 |
WHERE p.project_name IN (',@p,') |
|
75 |
LIMIT 10 |
|
76 |
'); |
|
77 |
PREPARE stmt FROM @sql; |
|
78 |
EXECUTE stmt; |
|
79 |
|
|
80 |
-- ------------------ |
|
81 |
-- 6. List of plots with stem measurements |
|
82 |
-- ------------------ |
|
83 |
SET @sql= CONCAT(' |
|
84 |
SELECT DISTINCT p.project_name, pm.SiteCode |
|
85 |
FROM projects p JOIN PlotMetadata pm |
|
86 |
JOIN PlotObservations po JOIN stems s |
|
87 |
ON p.project_id=pm.project_id AND pm.PlotID=po.PlotID |
|
88 |
AND po.PlotObsID=s.plotobs_id |
|
89 |
WHERE p.project_name IN (',@p,') |
|
90 |
GROUP BY p.project_name, SiteCode |
|
91 |
ORDER BY p.project_name, SiteCode |
|
92 |
LIMIT 10 |
|
93 |
'); |
|
94 |
PREPARE stmt FROM @sql; |
|
95 |
EXECUTE stmt; |
|
96 |
|
|
97 |
-- ------------------ |
|
98 |
-- 7.List of plots with counts of individuals per species |
|
99 |
-- ------------------ |
|
100 |
SET @sql= CONCAT(' |
|
101 |
SELECT DISTINCT p.project_name, pm.SiteCode |
|
102 |
FROM projects p JOIN PlotMetadata pm JOIN PlotObservations po |
|
103 |
ON p.project_id=pm.project_id AND pm.PlotID=po.PlotID |
|
104 |
WHERE p.project_name IN (',@p,') |
|
105 |
AND po.NoInd>1 |
|
106 |
GROUP BY p.project_name, SiteCode |
|
107 |
ORDER BY p.project_name, SiteCode |
|
108 |
LIMIT 10 |
|
109 |
'); |
|
110 |
PREPARE stmt FROM @sql; |
|
111 |
EXECUTE stmt; |
|
112 |
|
|
113 |
-- ------------------ |
|
114 |
-- 8.List of plots which use percent cover |
|
115 |
-- ------------------ |
|
116 |
SET @sql= CONCAT(' |
|
117 |
SELECT DISTINCT p.project_name, pm.SiteCode |
|
118 |
FROM projects p JOIN PlotMetadata pm JOIN PlotObservations po |
|
119 |
ON p.project_id=pm.project_id AND pm.PlotID=po.PlotID |
|
120 |
WHERE p.project_name IN (',@p,') |
|
121 |
AND cover_percent IS NOT NULL |
|
122 |
GROUP BY p.project_name, SiteCode |
|
123 |
ORDER BY p.project_name, SiteCode |
|
124 |
LIMIT 10 |
|
125 |
'); |
|
126 |
PREPARE stmt FROM @sql; |
|
127 |
EXECUTE stmt; |
|
128 |
|
|
129 |
-- ------------------ |
|
130 |
-- 9.List of plots which use line-intercept |
|
131 |
-- ------------------ |
|
132 |
SET @sql= CONCAT(' |
|
133 |
SELECT DISTINCT p.project_name, pm.SiteCode |
|
134 |
FROM projects p JOIN PlotMetadata pm JOIN PlotObservations po |
|
135 |
ON p.project_id=pm.project_id AND pm.PlotID=po.PlotID |
|
136 |
WHERE p.project_name IN (',@p,') |
|
137 |
AND intercept_cm IS NOT NULL |
|
138 |
GROUP BY p.project_name, SiteCode |
|
139 |
ORDER BY p.project_name, SiteCode |
|
140 |
LIMIT 10 |
|
141 |
'); |
|
142 |
PREPARE stmt FROM @sql; |
|
143 |
EXECUTE stmt; |
|
144 |
|
|
145 |
-- ------------------ |
|
146 |
-- 10. Count of individuals per plot in each project |
|
147 |
-- |
|
148 |
-- Method: Sum on NoInd (number of individuals) column. |
|
149 |
-- Note 1: `individuals` should be NULL for plots which use percent cover |
|
150 |
-- or line-intercept methods. |
|
151 |
-- Note 2: Do not count records in PlotObservations table, as this will give |
|
152 |
-- incorrect total for plots such as Gentry plot, which count individuals |
|
153 |
-- per species per subplot |
|
154 |
-- ------------------ |
|
155 |
SET @sql= CONCAT(' |
|
156 |
SELECT p.project_name, SiteCode AS plotcode, |
|
157 |
SUM(NoInd) AS individuals |
|
158 |
FROM projects p JOIN PlotMetadata pm JOIN PlotObservations po |
|
159 |
ON p.project_id=pm.project_id AND pm.PlotID=po.PlotID |
|
160 |
WHERE p.project_name IN (',@p,') |
|
161 |
GROUP BY p.project_name, SiteCode |
|
162 |
ORDER BY p.project_name, SiteCode |
|
163 |
LIMIT 10 |
|
164 |
'); |
|
165 |
PREPARE stmt FROM @sql; |
|
166 |
EXECUTE stmt; |
|
167 |
|
|
168 |
-- ------------------ |
|
169 |
-- 11. Count of stems per plot in each project |
|
170 |
-- |
|
171 |
-- Method: count records in stems table |
|
172 |
-- ------------------ |
|
173 |
SET @sql= CONCAT(' |
|
174 |
SELECT p.project_name, SiteCode AS plotcode, |
|
175 |
COUNT(DISTINCT stem_id) AS stems |
|
176 |
FROM projects p JOIN PlotMetadata pm |
|
177 |
JOIN PlotObservations po JOIN stems s |
|
178 |
ON p.project_id=pm.project_id AND pm.PlotID=po.PlotID |
|
179 |
AND po.PlotObsID=s.plotobs_id |
|
180 |
WHERE p.project_name IN (',@p,') |
|
181 |
GROUP BY p.project_name, SiteCode |
|
182 |
ORDER BY p.project_name, SiteCode |
|
183 |
LIMIT 10 |
|
184 |
'); |
|
185 |
PREPARE stmt FROM @sql; |
|
186 |
EXECUTE stmt; |
|
187 |
|
|
188 |
-- ------------------ |
|
189 |
-- 12. Count of verbatim taxa per plot in each project |
|
190 |
-- |
|
191 |
-- Note: Assumes identical concatenation of taxonomic fields |
|
192 |
-- to form verbatim taxon name in vegBIEN |
|
193 |
-- ------------------ |
|
194 |
SET @sql= CONCAT(" |
|
195 |
SELECT project_name, plotcode, COUNT(taxon) AS taxa |
|
196 |
FROM |
|
197 |
( |
|
198 |
SELECT DISTINCT p.project_name, SiteCode AS plotcode, |
|
199 |
TRIM(CONCAT_WS(' ',IFNULL(Family,''),IFNULL(Genus,''),IFNULL(Species,''), |
|
200 |
IF(infra_ep_1 IS NULL,IFNULL(auth,''), |
|
201 |
TRIM(CONCAT_WS(' ',IFNULL(infra_rank_1,''),IFNULL(infra_ep_1,''),IFNULL(infra_auth_1,''))) |
|
202 |
))) AS taxon |
|
203 |
FROM projects p JOIN PlotMetadata pm JOIN PlotObservations po |
|
204 |
ON p.project_id=pm.project_id AND pm.PlotID=po.PlotID |
|
205 |
WHERE p.project_name IN (",@p,") |
|
206 |
) AS a |
|
207 |
GROUP BY project_name, plotcode |
|
208 |
ORDER BY project_name, plotcode |
|
209 |
LIMIT 10 |
|
210 |
"); |
|
211 |
PREPARE stmt FROM @sql; |
|
212 |
EXECUTE stmt; |
|
213 |
|
|
214 |
-- ------------------ |
|
215 |
-- 13. List of distinct verbatim taxa in each plot in each project |
|
216 |
-- |
|
217 |
-- Note: Assumes identical concatenation of taxonomic fields |
|
218 |
-- to form verbatim taxon name in vegBIEN. |
|
219 |
-- ------------------ |
|
220 |
SET @sql= CONCAT(" |
|
221 |
SELECT DISTINCT p.project_name, SiteCode AS plotcode, |
|
222 |
TRIM(CONCAT_WS(' ',IFNULL(Family,''),IFNULL(Genus,''),IFNULL(Species,''), |
|
223 |
IF(infra_ep_1 IS NULL,IFNULL(auth,''), |
|
224 |
TRIM(CONCAT_WS(' ',IFNULL(infra_rank_1,''),IFNULL(infra_ep_1,''),IFNULL(infra_auth_1,''))) |
|
225 |
))) AS taxon |
|
226 |
FROM projects p JOIN PlotMetadata pm JOIN PlotObservations po |
|
227 |
ON p.project_id=pm.project_id AND pm.PlotID=po.PlotID |
|
228 |
WHERE p.project_name IN (",@p,") |
|
229 |
ORDER BY p.project_name, SiteCode, taxon |
|
230 |
LIMIT 10 |
|
231 |
"); |
|
232 |
PREPARE stmt FROM @sql; |
|
233 |
EXECUTE stmt; |
|
234 |
|
|
235 |
-- ------------------ |
|
236 |
-- 14. Count of individuals per (verbatim) taxon per plot in each project |
|
237 |
-- |
|
238 |
-- Note: Assumes identical concatenation of taxonomic fields |
|
239 |
-- to form verbatim taxon name in vegBIEN |
|
240 |
-- ------------------ |
|
241 |
SET @sql= CONCAT(" |
|
242 |
SELECT DISTINCT p.project_name, SiteCode AS plotcode, |
|
243 |
TRIM(CONCAT_WS(' ',IFNULL(Family,''),IFNULL(Genus,''),IFNULL(Species,''), |
|
244 |
IF(infra_ep_1 IS NULL,IFNULL(auth,''), |
|
245 |
TRIM(CONCAT_WS(' ',IFNULL(infra_rank_1,''),IFNULL(infra_ep_1,''),IFNULL(infra_auth_1,''))) |
|
246 |
))) AS taxon, |
|
247 |
SUM(NoInd) AS individuals |
|
248 |
FROM projects p JOIN PlotMetadata pm JOIN PlotObservations po |
|
249 |
ON p.project_id=pm.project_id AND pm.PlotID=po.PlotID |
|
250 |
WHERE p.project_name IN (",@p,") |
|
251 |
GROUP BY p.project_name, SiteCode, taxon |
|
252 |
ORDER BY p.project_name, SiteCode, taxon |
|
253 |
LIMIT 10 |
|
254 |
"); |
|
255 |
PREPARE stmt FROM @sql; |
|
256 |
EXECUTE stmt; |
|
257 |
|
|
258 |
-- ------------------ |
|
259 |
-- 15. Percent cover of each (verbatim) taxon in each plot in each project |
|
260 |
-- where percent cover is recorded |
|
261 |
-- |
|
262 |
-- Note 1: Assumes identical concatenation of taxonomic fields |
|
263 |
-- to form verbatim taxon name in vegbien |
|
264 |
-- Note 2: Grouping mean cover for entire plot allows inclusion of plots with |
|
265 |
-- subplots (separate cover measure for each species in each subplot), |
|
266 |
-- plots with strata (separate cover measures for each species in each |
|
267 |
-- stratum), and plots without subplots or strata (one cover measure |
|
268 |
-- per species per plot) in same query. |
|
269 |
-- Note 3: currently, there are no SALVIAS percent cover plots in BIEN |
|
270 |
-- ------------------ |
|
271 |
SET @sql= CONCAT(" |
|
272 |
SELECT DISTINCT p.project_name, SiteCode AS plotcode, |
|
273 |
TRIM(CONCAT_WS(' ',IFNULL(Family,''),IFNULL(Genus,''),IFNULL(Species,''), |
|
274 |
IF(infra_ep_1 IS NULL,IFNULL(auth,''), |
|
275 |
TRIM(CONCAT_WS(' ',IFNULL(infra_rank_1,''),IFNULL(infra_ep_1,''),IFNULL(infra_auth_1,''))) |
|
276 |
))) AS taxon, |
|
277 |
AVG(cover_percent) AS mean_cover |
|
278 |
FROM projects p JOIN PlotMetadata pm JOIN PlotObservations po |
|
279 |
ON p.project_id=pm.project_id AND pm.PlotID=po.PlotID |
|
280 |
WHERE p.project_name IN (",@p,") |
|
281 |
AND cover_percent IS NOT NULL |
|
282 |
GROUP BY p.project_name, SiteCode, taxon |
|
283 |
ORDER BY p.project_name, SiteCode, taxon |
|
284 |
LIMIT 10 |
|
285 |
"); |
|
286 |
PREPARE stmt FROM @sql; |
|
287 |
EXECUTE stmt; |
|
288 |
|
|
289 |
-- ------------------ |
|
290 |
-- 16. Intercept values for each (verbatim) taxon in each plot in each project |
|
291 |
-- where line-intercept values are recorded |
|
292 |
-- |
|
293 |
-- Note 1: Assumes identical concatenation of taxonomic fields |
|
294 |
-- to form verbatim taxon name in vegbien |
|
295 |
-- Note 2: Grouping mean cover for entire plot combines plots with |
|
296 |
-- subplots (separate cover measure for each species in each subplot) with |
|
297 |
-- plots with strata (separate cover measures for each species in each |
|
298 |
-- stratum) with plots without subplots or strata (one cover measure |
|
299 |
-- per species per plot. |
|
300 |
-- ------------------ |
|
301 |
SET @sql= CONCAT(" |
|
302 |
SELECT DISTINCT p.project_name, SiteCode AS plotcode, |
|
303 |
TRIM(CONCAT_WS(' ',IFNULL(Family,''),IFNULL(Genus,''),IFNULL(Species,''), |
|
304 |
IF(infra_ep_1 IS NULL,IFNULL(auth,''), |
|
305 |
TRIM(CONCAT_WS(' ',IFNULL(infra_rank_1,''),IFNULL(infra_ep_1,''),IFNULL(infra_auth_1,''))) |
|
306 |
))) AS taxon, |
|
307 |
intercept_cm |
|
308 |
FROM projects p JOIN PlotMetadata pm JOIN PlotObservations po |
|
309 |
ON p.project_id=pm.project_id AND pm.PlotID=po.PlotID |
|
310 |
WHERE p.project_name IN (",@p,") |
|
311 |
AND intercept_cm IS NOT NULL |
|
312 |
ORDER BY p.project_name, SiteCode, taxon |
|
313 |
LIMIT 10 |
|
314 |
"); |
|
315 |
PREPARE stmt FROM @sql; |
|
316 |
EXECUTE stmt; |
|
317 |
|
|
318 |
-- ------------------ |
|
319 |
-- 17. Count of subplots per plot, for each project |
|
320 |
-- ------------------ |
|
321 |
SET @sql= CONCAT(" |
|
322 |
SELECT project_name, pm.SiteCode AS plotcode, COUNT(DISTINCT Line) as subplots |
|
323 |
FROM projects p JOIN PlotMetadata pm JOIN PlotObservations po |
|
324 |
ON p.project_id=pm.project_id AND pm.PlotID=po.PlotID |
|
325 |
WHERE p.project_name IN (",@p,") |
|
326 |
GROUP BY project_name, pm.SiteCode |
|
327 |
ORDER BY project_name, pm.SiteCode |
|
328 |
LIMIT 10 |
|
329 |
"); |
|
330 |
PREPARE stmt FROM @sql; |
|
331 |
EXECUTE stmt; |
|
332 |
|
|
333 |
-- ------------------ |
|
334 |
-- 18. List of subplots codes for each plot for each project |
|
335 |
-- |
|
336 |
-- Note: includes plots with and without subplots |
|
337 |
-- ------------------ |
|
338 |
SET @sql= CONCAT(" |
|
339 |
SELECT DISTINCT project_name, pm.SiteCode AS plotcode, Line as subplot |
|
340 |
FROM projects p JOIN PlotMetadata pm JOIN PlotObservations po |
|
341 |
ON p.project_id=pm.project_id AND pm.PlotID=po.PlotID |
|
342 |
WHERE p.project_name IN (",@p,") |
|
343 |
AND pm.SiteCode IS NOT NULL |
|
344 |
ORDER BY project_name, pm.SiteCode, subplot |
|
345 |
LIMIT 10 |
|
346 |
"); |
|
347 |
PREPARE stmt FROM @sql; |
|
348 |
EXECUTE stmt; |
trunk/validation/aggregating/plots/SALVIAS/_archive/bien3_validations_salvias_db_original.by=Brad@iPlant.sql | ||
---|---|---|
1 |
-- ----------------------------------------------------------------------------- |
|
2 |
-- Quantitative validation queries against the original SALVIAS database |
|
3 |
-- ------------------------------------------------------------------------- |
|
4 |
|
|
5 |
-- ------------------ |
|
6 |
-- Set variables specific to this source |
|
7 |
-- Currently configured for SALVIAS |
|
8 |
-- ------------------ |
|
9 |
|
|
10 |
USE salvias_plots; |
|
11 |
|
|
12 |
-- SALVIAS projects in BIEN |
|
13 |
-- Must filter out all others as not all SALVIAS plots exported to BIEN |
|
14 |
SET @p = "'Gentry Transect Dataset','Boyle Transects','Enquist Lab Transect Dataset','ACA Amazon Forest Inventories','Bonifacino Forest Transects','Cam Webb Borneo Plots','DeWalt Bolivia forest plots','La Selva Secondary Forest Plots','Noel Kempff Forest Plots','Noel Kempff Savanna Plots','OTS Transects','Pilon Lajas Treeplots Bolivia','RAINFOR - 0.1 ha Madre de Dios, Peru','RAINFOR - 1 ha Peru'"; |
|
15 |
|
|
16 |
-- ------------------ |
|
17 |
-- 1. Count of projects |
|
18 |
-- ------------------ |
|
19 |
SET @sql= CONCAT(' |
|
20 |
SELECT COUNT(*) AS projects |
|
21 |
FROM projects |
|
22 |
WHERE project_name IN (',@p,') |
|
23 |
'); |
|
24 |
PREPARE stmt FROM @sql; |
|
25 |
EXECUTE stmt; |
|
26 |
|
|
27 |
-- ------------------ |
|
28 |
-- 2. List of project names |
|
29 |
-- ------------------ |
|
30 |
SET @sql= CONCAT(' |
|
31 |
SELECT project_name AS projectname |
|
32 |
FROM projects |
|
33 |
WHERE project_name IN (',@p,') |
|
34 |
'); |
|
35 |
PREPARE stmt FROM @sql; |
|
36 |
EXECUTE stmt; |
|
37 |
|
|
38 |
-- ------------------ |
|
39 |
-- 3. Count of all plots in this source |
|
40 |
-- ------------------ |
|
41 |
SET @sql= CONCAT(' |
|
42 |
SELECT COUNT(DISTINCT SiteCode) AS plots |
|
43 |
FROM projects p JOIN PlotMetadata pm |
|
44 |
ON p.project_id=pm.project_id |
|
45 |
WHERE p.project_name IN (',@p,') |
|
46 |
'); |
|
47 |
PREPARE stmt FROM @sql; |
|
48 |
EXECUTE stmt; |
|
49 |
|
|
50 |
-- ------------------ |
|
51 |
-- 4. Count of plots in each project in this source |
|
52 |
-- ------------------ |
|
53 |
SET @sql= CONCAT(' |
|
54 |
SELECT p.project_name, COUNT(*) AS plots |
|
55 |
FROM projects p JOIN PlotMetadata pm |
|
56 |
ON p.project_id=pm.project_id |
|
57 |
WHERE p.project_name IN (',@p,') |
|
58 |
GROUP BY p.project_name |
|
59 |
'); |
|
60 |
PREPARE stmt FROM @sql; |
|
61 |
EXECUTE stmt; |
|
62 |
|
|
63 |
-- ------------------ |
|
64 |
-- 5.List of plot codes by project |
|
65 |
-- ------------------ |
|
66 |
SET @sql= CONCAT(' |
|
67 |
SELECT p.project_name, pm.SiteCode |
|
68 |
FROM projects p JOIN PlotMetadata pm |
|
69 |
ON p.project_id=pm.project_id |
|
70 |
WHERE p.project_name IN (',@p,') |
|
71 |
'); |
|
72 |
PREPARE stmt FROM @sql; |
|
73 |
EXECUTE stmt; |
|
74 |
|
|
75 |
-- ------------------ |
|
76 |
-- 6. List of plots with stem measurements |
|
77 |
-- ------------------ |
|
78 |
SET @sql= CONCAT(' |
|
79 |
SELECT DISTINCT p.project_name, pm.SiteCode |
|
80 |
FROM projects p JOIN PlotMetadata pm |
|
81 |
JOIN PlotObservations po JOIN stems s |
|
82 |
ON p.project_id=pm.project_id AND pm.PlotID=po.PlotID |
|
83 |
AND po.PlotObsID=s.plotobs_id |
|
84 |
WHERE p.project_name IN (',@p,') |
|
85 |
GROUP BY p.project_name, SiteCode |
|
86 |
ORDER BY p.project_name, SiteCode |
|
87 |
'); |
|
88 |
PREPARE stmt FROM @sql; |
|
89 |
EXECUTE stmt; |
|
90 |
|
|
91 |
-- ------------------ |
|
92 |
-- 7.List of plots with counts of individuals per species |
|
93 |
-- ------------------ |
|
94 |
SET @sql= CONCAT(' |
|
95 |
SELECT DISTINCT p.project_name, pm.SiteCode |
|
96 |
FROM projects p JOIN PlotMetadata pm JOIN PlotObservations po |
|
97 |
ON p.project_id=pm.project_id AND pm.PlotID=po.PlotID |
|
98 |
WHERE p.project_name IN (',@p,') |
|
99 |
AND po.NoInd>1 |
|
100 |
GROUP BY p.project_name, SiteCode |
|
101 |
ORDER BY p.project_name, SiteCode |
|
102 |
'); |
|
103 |
PREPARE stmt FROM @sql; |
|
104 |
EXECUTE stmt; |
|
105 |
|
|
106 |
-- ------------------ |
|
107 |
-- 8.List of plots which use percent cover |
|
108 |
-- ------------------ |
|
109 |
SET @sql= CONCAT(' |
|
110 |
SELECT DISTINCT p.project_name, pm.SiteCode |
|
111 |
FROM projects p JOIN PlotMetadata pm JOIN PlotObservations po |
|
112 |
ON p.project_id=pm.project_id AND pm.PlotID=po.PlotID |
|
113 |
WHERE p.project_name IN (',@p,') |
|
114 |
AND cover_percent IS NOT NULL |
|
115 |
GROUP BY p.project_name, SiteCode |
|
116 |
ORDER BY p.project_name, SiteCode |
|
117 |
'); |
|
118 |
PREPARE stmt FROM @sql; |
|
119 |
EXECUTE stmt; |
|
120 |
|
|
121 |
-- ------------------ |
|
122 |
-- 9.List of plots which use line-intercept |
|
123 |
-- ------------------ |
|
124 |
SET @sql= CONCAT(' |
|
125 |
SELECT DISTINCT p.project_name, pm.SiteCode |
|
126 |
FROM projects p JOIN PlotMetadata pm JOIN PlotObservations po |
|
127 |
ON p.project_id=pm.project_id AND pm.PlotID=po.PlotID |
|
128 |
WHERE p.project_name IN (',@p,') |
|
129 |
AND intercept_cm IS NOT NULL |
|
130 |
GROUP BY p.project_name, SiteCode |
|
131 |
ORDER BY p.project_name, SiteCode |
|
132 |
'); |
|
133 |
PREPARE stmt FROM @sql; |
|
134 |
EXECUTE stmt; |
|
135 |
|
|
136 |
-- ------------------ |
|
137 |
-- 10. Count of individuals per plot in each project |
|
138 |
-- |
|
139 |
-- Method: Sum on NoInd (number of individuals) column. |
|
140 |
-- Note 1: `individuals` should be NULL for plots which use percent cover |
|
141 |
-- or line-intercept methods. |
|
142 |
-- Note 2: Do not count records in PlotObservations table, as this will give |
|
143 |
-- incorrect total for plots such as Gentry plot, which count individuals |
|
144 |
-- per species per subplot |
|
145 |
-- ------------------ |
|
146 |
SET @sql= CONCAT(' |
|
147 |
SELECT p.project_name, SiteCode AS plotcode, |
|
148 |
SUM(NoInd) AS individuals |
|
149 |
FROM projects p JOIN PlotMetadata pm JOIN PlotObservations po |
|
150 |
ON p.project_id=pm.project_id AND pm.PlotID=po.PlotID |
|
151 |
WHERE p.project_name IN (',@p,') |
|
152 |
GROUP BY p.project_name, SiteCode |
|
153 |
ORDER BY p.project_name, SiteCode |
|
154 |
'); |
|
155 |
PREPARE stmt FROM @sql; |
|
156 |
EXECUTE stmt; |
|
157 |
|
|
158 |
-- ------------------ |
|
159 |
-- 11. Count of stems per plot in each project |
|
160 |
-- |
|
161 |
-- Method: count records in stems table |
|
162 |
-- ------------------ |
|
163 |
SET @sql= CONCAT(' |
|
164 |
SELECT p.project_name, SiteCode AS plotcode, |
|
165 |
COUNT(DISTINCT stem_id) AS stems |
|
166 |
FROM projects p JOIN PlotMetadata pm |
|
167 |
JOIN PlotObservations po JOIN stems s |
|
168 |
ON p.project_id=pm.project_id AND pm.PlotID=po.PlotID |
|
169 |
AND po.PlotObsID=s.plotobs_id |
|
170 |
WHERE p.project_name IN (',@p,') |
|
171 |
GROUP BY p.project_name, SiteCode |
|
172 |
ORDER BY p.project_name, SiteCode |
|
173 |
'); |
|
174 |
PREPARE stmt FROM @sql; |
|
175 |
EXECUTE stmt; |
|
176 |
|
|
177 |
-- ------------------ |
|
178 |
-- 12. Count of verbatim taxa per plot in each project |
|
179 |
-- |
|
180 |
-- Note: Assumes identical concatenation of taxonomic fields |
|
181 |
-- to form verbatim taxon name in vegBIEN |
|
182 |
-- ------------------ |
|
183 |
SET @sql= CONCAT(" |
|
184 |
SELECT project_name, plotcode, COUNT(taxon) AS taxa |
|
185 |
FROM |
|
186 |
( |
|
187 |
SELECT DISTINCT p.project_name, SiteCode AS plotcode, |
|
188 |
TRIM(CONCAT_WS(' ',IFNULL(Family,''),IFNULL(Genus,''),IFNULL(Species,''), |
|
189 |
IF(infra_ep_1 IS NULL,IFNULL(auth,''), |
|
190 |
TRIM(CONCAT_WS(' ',IFNULL(infra_rank_1,''),IFNULL(infra_ep_1,''),IFNULL(infra_auth_1,''))) |
|
191 |
))) AS taxon |
|
192 |
FROM projects p JOIN PlotMetadata pm JOIN PlotObservations po |
|
193 |
ON p.project_id=pm.project_id AND pm.PlotID=po.PlotID |
|
194 |
WHERE p.project_name IN (",@p,") |
|
195 |
) AS a |
|
196 |
GROUP BY project_name, plotcode |
|
197 |
ORDER BY project_name, plotcode |
|
198 |
"); |
|
199 |
PREPARE stmt FROM @sql; |
|
200 |
EXECUTE stmt; |
|
201 |
|
|
202 |
-- ------------------ |
|
203 |
-- 13. List of distinct verbatim taxa in each plot in each project |
|
204 |
-- |
|
205 |
-- Note: Assumes identical concatenation of taxonomic fields |
|
206 |
-- to form verbatim taxon name in vegBIEN. |
|
207 |
-- ------------------ |
|
208 |
SET @sql= CONCAT(" |
|
209 |
SELECT DISTINCT p.project_name, SiteCode AS plotcode, |
|
210 |
TRIM(CONCAT_WS(' ',IFNULL(Family,''),IFNULL(Genus,''),IFNULL(Species,''), |
|
211 |
IF(infra_ep_1 IS NULL,IFNULL(auth,''), |
|
212 |
TRIM(CONCAT_WS(' ',IFNULL(infra_rank_1,''),IFNULL(infra_ep_1,''),IFNULL(infra_auth_1,''))) |
|
213 |
))) AS taxon |
|
214 |
FROM projects p JOIN PlotMetadata pm JOIN PlotObservations po |
|
215 |
ON p.project_id=pm.project_id AND pm.PlotID=po.PlotID |
|
216 |
WHERE p.project_name IN (",@p,") |
|
217 |
ORDER BY p.project_name, SiteCode, taxon |
|
218 |
"); |
|
219 |
PREPARE stmt FROM @sql; |
|
220 |
EXECUTE stmt; |
|
221 |
|
|
222 |
-- ------------------ |
|
223 |
-- 14. Count of individuals per (verbatim) taxon per plot in each project |
|
224 |
-- |
|
225 |
-- Note: Assumes identical concatenation of taxonomic fields |
|
226 |
-- to form verbatim taxon name in vegBIEN |
|
227 |
-- ------------------ |
|
228 |
SET @sql= CONCAT(" |
|
229 |
SELECT DISTINCT p.project_name, SiteCode AS plotcode, |
|
230 |
TRIM(CONCAT_WS(' ',IFNULL(Family,''),IFNULL(Genus,''),IFNULL(Species,''), |
|
231 |
IF(infra_ep_1 IS NULL,IFNULL(auth,''), |
|
232 |
TRIM(CONCAT_WS(' ',IFNULL(infra_rank_1,''),IFNULL(infra_ep_1,''),IFNULL(infra_auth_1,''))) |
|
233 |
))) AS taxon, |
|
234 |
SUM(NoInd) AS individuals |
|
235 |
FROM projects p JOIN PlotMetadata pm JOIN PlotObservations po |
|
236 |
ON p.project_id=pm.project_id AND pm.PlotID=po.PlotID |
|
237 |
WHERE p.project_name IN (",@p,") |
|
238 |
GROUP BY p.project_name, SiteCode, taxon |
|
239 |
ORDER BY p.project_name, SiteCode, taxon |
|
240 |
"); |
|
241 |
PREPARE stmt FROM @sql; |
Also available in: Unified diff
validation/**: removed by=... indicators so that we don't have to add an author name every time a file is changed. the author indicator is not needed now that both Brad's and my edits are open-source.