Join 2 SQL with different results

Asked

Viewed 412 times

-1

They work perfectly, but now I need to join them, but the results will be different or I can not join the result, but rather join the SQL and get the result of the two below, but I do not know how to do.

Look what I got:

SELECT COUNT(a.arquivada=1)AS orden, c.nomeCliente  
    FROM tbl_os o  
    INNER JOIN tbl_clientes c 
    ON(o.tecnico = c.id)  
    WHERE o.arquivada=1 
    AND o.tecnico=1 
    AND DAY(o.dataSaida)=x 
    AND MONTH(o.dataSaida) = x

SELECT SUM(a.idTipoAtendimento=1) AS sistema, SUM(a.idTipoAtendimento=2)AS servico, c.nomeCliente 
    FROM tbl_Atendimento a  
    INNER JOIN tbl_clientes c 
    ON(a.tecnico = c.id)  
    WHERE a.arquivada=1 
    AND a.tecnico=1 
    AND DAY(a.dataMarcada)=x 
    AND MONTH(a.dataMarcada) = x
  • 1

    Are you sure these selects work without the GROUP BY clause?

  • What do you mean join without joining? And I can’t see "they’re in bold"

  • http://forum.imasters.com.br/topic/555805-sql-com-2-where-ou-algo-assim/#entry2216981

  • Oops, I’m new here, I had the bold, but it didn’t come out. , so these SELECT works separately, but I need the COUNT and SUM value that are in them. need to join the columns in the same SQL, so far I haven’t found anything.

1 answer

1

Thus

SELECT SUM(Temp.ordem) as ordem, sum(Temp.sistema) as sistema from
(SELECT COUNT(a.arquivada=1)AS orden, 0 as sistema
FROM tbl_os o
INNER JOIN tbl_clientes c ON(o.tecnico = c.id)
WHERE o.arquivada=1 AND o.tecnico=1 AND DAY(o.dataSaida)=x AND MONTH(o.dataSaida) = x
UNION ALL
SELECT 0 as ordem, SUM(a.idTipoAtendimento=1) AS sistema, SUM(a.idTipoAtendimento=2)AS servico, c.nomeCliente FROM tbl_Atendimento a
INNER JOIN tbl_clientes c ON(a.tecnico = c.id)
WHERE a.arquivada=1 AND a.tecnico=1 AND DAY(a.dataMarcada)=x AND MONTH(a.dataMarcada) = x)
as Temp

what we do before uniting the two queries is to ensure that they have the same columns, so SELECT ... 0 the order...

Then I use the Sum(Temp.order) so that the two results are on the same line.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.