How to order by the largest number of groupings, excluding those who have no more than one grouping?

Asked

Viewed 6,683 times

0

I need to sort my records by as many as GROUPINGS, that is, I have the SQL below and I need the column TYPE who is being grouped, is ordered from the largest cluster to the smallest, excluding the display of TYPE that does not have GROUPING.

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

I’m waiting for help.

2 answers

2


I believe 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

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

  • 1

    Is the use of the label not valid? Humm.. I am without a tool to test at the moment, but I believe that the use of Abels is valid yes.

  • is valid yes I believe this is the answer he wants. + 1

  • @According to what I put what is missing is not to display the TYPE that has no grouping, because my intention is to display only the types that have groupings and exclude those that do not have.

  • I had not seen the excluded those who do not have, I will change the answer

  • @Rubico did not work friend. TYPE that has no grouping continues to be displayed.

  • What would you say by type that has no grouping? The type that has 0 immobles? It would show to give a DESC in your immovable table and post the result?

  • @Rubico Example: As TYPE I have the values: 2 , 8, 6, 8, 2 - Only display the groupings 2 and 8. Excluding the display of 6. Got it? Since 6 is alone.

  • I understood that you have 3 values and want to display 2 of them. But I don’t understand why, what makes these 8 and 2 to be displayed and 6 not? You’re telling me there is no immovable with type 6?

Show 4 more comments

0

Add the sort on SELECT among the parêtensis:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.