1- SELECT
1+ WITH dist_units AS (
2+ SELECT sp .entityid , vu .variableunits
3+ FROM ndb .speleothems sp
4+ LEFT JOIN ndb .variableunits vu ON sp .entrancedistanceunits = vu .variableunitsid
5+ ),
6+ speleothem_type AS (
7+ SELECT sp .entityid , st .speleothemtype
8+ FROM ndb .speleothems sp
9+ LEFT JOIN ndb .speleothemtypes st ON sp .speleothemtypeid = st .speleothemtypeid
10+ ),
11+ speleothem_dt AS (
12+ SELECT en .entityid , dt .speleothemdriptype , en .entitydripheight , vu .variableunits AS dripheightunits
13+ FROM ndb .entitydripheight en
14+ LEFT JOIN ndb .speleothemdriptypes dt ON en .speleothemdriptypeid = dt .speleothemdriptypeid
15+ LEFT JOIN ndb .variableunits vu ON en .entitydripheightunit = vu .variableunitsid
16+ ),
17+ entitycovers AS (
18+ SELECT ec .entityid , ect .entitycovertype , ec .entitycoverthickness
19+ FROM ndb .entitycovers ec
20+ LEFT JOIN ndb .entitycovertypes ect ON ec .entitycoverid = ect .entitycoverid
21+ ),
22+ geology AS (
23+ SELECT g .entityid , ra .relativeage
24+ FROM ndb .entitygeology g
25+ LEFT JOIN ndb .relativeages ra ON g .speleothemgeologyid = ra .relativeageid
26+ ),
27+ landusecover AS (
28+ SELECT luc .entityid , vct .vegetationcovertype , luc .landusecoverpercent
29+ FROM ndb .entitylandusecover luc
30+ LEFT JOIN ndb .vegetationcovertypes vct ON luc .landusecovertypeid = vct .vegetationcovertypeid
31+ ),
32+ vegetationcovertypes AS (
33+ SELECT evc .entityid , vct .vegetationcovertype , evc .vegetationcoverpercent
34+ FROM ndb .entityvegetationcover evc
35+ LEFT JOIN ndb .vegetationcovertypes vct ON evc .vegetationcovertypeid = vct .vegetationcovertypeid
36+ )
37+ SELECT DISTINCT
238jsonb_build_object( ' siteid' , sp .siteid ,
339 ' collectionunitid' , scu .collectionunitid ,
440 ' entityid' , sp .entityid ,
541 ' entityname' , sp .entityname ,
42+ ' speleothemtype' , st .speleothemtype ,
43+ ' speleothemdriptype' , sdt .speleothemdriptype ,
44+ ' dripheight' , sdt .entitydripheight ,
45+ ' dripheightunits' , sdt .dripheightunits ,
46+ ' entitycovertype' , ec .entitycovertype ,
47+ ' entitycoverthickness' , ec .entitycoverthickness ,
48+ ' relativeage' , g .relativeage ,
49+ ' vegetationcovertype' , vct .vegetationcovertype ,
50+ ' vegetationcoverpercent' , vct .vegetationcoverpercent ,
51+ ' landusecovertype' , luc .vegetationcovertype ,
52+ ' landusecoverpercent' , luc .landusecoverpercent ,
653 ' monitoring' , sp .monitoring ,
7- ' rockageid' , sp .rockageid , -- check if there is a rockageid
854 ' entrancedistance' , sp .entrancedistance ,
9- ' entrancedistanceunits' , sp .entrancedistanceunits ,
10- ' speleothemtypeid' , sp .speleothemtypeid ) AS speleothem -- create a join to extract the type rather than just the id
55+ ' entrancedistanceunits' , sp .entrancedistanceunits ) AS speleothem
1156FROM ndb .speleothems sp
12- LEFT JOIN ndb .speleothemcollectionunits scu
13- ON sp .entityid = scu .entityid
14- WHERE scu .collectionunitid IN ($1 :csv);
57+ LEFT JOIN ndb .speleothemcollectionunits scu ON scu .entityid = sp .entityid
58+ LEFT JOIN ndb .collectionunits cu ON scu .collectionunitid = cu .collectionunitid
59+ LEFT JOIN ndb .datasets ds ON cu .collectionunitid = ds .collectionunitid
60+ LEFT JOIN dist_units du ON du .entityid = sp .entityid
61+ LEFT JOIN speleothem_type st ON st .entityid = sp .entityid
62+ LEFT JOIN speleothem_dt sdt ON sdt .entityid = sp .entityid
63+ LEFT JOIN entitycovers ec ON ec .entityid = sp .entityid
64+ LEFT JOIN geology g ON g .entityid = sp .entityid
65+ LEFT JOIN landusecover luc ON luc .entityid = sp .entityid
66+ LEFT JOIN vegetationcovertypes vct ON vct .entityid = sp .entityid
67+ WHERE cu .collectionunitid IN ($1 :csv);
0 commit comments