Count equal records with SELECT DISTINCT and COUNT

2

How do I count the same results and add the amount up front?

For example:

Currently the records in my table look like this:

Id  | Nome 
------------
0   | Teste
1   | Teste
2   | Teste 

I want to give DISTINCT to them and leave them like this:

Id  | Nome 
------------
0   | Teste(3)

SQLFIDDLE for testing

I am using MySql !

    
asked by anonymous 30.09.2014 / 22:28

4 answers

8

You can try to use the following sql expression:

select  distinct id, CONCAT(nome, "(",count(id),")") as Nome from teste group by nome;
    
30.09.2014 / 22:44
2

I looked for the same help and this was a solution that worked.

SELECT DISTINCT campo1
               ,campo2
               ,campo3
               ,campo4
               ,campo5
               ,campo6
               ,campo7
               ,COUNT(*) AS quantidade
  FROM tabela_a
 GROUP BY campo1
         ,campo2
         ,campo3
         ,campo4
         ,campo5
         ,campo6
         ,campo7
 ORDER BY quantidade DESC

Credits: Luan Moreno [SQL Soul] || SQL Server Developer || MCTS SQL Server Admin and Dev @luansql

    
01.02.2016 / 20:37
1

See if this helps you:

select COUNT(distinct Nome) from @TableTeste
    
30.09.2014 / 22:43
1

In SQLFIDDLE Test:  

create table algo(
  id int not null auto_increment,
  nome varchar(10) not null,
  primary key(id));


  insert into algo (id, nome) values (0,'Teste');
  insert into algo (nome) values (1,'Teste');
  insert into algo (nome) values (2,'Teste');

  select distinct concat(nome,'(',(select count(id) from algo),')') as nome from algo;
  select distinct concat(nome,'(',count(id),')') as nome from algo;
  select distinct sum(id-id) as id, concat(nome,'(',count(id),')') as nome from algo group by nome;

    
09.06.2017 / 07:32