How to sort by the largest number of groupings, excluding who has no more than one grouping?

0

I need to sort my records by the largest number of GROUPS , that is, I have the SQL below and I need the TYPE column to be grouped >, be sorted from the largest grouping to the smallest, excluding display of TYPE that does not have GROUP .

(SELECT tipo, count(id) as quantidade
FROM imoveis
WHERE cod = '$cliente'
AND negociacao <> '0'
GROUP BY tipo)
ORDER BY quantidade DESC

I look forward to helping you.

    
asked by anonymous 17.03.2016 / 12:58

2 answers

2

I think what you're looking for is something like this:

SELECT tipo,
       count(id) AS quantidade
FROM imoveis
WHERE cod = '$cliente'
      AND negociacao <> '0'
GROUP BY tipo
HAVING quantidade > 1
ORDER BY quantidade DESC

Since you are grouping by type, you simply count how many ids there are in that type group and sort by it.

    
17.03.2016 / 13:06
0

Add the ordering in the SELECT among the partens:

select
id,
tipo
from
imoveis
where
cod = '$cliente'
AND negociacao <> '0'
GROUP BY tipo ORDER BY tipo DESC
    
17.03.2016 / 13:09