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'
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'
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;
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
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
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'