Project

General

Profile

1
-- -------------------------------------------------------------------------------
2
-- Quantitative validation queries against the trait table in db bien2 on nimoy
3
--
4
-- For all queries, omit records where taxon or traitName are null or blank
5
-- This filtering would have been performed prior to import
6
-- -------------------------------------------------------------------------------
7

    
8
SET search_path TO "bien2_traits", util;
9

    
10
-- ------------------
11
-- 1. Count records
12
-- ------------------
13
CREATE OR REPLACE VIEW _traits_01_count_records AS
14
SELECT COUNT(*) AS totalrecords
15
FROM "TraitObservation"
16
WHERE "taxonName" IS NOT NULL AND "taxonName"<>'' AND "measurementName" IS NOT NULL AND "measurementName"<>''
17
;
18

    
19
-- ------------------
20
-- 2. Count trait names
21
-- ------------------
22
CREATE OR REPLACE VIEW _traits_02_count_trait_names AS
23
SELECT COUNT(DISTINCT "measurementName") AS traits
24
FROM "TraitObservation"
25
WHERE "taxonName" IS NOT NULL AND "taxonName"<>'' AND "measurementName" IS NOT NULL AND "measurementName"<>''
26
;
27

    
28
-- ------------------
29
-- 3. List trait names
30
-- ------------------
31
CREATE OR REPLACE VIEW _traits_03_list_trait_names AS
32
SELECT DISTINCT "measurementName" AS trait
33
FROM "TraitObservation"
34
WHERE "taxonName" IS NOT NULL AND "taxonName"<>'' AND "measurementName" IS NOT NULL AND "measurementName"<>''
35
ORDER BY trait
36
;
37

    
38
-- ------------------
39
-- 4. Count records per trait
40
-- ------------------
41
CREATE OR REPLACE VIEW _traits_04_count_records_per_trait AS
42
SELECT "measurementName" AS trait, COUNT(*) AS measurements
43
FROM "TraitObservation"
44
WHERE "taxonName" IS NOT NULL AND "taxonName"<>'' AND "measurementName" IS NOT NULL AND "measurementName"<>''
45
GROUP BY trait
46
ORDER BY trait
47
;
48

    
49
-- ------------------
50
-- 5. Count taxa
51
-- ------------------
52
CREATE OR REPLACE VIEW _traits_05_count_taxa AS
53
SELECT COUNT(DISTINCT
54
TRIM(CONCAT_WS(' ',IFNULL("taxonName",''),IFNULL("scientificNameAuthorship",'')))
55
) AS taxa
56
FROM "TraitObservation"
57
WHERE "taxonName" IS NOT NULL AND "taxonName"<>'' AND "measurementName" IS NOT NULL AND "measurementName"<>''
58
;
59

    
60
-- ------------------
61
-- 6. List distinct taxa
62
-- ------------------
63
CREATE OR REPLACE VIEW _traits_06_list_distinct_taxa AS
64
SELECT DISTINCT
65
TRIM(CONCAT_WS(' ',IFNULL("taxonName",''),IFNULL("scientificNameAuthorship",'')))
66
AS taxonwithauthor
67
FROM "TraitObservation"
68
WHERE "taxonName" IS NOT NULL AND "taxonName"<>'' AND "measurementName" IS NOT NULL AND "measurementName"<>''
69
ORDER BY taxonwithauthor
70
;
71

    
72
-- ------------------
73
-- 7. Trait, value and units
74
-- ------------------
75
CREATE OR REPLACE VIEW _traits_07_trait_value_and_units AS
76
SELECT "measurementName" AS trait, "measurementValue" AS value, "measurementUnit" AS units
77
FROM "TraitObservation"
78
WHERE "taxonName" IS NOT NULL AND "taxonName"<>'' AND "measurementName" IS NOT NULL AND "measurementName"<>''
79
ORDER BY trait, "value", units
80
;
81

    
82
-- ------------------
83
-- 8. "taxonName", trait and value for first 5000 records
84
-- ------------------
85
CREATE OR REPLACE VIEW _traits_08_taxonname_trait_and_value_for_first_5000_records AS
86
SELECT
87
TRIM(CONCAT_WS(' ',IFNULL("taxonName",''),IFNULL("scientificNameAuthorship",'')))
88
AS taxonwithauthor,
89
"measurementName" AS trait, "measurementValue" AS "value"
90
FROM "TraitObservation"
91
WHERE "taxonName" IS NOT NULL AND "taxonName"<>'' AND "measurementName" IS NOT NULL AND "measurementName"<>''
92
ORDER BY taxonwithauthor, trait, "value"
93
;
(1-1/2)