mysql - Need to SUM All Rows by a Non-Numeric Condition AND a GROUP BY -
i have table contains condition need test , total group total of number of groups equal value.
example:
- group 1 total of conditions = 1
- group 2 total of conditions = 14
- group 3 total of conditions = 5
- group 4 total of conditions = 1
etc.
i need calculate total of each group total groups = 1
this base code have come basic group totals. (i'm using sum(if(condition = true,1,0)) in effort build query can used subquery):
select sum(if(`condition` <> '' , `condition` not null,1,0)) totalerrors site_analytics group fourid;
of course same as:
select count(*) totalerrors site_analytics group fourid `condition` <> '' , `condition` not null;
returns row each group group total. need group groups group total.
either way need number of groups (fourid) = 1 (or 2,3,4 etc)
the end result i'm looking generate report has number of groups = 1, 2,3,4,5,5+
i have tried using above queries sub-queries no success such as:
select sum((select sum(if(`condition` <> '' , `condition` not null,1,0)) site_analytics group fourid) `siteswith1error` sitelog_master;
this query used getting other totals similar report:
select (select count(`fourid`) sitelog_master) `totalsites`, (select count(`condition`) site_analytics (`condition` <> '' , `condition` not null)) `totalfieldswitherrors`, (select count(distinct m.fourid) sitelog_master m left join site_analytics using (fourid) (`condition` null or `condition` <> '')) `totalsiteswitherrors`, (select count(`condition`) site_analytics `condition` '%required%') `totalmissingrequiredfields`, (select count(distinct `condition`) site_analytics (`condition` <> '' , `condition` not null)) `totaluniquetypesoferrors` sitelog_master limit 1;
this came with. moved sub-query clause instead of trying in select clause:
select sum(t.totalerrors) (select sum(if(`condition` <> '' , `condition` not null,1,0)) totalerrors site_a nalytics group fourid having totalerrors = 1) t;
the sub-query returns 45 rows sum in select clause sums single row.
to add remaining breakouts need. if() in select clause sum , remove having from clause can create other summed totals:
select sum(if(t.totalerrors = 1,1,0)) group1, sum(if(t.totalerrors = 2,1,0)) group2, sum(if(t.totalerrors = 3,1,0)) group3, sum(if(t.totalerrors = 4,1,0)) group4, sum(if(t.totalerrors >= 5,1,0)) group5 (select sum(if(`condition` <> '' , `condition` not null,1,0)) totalerrors site_analytics group fourid) t;
returns:
group1 45
group2 76
group3 90
group4 57
group5+ 360
Comments
Post a Comment