How to group by column only when it is not null (Oracle)?

Asked

Viewed 146 times

0

The query is summarized to facilitate:

SELECT
    nome,idade,profissao,cidade, bairro, estado, pais
FROM objeto
group by  nome,idade,profissao,cidade, bairro, estado, pais;

However 3 columns can come null (profissao, bairro, estado). When one of these columns comes null, the column null should not be used in the grouping of records, should group by which are not null.

That is to say, (profissao, bairro, estado) may be null or not, but need to be grouped by them when they come not null.

  • can’t use a where to filter?

  • Here’s the deal. There’s no crying. If you don’t want to group by null, then you can’t come null in these columns, simple as that. Otherwise it will group by null. Add in your WHERE the clauses AND Nomecoluna IS NOT NULL and be happy. If I could do any tricks, it would be at HAVING of GROUP BY, but I believe that I can not.

  • I get it. But if I put the " WHERE clauses AND IS NOT NULL Column", it will not bring the records that have some null column, and I need them. For example, one of the columns is of value, and I would need it grouped if the other columns that are not null are equal

  • Try something like nvl(column,'no information') in grouping.

  • By this your example will SELECT DISTINCT nome,idade,profissao,cidade, bairro, estado, pais FROM objeto would not answer? It may be that its simplification has hindered the understanding.

  • pq a group by se has no aggregation function ?

  • agree with @Rovannlinhalis if select does not use aggregation there is no need for group by... if using to eliminate duplicates use the suggestion from DISTINCT above which is the correct way to do this

Show 2 more comments
No answers

Browser other questions tagged

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