Mysql - How to add two fields with the same name, group them and count them?

3

Good morning, I hope you can help me with this query, I have this query:

SELECT at_ce.condicion, COUNT(*) AS cantidad
FROM at_cons_externa at_ce
GROUP BY at_ce.condicion

And as a result, it gives me this:

What I need is to put together ONE SINGLE query that adds the field "CONFIRMADO" + "OBSERVADO" and another one that adds me "I DO NOT PASS CONSULTATION" + "CONFIRMADO" + "OBSERVADO". I hope someone can help me, or guide. Maybe inside you can handle an "if" or related.

Thank you ...

    
asked by Jean Paul 16.06.2016 в 15:19
source

2 answers

2

Without considering optimizations and I think they are small tables ...:

SELECT COUNT(*) AS cantidad INTO #TablaTemporalConfirmadoObservado
FROM at_cons_externa at_ce
where at_ce.condicion IN ('CONFIRMADO','OBSERVADO')

SELECT COUNT(*) AS cantidad INTO #TablaTemporalConfirmadoObservadoNoPaso
FROM at_cons_externa at_ce
where at_ce.condicion IN ('CONFIRMADO','OBSERVADO','NO PASO CONSULTA')

select t1.cantidad AS [Total Confirmado y Observado], t2.cantidad AS [Total Confirmado, Observado y No paso consulta]
from #TablaTemporalConfirmadoObservado t1,  #TablaTemporalConfirmadoObservadoNoPaso t2

drop table #TablaTemporalConfirmadoObservado
drop table #TablaTemporalConfirmadoObservadoNoPaso

Tables that start with # are tables that are created at the moment (temporary) but it is important to make a drop table at the end to be able to reuse them.

I like to use temporary tables, but if you want to do it in a single query ...:

With UNION :

SELECT COUNT(*) AS [Total Confirmado y Observado]
FROM at_cons_externa at_ce
where at_ce.condicion IN ('CONFIRMADO','OBSERVADO')
union
SELECT COUNT(*) AS [Total Confirmado, Observado y No paso consulta] 
FROM at_cons_externa at_ce
where at_ce.condicion IN ('CONFIRMADO','OBSERVADO','NO PASO CONSULTA')
    
answered by 16.06.2016 / 15:26
source
0

hi I'm going to recommend something better under a program called SQLyog Enterprise connect your mysql to the program and generate a query builder, you just have to drag and drop you select the table and the values you want and the only generates the query copies and hit on your page and go. see an example in the photo

    
answered by 17.06.2016 в 02:37