Foros del Web » Programación para mayores de 30 ;) » Bases de Datos General » SQL Server »

Unir Consultas SQl HORizontal

Estas en el tema de Unir Consultas SQl HORizontal en el foro de SQL Server en Foros del Web. HOla que tal, =) queria saber si hay alguna manera de unir 3 consultas de manera horizontal pongo las 3 consultas y sus resultados ------- ...
  #1 (permalink)  
Antiguo 23/03/2012, 10:37
 
Fecha de Ingreso: marzo-2012
Mensajes: 2
Antigüedad: 12 años, 1 mes
Puntos: 0
Exclamación Unir Consultas SQl HORizontal

HOla que tal, =)
queria saber si hay alguna manera de unir 3 consultas de manera horizontal
pongo las 3 consultas y sus resultados


------- clientes potenciales -------------------------

select AUXILIAR4,count(g.IDCLIENTE) as total
from teleoperacion t
inner join estadogestion e on t.idgestion=e.idgestion
inner join gestion g on t.idcliente=g.idcliente
where fecha_gestion<'23/03/2012' and e.tipo_rpta='POTENCIALES' and
num_gestion=(select max(num_gestion) from teleoperacion
where idcartera=t.idcartera and idcliente=t.idcliente
and fecha_gestion<'23/03/2012'
group by idcartera,idcliente)
group by AUXILIAR4

--------- clientes negativos -------------------------

select AUXILIAR4,count(g.IDCLIENTE) as total
from teleoperacion t
inner join estadogestion e on t.idgestion=e.idgestion
inner join gestion g on t.idcliente=g.idcliente
where fecha_gestion<'23/03/2012' and e.tipo_rpta='Contactos Negativos' and
num_gestion=(select max(num_gestion) from teleoperacion
where idcartera=t.idcartera and idcliente=t.idcliente
and fecha_gestion<'23/03/2012'
group by idcartera,idcliente)
group by AUXILIAR4

--------- marcaciones -------------------------------

select auxiliar4,count(t.idcliente) as total
from teleoperacion t
inner join gestion g on g.idcliente=t.idcliente
where fecha_gestion<'23/03/2012'
group by auxiliar4


TIPO TOTAL
10 1110
20 154
30 27
40 14
50 27

TIPO TOTAL
10 4654
20 920
30 374
40 234
50 169

TIPO TOTAL
10 39197
20 11959
30 5957
40 3885
50 2951

como veran los campos que uso son de las mismas tablas de las demas consultas
y lo que quiero al final es este resultado



TIPO TOTAL1 TOTAL2 TOTAL3
10 1110 4654 39197
20 154 920 11959
30 27 374 5957
40 14 124 3995
50 27 169 251


espero alguien me pueda ayudar

gracias
  #2 (permalink)  
Antiguo 23/03/2012, 11:06
Avatar de manuel2011  
Fecha de Ingreso: marzo-2012
Ubicación: Cuautla, Morelos
Mensajes: 31
Antigüedad: 12 años, 1 mes
Puntos: 1
Respuesta: Unir Consultas SQl HORizontal

Que tal takeo_mahou, la verdad no entiendo cual es el fin de lo que quieres hacer, en verdad es necesario hacerlo? Ademas de que los inner join no son iguales..
  #3 (permalink)  
Antiguo 23/03/2012, 11:18
 
Fecha de Ingreso: marzo-2012
Mensajes: 2
Antigüedad: 12 años, 1 mes
Puntos: 0
Respuesta: Unir Consultas SQl HORizontal

los inner join no son iguales en la ultima consulta pero. si te fijas bien, los campos que relacionan son los mismo solo que cambia el tiporespuesta y en el ultimo no esta eso, haciendo un count general

lo que busco es simplificar las 3 consultas en 1 sola
para agilizar los informes que aca hacemos..

un solo campo comparten que es TIPO las 3 consultas y los otros TOTAL salgan en forma horizontal

=)
  #4 (permalink)  
Antiguo 23/03/2012, 12:28
Colaborador
 
Fecha de Ingreso: enero-2007
Ubicación: México
Mensajes: 2.097
Antigüedad: 17 años, 3 meses
Puntos: 447
Respuesta: Unir Consultas SQl HORizontal

Hola takeo_mahou:

Para el caso de las dos primeras consultas (donde tanto los JOIN's como las condiciones son las mismas) puedes utilizar un SUM condicional en lugar de un COUNT, sería más o menos así:

Código:
select AUXILIAR4,
SUM(CASE WHEN e.tipo_rpta='POTENCIALES' THEN 1 ELSE 0 END) as total_potenciales,
SUM(CASE WHEN e.tipo_rpta='Contactos Negativos' THEN 1 ELSE 0 END) as total_negativos
from teleoperacion t
inner join estadogestion e on t.idgestion=e.idgestion
inner join gestion g on t.idcliente=g.idcliente
where fecha_gestion<'23/03/2012' and 
num_gestion=(select max(num_gestion) from teleoperacion
where idcartera=t.idcartera and idcliente=t.idcliente 
and fecha_gestion<'23/03/2012'
group by idcartera,idcliente)
group by AUXILIAR4
Para el último caso podrías tratar emplear una suma semenjante, pero tendrías que dejar en la consulta generar las condiciones que se aplican a todos los casos y en el CASE-WHEN las específicas para cada caso o en su defecto hacer otro INNER JOIN sobre to consulta, es decir, algo como esto:

Código:
select AUXILIAR4,
SUM(CASE WHEN e.tipo_rpta='POTENCIALES' THEN 1 ELSE 0 END) as total_potenciales,
SUM(CASE WHEN e.tipo_rpta='Contactos Negativos' THEN 1 ELSE 0 END) as total_negativos
from teleoperacion t
inner join estadogestion e on t.idgestion=e.idgestion
inner join gestion g on t.idcliente=g.idcliente
LEFT JOIN (AQUÍ PONES LA TERCER CONSULTA) TAux 
on Taux.AUXILIAR4 = T.AUXILIAR4
where fecha_gestion<'23/03/2012' and 
num_gestion=(select max(num_gestion) from teleoperacion
where idcartera=t.idcartera and idcliente=t.idcliente 
and fecha_gestion<'23/03/2012'
group by idcartera,idcliente)
group by AUXILIAR4
Ojo, en tu post no mencionas a qué tabla pertenece el campo AUXILIAR4, es por eso que lo marco en rojo al momento de hacer el join, estoy suponiendo que está en la tabla (teleoperacion t)

Haz la prueba y nos comentas.

Etiquetas: fecha, horizontal, select, sql, tabla, campos
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 20:27.