@@ -7,22 +7,44 @@ module.exports = function (req, res, next, cb) {
77 }
88 //There will be a discrepancy with a key in production. Updated in_proccess_cols to in_process_cols key. Values are
99 //still the same.
10- var sql = `WITH RECURSIVE in_proc AS (SELECT count(distinct id) c,project_id from macrostrat.cols where status_code='in process' group by project_id),
11- obs AS (SELECT count(distinct id) co,project_id from macrostrat.cols where status_code='obsolete' group by project_id)
12- SELECT projects.id AS project_id,
13- project,
14- descrip,
15- timescale_id,
16- COUNT(DISTINCT units_sections.col_id)::integer AS t_cols,
17- coalesce(c,0)::integer as in_process_cols,
18- coalesce(co,0)::integer as obsolete_cols,
19- COUNT(DISTINCT units_sections.unit_id)::integer AS t_units,
20- round(SUM(cols.col_area)::integer,0)::integer as area
10+ var sql = `WITH in_proc AS (
11+ SELECT COUNT(DISTINCT id) AS c, project_id
12+ FROM macrostrat.cols
13+ WHERE status_code = 'in process'
14+ GROUP BY project_id
15+ ),
16+ obs AS (
17+ SELECT COUNT(DISTINCT id) AS co, project_id
18+ FROM macrostrat.cols
19+ WHERE status_code = 'obsolete'
20+ GROUP BY project_id
21+ ),
22+ col_area_sum AS (
23+ SELECT project_id, SUM(col_area) AS total_area
24+ FROM macrostrat.cols
25+ WHERE status_code = 'active'
26+ GROUP BY project_id
27+ )
28+
29+
30+ SELECT
31+ projects.id AS project_id,
32+ projects.project,
33+ projects.descrip,
34+ projects.timescale_id,
35+ COUNT(DISTINCT units_sections.col_id)::integer AS t_cols,
36+ COALESCE(c, 0)::integer AS in_process_cols,
37+ COALESCE(co, 0)::integer AS obsolete_cols,
38+ COUNT(DISTINCT units_sections.unit_id)::integer AS t_units,
39+ COALESCE(ROUND(total_area), 0)::integer AS area
40+
41+
2142 FROM macrostrat.projects
2243 LEFT JOIN macrostrat.cols ON projects.id = cols.project_id
2344 LEFT JOIN macrostrat.units_sections ON units_sections.col_id = cols.id
24- LEFT JOIN in_proc using (project_id)
25- left join obs using (project_id)
45+ LEFT JOIN in_proc USING (project_id)
46+ LEFT JOIN obs USING (project_id)
47+ LEFT JOIN col_area_sum ON projects.id = col_area_sum.project_id
2648 ` ;
2749
2850 var where = [ ] ;
@@ -35,7 +57,14 @@ module.exports = function (req, res, next, cb) {
3557 if ( where . length ) {
3658 sql += ` WHERE ${ where . join ( " AND " ) } ` ;
3759 }
38- sql += "\nGROUP BY projects.id, in_proc.c, obs.co" ;
60+ sql += `\nGROUP BY
61+ projects.id,
62+ projects.project,
63+ projects.descrip,
64+ projects.timescale_id,
65+ c,
66+ co,
67+ total_area;` ;
3968
4069 larkin . queryPg ( "burwell" , sql , params , function ( error , data ) {
4170 if ( error ) {
0 commit comments