Project

General

Profile

1
-- -------------------------------------------------------------------------------
2
-- Quantitative validation queries against table Trait in BIEN3 postgres db
3
-- 
4
-- DB: vegbien
5
-- Host: vegbiendev.nceas.ucsb.edu
6
-- -------------------------------------------------------------------------------
7

    
8
SET search_path TO public_validations, public;
9

    
10
SET enable_seqscan = off;
11
SET join_collapse_limit = 1; -- turn it off
12

    
13
-- ------------------
14
-- 1. Count records
15
-- ------------------
16
CREATE VIEW _traits_01_count_records AS
17
SELECT COUNT(*) AS "totalRecords"
18
FROM trait
19
JOIN taxonoccurrence USING (taxonoccurrence_id)
20
WHERE taxonoccurrence.source_id = (SELECT source_by_shortname(current_schema))
21
;
22

    
23
-- ------------------
24
-- 2. Count trait names
25
-- ------------------
26
CREATE VIEW _traits_02_count_trait_names AS
27
SELECT COUNT(DISTINCT name) AS traits
28
FROM trait
29
JOIN taxonoccurrence USING (taxonoccurrence_id)
30
WHERE taxonoccurrence.source_id = (SELECT source_by_shortname(current_schema));
31

    
32
-- ------------------
33
-- 3. List trait names
34
-- ------------------
35
CREATE VIEW _traits_03_list_trait_names AS
36
SELECT DISTINCT name AS trait
37
FROM trait
38
JOIN taxonoccurrence USING (taxonoccurrence_id)
39
WHERE taxonoccurrence.source_id = (SELECT source_by_shortname(current_schema))
40
ORDER BY name
41
;
42

    
43
-- ------------------
44
-- 4. Count records per trait
45
-- ------------------
46
CREATE VIEW _traits_04_count_records_per_trait AS
47
SELECT name AS trait, COUNT(*) AS measurements
48
FROM trait
49
JOIN taxonoccurrence USING (taxonoccurrence_id)
50
WHERE taxonoccurrence.source_id = (SELECT source_by_shortname(current_schema))
51
GROUP BY name
52
ORDER BY name
53
;
54

    
55
-- ------------------
56
-- 5. Count taxa
57
-- 
58
-- Note: No morphospecies in trait table, therefore count
59
-- 		taxon + authority only
60
-- ------------------
61
CREATE VIEW _traits_05_count_taxa AS
62
SELECT COUNT(DISTINCT (taxonname, author)) AS taxa
63
FROM taxonverbatim
64
WHERE source_id = (SELECT source_by_shortname(current_schema))
65
;
66

    
67
-- ------------------
68
-- 6. List taxa
69
-- 
70
-- Note 1: No morphospecies in trait table, use taxon + authority 
71
-- Note 2: Note formation of taxonCorrected: includes family ONLY if
72
-- taxon is not determined at least to genus
73
-- ------------------
74
CREATE VIEW _traits_06_list_taxa AS
75
SELECT DISTINCT concat_ws(' ', taxonname, author) AS taxonwithauthor
76
FROM taxonverbatim
77
WHERE source_id = (SELECT source_by_shortname(current_schema))
78
ORDER BY concat_ws(' ', taxonname, author)
79
;
80

    
81
-- ------------------
82
-- 7. Trait, value and units for first 5000 records 
83
-- ------------------
84
CREATE VIEW _traits_07_trait_value_and_units_for_first_5000_records AS
85
SELECT name AS trait, value, units
86
FROM trait
87
JOIN taxonoccurrence USING (taxonoccurrence_id)
88
WHERE taxonoccurrence.source_id = (SELECT source_by_shortname(current_schema))
89
ORDER BY name, value, units
90
LIMIT 5000
91
;
92

    
93
-- ------------------
94
-- 8. Taxon, trait and value for first 5000 records 
95
-- ------------------
96
CREATE VIEW _traits_08_taxon_trait_and_value_for_first_5000_records AS
97
SELECT concat_ws(' ', taxonname, author) AS taxonwithauthor, name AS trait, value
98
FROM taxonverbatim
99
JOIN taxondetermination ON taxondetermination.taxonverbatim_id = taxonverbatim.taxonverbatim_id AND taxondetermination.iscurrent
100
JOIN trait USING (taxonoccurrence_id)
101
WHERE taxonverbatim.source_id = (SELECT source_by_shortname(current_schema))
102
ORDER BY taxonname
103
LIMIT 5000
104
;
    (1-1/1)