Mysql - No Addition in my Calc Query -


i make calculation of notes , realise mysql query. here whole query:

select distinct round(avg(avg = 2) * count(avg)) new,                 sma_famille.famille   (select distinct round(sum(note) / count(note)) avg,                         sma_famille.famille,                         sma_agents.nom           sma_notes_conso                inner join sma_famille                        on sma_famille.id_service                inner join sma_agents                        on sma_notes_conso.id_agent = sma_agents.id_agent                inner join sma_service_activite                        on sma_service_activite.id_activite =                           sma_notes_conso.id_activite                inner join sma_service                        on sma_service.id_service          sma_service.id_entites in( 20 )                , sma_famille.id_service in( 988, 989, 990 )                , sma_service_activite.id_famille = sma_famille.id_famille                , sma_service_activite.id_service = sma_famille.id_service                , sma_service_activite.id_service = sma_service.id_service                , date_conso = '2013-04-03'         group  sma_famille.famille,                   sma_agents.nom) fn        inner join sma_famille                on sma_famille.id_service        inner join sma_service                on sma_service.id_service  sma_service.id_entites in( 20 )        , sma_famille.id_service in( 988, 989, 990 )        , sma_service.id_service = sma_famille.id_service        , fn.famille = sma_famille.famille group  fn.famille   

the query works correctly if have 1 id_service like: here first one:

    , sma_famille.id_service in(988) 

and these resultset:

    |9  |math|     |13 |english|     |2  |bio| 

here second one

    , sma_famille.id_service in(989)      |5  |math|     |8  |english|     |0  |bio| 

if take both, multiplied 2

    , sma_famille.id_service in(988,989) 

and these resultset:

    |28 |math|     |42 |english|     |4  |bio| 

but don't want multiplication, want addition of both 'id_service' like:

    |14  |math|     |21  |english|     |2   |bio| 

and if have 3 id's, multiplied 3!!!

everytime if add 'id_service' multiplied number of id_service

i have 3 times same query every note. notes 1, 2, 3. these example note 2

anybody can see problem?

thx in advance

i think found problem:

you did not set inner join correctly. query did cartesian join , multiplicated values. follwing should correct:

select distinct round( avg( avg =1 ) * count( avg ) ) new, sma_famille.famille                             (...                             ) fn                             inner join sma_famille on sma_famille.id_service = fn.id_service -- added join criteria                             inner join sma_service on sma_service.id_service = fn.id_service -- added join criteria                             sma_service.id_entites in(20)                             , sma_famille.id_service in(988,989,990)                              , sma_service.id_service = sma_famille.id_service                             , fn.famille = sma_famille.famille                             group fn.famille; 

Comments

Popular posts from this blog

node.js - Bad Request - node js ajax post -

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -