Can I put a WHERE for each column?

-1

I want to put a condition for each column in my SELECT, is it possible?

SELECT count(id_casa) as casaAzul, count(id_casa) as casaAmarela FROM tb_casa

Where: Where column 1: where cor_casa = 'azul' Where column 2: where cor_casa = 'yellow'

    
asked by anonymous 28.09.2018 / 20:09

4 answers

2

One option to do this count is:

SELECT
    SUM(CASE cor_casa = 'azul' THEN 1 ELSE 0 END) AS casaAzul,
    SUM(CASE cor_casa = 'amarela' THEN 1 ELSE 0 END) AS casaAmarela
FROM tb_casa;
    
30.09.2018 / 19:48
2

You are counting the same column twice (home_id) and calling one from CasaAzul and another from CasaAmarela. What you should do is group the houses by color and give a count on it.

SELECT COR_CASA as COR, COUNT(*) as QUANTIDADE
  FROM TB_CASA
 WHERE COR_CASA IN ('azul', 'amarela')
 GROUP BY COR_CASA
    
28.09.2018 / 20:26
1

We have solved!

The following is the SQL code:

SELECT DATE(DATE) AS DATA, 
(SELECT COUNT(*) 
FROM TB_VBV T 
WHERE DATE(T.DATE) = DATE(T1.DATE) 
AND GROSS > 12600 ) as QUANTIDADE_PESADOS, 
(SELECT COUNT(*) 
FROM TB_VBV T 
WHERE DATE(T.DATE) = DATE(T1.DATE) 
AND GROSS < 12600 ) as QUANTIDADE_OUTROS 
FROM TB_VBV as T1
GROUP BY DATA 
ORDER BY DATA
    
28.09.2018 / 21:46
0

Yes, just use logical SQL operators, such as AND, for example:

SELECT nm_casa, count(id_casa) FROM tb_casa
WHERE nm_casa = 'minhacondição' AND id_casa = 'minhacondição'

More about logical operators in sql.

    
28.09.2018 / 20:12