Ayuda con un query

Buen dia,

Antes que nada un saludo a toda la comunidad y aprovechando a ver si me pueden ayudar a armar un query. Tengo el siguiente query el cual me da cada numero que tiene mas de un mensaje 2001 en la table b;

select distinct a.number_from, count(*) 
from xnp_port_msg_range a join xnp_port_msg b on b.req_seq=a.REQ_SEQ 
where b.msg_type_id='2001' and b.create_time between to_date('20131008','YYYYMMDD') and to_date('20131009','YYYYMMDD') 
group by a.NUMBER_FROM having count (*) > 1

El total de lineas que me regresa es de 7800.

Ahora lo que quiero es que envez que me de cada linea, me las agrupe por mes, es decir que me de el count por mes del total de casos en los que el number_from tiene mas de 1 mensaje 2001 Intente de esta forma pero no me da el mismo resultado del query anterior;

select to_char(b.create_time,'YYYY/MM') , count(*) 
from xnp_port_msg_range a join xnp_port_msg b on b.req_seq=a.REQ_SEQ 
where b.msg_type_id='2001' and b.create_time between to_date('20131008','YYYYMMDD') and to_date('20131009','YYYYMMDD') 
group by to_char(b.create_time,'YYYY/MM') 
having count ( a.NUMBER_FROM) > 1

Podrian por favor ayudarme con esta situacion? estoy usando oracle 11 g. Gracias de antemano.

Bueno ya tengo el query funcionando

select create_time, count(*) from(

select to_char(b.create_time,'YYYY/MM') create_time, count(1) from xnp_port_msg_range a join xnp_port_msg b on b.req_seq=a.REQ_SEQ where b.msg_type_id='2001'

and b.create_time between to_date('20131008','YYYYMMDD') and to_date('20131009','YYYYMMDD')

group by to_char(b.create_time,'YYYY/MM'), a.number_from

having count (a.NUMBER_FROM) > 1)

group by create_time

ahora necesito convinarlo con estro otro query;

select to_char(create_time,'YYYY/MM'), count(*) from xnp_port_msg where msg_type_id='1001' and service_type='MOBIL_PRE'; probe agregarle un "and exists y el query anterior pero no funciona"

 

alguna idea?

Creo que como dice Carlos, debes hacer un join: inner join: si quieres los comunes left join: si quieres que prevalezcan los de la tabla de la primera query (right join al contrario) full join: que te cruce todo. Ya nos dices...

gracias por sus comenarios,

lo intente de la siguiente forma y no me arroja error pero el resultado esta en blanco;

select create_time, count(*) from (

select to_char(b.create_time,'YYYY/MM') create_time, count(1) from xnp_port_msg_range a join xnp_port_msg b on b.req_seq=a.REQ_SEQ

full join xnp_port_msg c on b.port_id=c.port_id

where b.msg_type_id='2001' and

c.msg_type_id='1001' and c.SERVICE_TYPE='MOBILE_PRE'

and b.create_time between to_date('20131009','YYYYMMDD') and to_date('20131010','YYYYMMDD')

group by to_char(b.create_time,'YYYY/MM'), a.number_from

having count (a.NUMBER_FROM) > 1)

group by create_time

order by create_time

 

Probe full, left, right, inner join y siempre el resultado fue "no rows selected"

En respuesta a por kobeson

Vamos a probar con un poco de indentación y colocando las condiciones de cada join dentro de cada una en lugar de situarlas todas juntas en el where:

select create_time, count(*) from (
  select to_char(b.create_time,'YYYY/MM') create_time, count(1) 
  from xnp_port_msg_range a 
       join xnp_port_msg b on b.req_seq=a.REQ_SEQ and b.msg_type_id='2001' and
                              b.create_time between to_date('20131009','YYYYMMDD') and to_date('20131010','YYYYMMDD')
       join xnp_port_msg c on b.port_id=c.port_id and c.msg_type_id='1001' and c.SERVICE_TYPE='MOBILE_PRE'     
  group by to_char(b.create_time,'YYYY/MM'), a.number_from
  having count (a.NUMBER_FROM) > 1)
group by create_time
order by create_time

Yo poniéndolo así lo que no veo claro es la condición de la ultima join 'b.port_id=c.port_id', pero tampoco sé bien lo que quieres hacer.

¿No sería mejor enlazar con la tabla común xnp_port_msg_range por el id REQ_SEQ, como en la primera consulta?