@@ -7,22 +7,42 @@ 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+ SELECT
30+ projects.id AS project_id,
31+ projects.project,
32+ projects.descrip,
33+ projects.timescale_id,
34+ COUNT(DISTINCT units_sections.col_id)::integer AS t_cols,
35+ COALESCE(c, 0)::integer AS in_process_cols,
36+ COALESCE(co, 0)::integer AS obsolete_cols,
37+ COUNT(DISTINCT units_sections.unit_id)::integer AS t_units,
38+ COALESCE(ROUND(total_area), 0)::integer AS area
39+
2140 FROM macrostrat.projects
2241 LEFT JOIN macrostrat.cols ON projects.id = cols.project_id
2342 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)
43+ LEFT JOIN in_proc USING (project_id)
44+ LEFT JOIN obs USING (project_id)
45+ LEFT JOIN col_area_sum ON projects.id = col_area_sum.project_id
2646 ` ;
2747
2848 var where = [ ] ;
@@ -35,7 +55,14 @@ module.exports = function (req, res, next, cb) {
3555 if ( where . length ) {
3656 sql += ` WHERE ${ where . join ( " AND " ) } ` ;
3757 }
38- sql += "\nGROUP BY projects.id, in_proc.c, obs.co" ;
58+ sql += `\nGROUP BY
59+ projects.id,
60+ projects.project,
61+ projects.descrip,
62+ projects.timescale_id,
63+ c,
64+ co,
65+ total_area;` ;
3966
4067 larkin . queryPg ( "burwell" , sql , params , function ( error , data ) {
4168 if ( error ) {
@@ -65,4 +92,4 @@ module.exports = function (req, res, next, cb) {
6592 ) ;
6693 }
6794 } ) ;
68- } ;
95+ } ;
0 commit comments