Unir 2 querys

29/09/2007 - 06:47 por Pablo_Hack | Informe spam
Amigos he buscado por todo internet, he agotado todas las instacias q
me llevan a lo mismo.
El problema es el Siguiente. lo hare en un ejemplo simple.
Query1:
select ID ,count(*) as total1 from tabla1 group by ID
ID total1
1 5
2 15
3 15
5 3

Query2:
select ID ,count(*) as total2 from tabla1 where sesion>150 group by
ID
ID total2
1 9
5 16


Quiero q salga asi:
ID total1 total2
1 5 9
2 15
3 15
5 3 16


La pregunta es la siguiente. como lo hago en realidad es unir las dos
consultas en una, se puede? porfavor confio en ustedes.

Preguntas similare

Leer las respuestas

#1 Carlos M. Calvelo
29/09/2007 - 11:36 | Informe spam
On 29 sep, 06:47, Pablo_Hack wrote:
Amigos he buscado por todo internet, he agotado todas las instacias q
me llevan a lo mismo.
El problema es el Siguiente. lo hare en un ejemplo simple.
Query1:
select ID ,count(*) as total1 from tabla1 group by ID
ID total1
1 5
2 15
3 15
5 3

Query2:
select ID ,count(*) as total2 from tabla1 where sesion>150 group by
ID
ID total2
1 9
5 16

Quiero q salga asi:
ID total1 total2
1 5 9
2 15
3 15
5 3 16

La pregunta es la siguiente. como lo hago en realidad es unir las dos
consultas en una, se puede? porfavor confio en ustedes.



La respuesta ya está casi escrita :)
Los resultados de Query1 y Query2 "son" tablas;
haces un left join de query1 con query2 y ya está:

select t1.ID, t1.total1, t2.total2
(select ID ,count(*) as total1 from tabla1 group by ID) t1 --Query1
left join
(select ID ,count(*) as total2 from tabla1 where sesion>150 group by
ID) t2 -- query2
on t1.ID=t2.ID

Pero bueno... seguro que estás buscando esto:
select ID, count(*) as total1,
sum(case when sesion>150 then 1 else 0 end) as total2
from tabla1
group by ID

Saludos,
Carlos
Respuesta Responder a este mensaje
#2 Carlos M. Calvelo
29/09/2007 - 11:44 | Informe spam
On 29 sep, 11:36, "Carlos M. Calvelo" wrote:

corrección:

select t1.ID, t1.total1, t2.total2



FROM

(select ID ,count(*) as total1 from tabla1 group by ID) t1 --Query1
left join
(select ID ,count(*) as total2 from tabla1 where sesion>150 group by
ID) t2 -- query2
on t1.ID=t2.ID




saludos
Respuesta Responder a este mensaje
#3 Alejandro Mesa
29/09/2007 - 18:36 | Informe spam
Trata:

select
[ID],
count(*) as total1,
sum(case when sesion > 150 then 1 else 0 end) as total2
from
tabla1
group by
[ID]
go


AMB

"Pablo_Hack" wrote:

Amigos he buscado por todo internet, he agotado todas las instacias q
me llevan a lo mismo.
El problema es el Siguiente. lo hare en un ejemplo simple.
Query1:
select ID ,count(*) as total1 from tabla1 group by ID
ID total1
1 5
2 15
3 15
5 3

Query2:
select ID ,count(*) as total2 from tabla1 where sesion>150 group by
ID
ID total2
1 9
5 16


Quiero q salga asi:
ID total1 total2
1 5 9
2 15
3 15
5 3 16


La pregunta es la siguiente. como lo hago en realidad es unir las dos
consultas en una, se puede? porfavor confio en ustedes.


Respuesta Responder a este mensaje
#4 mariangeles.garciamartinez
29/09/2007 - 22:48 | Informe spam
Otra posibilidad "creativa"

select ID, sum(total1) as total1, sum(total2) as total2

from (

select ID, total1, '' as total2 from t1
union
select ID, '' as total1, total2 from t2) t1y2

group by ID
Respuesta Responder a este mensaje
#5 josemanuel.gor
29/09/2007 - 22:56 | Informe spam
Otra posibilidad "creativa"


select ID, sum(total1) as total1, sum(total2) as total2

from (

select ID, total1, '' as total2 from t1
union
select ID, '' as total1, total2 from t2) t1_2

group by ID
email Siga el debate Respuesta Responder a este mensaje
Ads by Google
Help Hacer una preguntaRespuesta Tengo una respuesta
Search Busqueda sugerida