mysql - How can I count the number of entities that don't have certain attributes grouped by a common attribute in an EAV(ish) schema? -
i'm trying count region of number of files don't contain "important" attributes given following dataset:
files ------------------------------------ id | file | region ------------------------------------ 1 | data.xml | eastern 2 | 2011-01-01-report.xml | eastern 3 | regional report.xml | western 4 | data.xml | central 5 | 2010 summary.xml | eastern file_attributes -------------------------------------------- file_id | attribute | value | importance -------------------------------------------- 1 | patients | 18 | 0 1 | deaths | 17 | 1 2 | clients | 5 | 0 3 | refunds | 12 | 1 5 | deaths | 4 | 1
i can count of number of files have important attributes this:
select region , count(f.id) file_count , count(distinct if(fa.importance = 1, f.id)) files_w_important_attr , count(distinct if(fa.importance = 0, f.id)) files_w_unimportant_attr files f left join object_attributes fa on f.id = fa.object_id group f.region
this yields following results:
region | file_count | files_w_important_attr | files_w_unimportant_attr ------------------------------------------------------------------------ central | 1 | 0 | 0 eastern | 3 | 2 | 2 western | 1 | 1 | 0
i'm having trouble figuring out how count of files without important attributes. note i'm not trying count of files have unimportant attributes 3rd column in above query yields. want following results:
region | file_count | files_w_important_attr | files_w_no_important_attr ------------------------------------------------------------------------ central | 1 | 0 | 1 eastern | 3 | 2 | 1 western | 1 | 1 | 0
how this?
sum( fa.importance null ) nulledfiles
Comments
Post a Comment