Juntar as select no Mysql
Alguém poderia me ajudar estou tentando unir a mesma select só com a "dat" diferente para fazer um relatório
RESULTADO DA SELECT
Alessandra Martins 12345678900 14 -
Alessandra Martins 12345678900 - 14
Alice Castilho 12345678900 - 1052
Alice Castilho 12345678900 904 -
Andre Felipe 12345678900 10 -
Andre Felipe 12345678900 - 10
Antonio Neto 12345678900 5 -
Antonio Neto 12345678900 - 5
RESULTADO DA SELECT QUE EU GOSTARIA
Alessandra Martins 12345678900 14 14
Alice Castilho 12345678900 904 1052
Andre Felipe 12345678900 10 10
Antonio Neto 12345678900 5 5
DESDE JÁ AGRADEÇO!!
select
l.nome,
m.cpfcnpj,
sum(if(descricao='apply',ncotas,0)) - sum(if(descricao='rescue',ncotas,0)) AS anterior,
'-' as atual
from
movimentacao m,
login l
where
empresa ='sec'
and
m.cpfcnpj=l.cpfcnpj
and
length(m.cpfcnpj)=11
and
dat < '2018-07-31'
group by cpfcnpj
union
select
l.nome,
m.cpfcnpj,
'-' as anterior,
sum(if(descricao='apply',ncotas,0)) - sum(if(descricao='rescue',ncotas,0)) AS atual
from
movimentacao m,
login l
where
empresa ='sec'
and
m.cpfcnpj=l.cpfcnpj
and
length(m.cpfcnpj)=11
and
dat < '2018-08-31'
group by cpfcnpj
order by nome;
RESULTADO DA SELECT
Alessandra Martins 12345678900 14 -
Alessandra Martins 12345678900 - 14
Alice Castilho 12345678900 - 1052
Alice Castilho 12345678900 904 -
Andre Felipe 12345678900 10 -
Andre Felipe 12345678900 - 10
Antonio Neto 12345678900 5 -
Antonio Neto 12345678900 - 5
RESULTADO DA SELECT QUE EU GOSTARIA
Alessandra Martins 12345678900 14 14
Alice Castilho 12345678900 904 1052
Andre Felipe 12345678900 10 10
Antonio Neto 12345678900 5 5
DESDE JÁ AGRADEÇO!!
Jailson Santos
Curtidas 0
Melhor post
Luiz Santos
12/02/2019
Faz um SELECT por fora com GROUP BY.
Usa essa query imensa sua dentro do FROM.
Mas tira esse tracinho, se não vai dar ruim. Tenta usar 0 no lugar.
Consegui ser claro?
Grande abraço
Usa essa query imensa sua dentro do FROM.
Mas tira esse tracinho, se não vai dar ruim. Tenta usar 0 no lugar.
Consegui ser claro?
Grande abraço
GOSTEI 1
Mais Respostas
Jailson Santos
10/02/2019
resultado da select no link abaixo
https://ibb.co/f2Xsr6n
https://ibb.co/f2Xsr6n
GOSTEI 0
Luiz Santos
10/02/2019
Para facilitar sua vida.
SELECT Q.nome
, Q.cpfcnpj
, SUM(Q.anterior) AS anterior
, SUM(Q.atual) AS atual
FROM (
select
l.nome,
m.cpfcnpj,
sum(if(descricao='apply',ncotas,0)) - sum(if(descricao='rescue',ncotas,0)) AS anterior,
0 as atual
from
movimentacao m,
login l
where
empresa ='sec'
and
m.cpfcnpj=l.cpfcnpj
and
length(m.cpfcnpj)=11
and
dat < '2018-07-31'
group by cpfcnpj
union
select
l.nome,
m.cpfcnpj,
0 as anterior,
sum(if(descricao='apply',ncotas,0)) - sum(if(descricao='rescue',ncotas,0)) AS atual
from
movimentacao m,
login l
where
empresa ='sec'
and
m.cpfcnpj=l.cpfcnpj
and
length(m.cpfcnpj)=11
and
dat < '2018-08-31'
group by cpfcnpj ) AS Q
GROUP BY
Q.nome
, Q.cpfcnpj
order by
Q.nome;
GOSTEI 1
Jailson Santos
10/02/2019
Para facilitar sua vida.
SELECT Q.nome
, Q.cpfcnpj
, SUM(Q.anterior) AS anterior
, SUM(Q.atual) AS atual
FROM (
select
l.nome,
m.cpfcnpj,
sum(if(descricao='apply',ncotas,0)) - sum(if(descricao='rescue',ncotas,0)) AS anterior,
0 as atual
from
movimentacao m,
login l
where
empresa ='sec'
and
m.cpfcnpj=l.cpfcnpj
and
length(m.cpfcnpj)=11
and
dat < '2018-07-31'
group by cpfcnpj
union
select
l.nome,
m.cpfcnpj,
0 as anterior,
sum(if(descricao='apply',ncotas,0)) - sum(if(descricao='rescue',ncotas,0)) AS atual
from
movimentacao m,
login l
where
empresa ='sec'
and
m.cpfcnpj=l.cpfcnpj
and
length(m.cpfcnpj)=11
and
dat < '2018-08-31'
group by cpfcnpj ) AS Q
GROUP BY
Q.nome
, Q.cpfcnpj
order by
Q.nome;
Obrigado Luiz funcionou perfeitamente. Fiz de outra forma usando left join, mas obrigado mesmo assim
GOSTEI 0