oracle - SQL Join 2 table with 2 count -


i'll try explain problem:

i've 2 similar query:

first one

select mod_desc, mod_cod, count(mod_desc) num modelli, pv_proposti, gestione_commesse   

second one

select mod_desc, mod_cod, count(mod_desc) num  distrib, modelli  

now, result its' similar since difference conditions:

 mod_desc   mod_cod  num   desc1      cod1     2    desc2      cod2     1    desc3      cod3     3   

and

 mod_desc  mod_cod  num   desc1      cod1     5    desc2      cod2     2    desc4      cod4     3   

but want "merge" 2 queries , have result like

 mod_desc  mod_cod  num1  num2   desc1      cod1     2      5    desc2      cod2     1      2    desc3      cod3     3      0    desc3      cod3     0      3   

i've tryed query:

select t2.mod_desc, t2.mod_cod, count(t2.mod_desc) num1, count(t1.mod_desc) num2 (          select mod_desc, mod_cod        modelli, pv_proposti, gestione_commesse                ) t1          right join        (         select mod_desc, mod_cod         distrib, modelli                ) t2 on t1.mod_cod = t2.mod_cod   group t2.mod_desc, t2.mod_cod order t2.mod_desc, t2.mod_cod 

but don't corret result.

some help?

(i'm on oracle db)

a few notes.

1) counting in sub-queries, not in outer query. joining results before counting change results of counts.

2) use full outer join in case there records present in 1 sub-query not other

3) join on both fields (mod_desc , mod_cod), not one

select   * (   select mod_desc, mod_cod, count(mod_desc) num   modelli, pv_proposti, gestione_commesse     )   mpg full outer join (   select mod_desc, mod_cod, count(mod_desc) num    distrib, modelli    )   dm     on  dm.mod_desc = mpg.mod_desc     , dm.mod_cod  = mpg.mod_cod 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -