Gustavo Garcia
Gustavo Garcia

Reputation: 3

How can I distinctively count values I recieve from a union query?

I have this query:

SELECT Pedido1 from mydb.atendimentos 
UNION ALL
SELECT Pedido2 from mydb.atendimentos
order by Pedido1 ASC

That gets me this result: What I get when executing the query Now what I it to deliver is:

Teste -> 3
Teste2 -> 1

Is there any way of doing this with a union?

Upvotes: 0

Views: 30

Answers (1)

Juan Carlos Oropeza
Juan Carlos Oropeza

Reputation: 48187

This is called derived table.

http://www.programmerinterview.com/index.php/database-sql/derived-table-vs-subquery/

SELECT Pedido1, COUNT(*) AS PedidoCount
FROM 
(
    SELECT Pedido1 FROM mydb.atendimentos 
    UNION ALL
    SELECT Pedido2 FROM mydb.atendimentos
) T
GROUP BY Pedido1
ORDER BY Pedido1

Upvotes: 2

Related Questions