-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchronologybyid.sql
More file actions
50 lines (49 loc) · 2.04 KB
/
chronologybyid.sql
File metadata and controls
50 lines (49 loc) · 2.04 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
CREATE OR REPLACE FUNCTION da.chronologybyid(_chronid integer)
RETURNS TABLE(controls json,
"Default" boolean,
ChronologyName character varying,
AgeType character varying,
AgeModel character varying,
AgeOlder integer,
ChronologyID integer,
AgeYounger integer,
datasets json,
notes character varying,
dateprepared date)
LANGUAGE sql
AS $function$
SELECT json_agg(json_build_object('AgeYoungest', ccr.agelimityounger,
'Age', ccr.age,
'ControlType', ccrt.chroncontroltype,
'ChronControlID', ccr.chroncontrolid,
'Depth', ccr.depth,
'AgeOldest', ccr.agelimitolder,
'Thickness', ccr.thickness)) AS controls,
chr.isdefault AS Default,
chr.chronologyname AS ChronologyName,
aty.agetype AS AgeType,
chr.agemodel AS AgeModel,
chr.ageboundolder AS AgeOlder,
chr.chronologyid AS ChronologyID,
chr.ageboundyounger AS AgeYounger,
json_agg(DISTINCT jsonb_build_object('DatasetType', dst.datasettype,
'DatasetID', ds.datasetid)) AS datasets,
chr.notes AS Notes,
chr.dateprepared AS DatePrepared
FROM ndb.chronologies AS chr
INNER JOIN ndb.agetypes AS aty ON chr.agetypeid = aty.agetypeid
LEFT JOIN ndb.chroncontrols AS ccr ON chr.chronologyid = ccr.chronologyid
LEFT JOIN ndb.chroncontroltypes AS ccrt ON ccr.chroncontroltypeid = ccrt.chroncontroltypeid
LEFT JOIN ndb.datasets AS ds ON ds.collectionunitid = chr.collectionunitid
LEFT JOIN ndb.datasettypes AS dst ON dst.datasettypeid = ds.datasettypeid
WHERE chr.chronologyid = _chronid
GROUP BY chr.isdefault,
chr.chronologyname,
aty.agetype,
chr.ageboundolder,
chr.ageboundyounger,
chr.agemodel,
chr.notes,
chr.chronologyid,
chr.dateprepared
$function$