Group table data with two columns

2

Well, I'm in an incognita I'd like to solve directly in the SQL query, see this supposed example:

TIPO | DE | IDENTIFICADOR
-------------------------
001  | 23 | 9
001  | 44 | 9
001  | 23 | 8
002  | 11 | 11

I want to group the data in order to facilitate interpretation, in which case it should group data of

asked by anonymous 15.11.2016 / 22:00

2 answers

3

I made a part of your query, see it working here: link

CREATE TABLE 'teste' (
  'tipo' varchar(10),
  'de'  varchar(10),
  'identificador' varchar (10)
);

INSERT INTO 'teste' ('tipo','de', 'identificador') VALUES
('001','23', '9'),
('001','44', '9'),
('001','23', '8'),
('002','11', '11');

SELECT identificador, GROUP_CONCAT(de ORDER BY de ASC SEPARATOR ',') AS campo
FROM teste
GROUP BY identificador

The part of counting how many of each I will leave you to do ;-)

    
16.11.2016 / 00:31
3

If you want to get as separate data:

SELECT COUNT(tipo) as quantidade,
       identificador,
       GROUP_CONCAT(de ORDER BY de SEPARATOR ', ') AS dados
  FROM tabela
 GROUP BY tipo,
          identificador

Or if you want to get already with the text that was proposed:

SELECT CONCAT(COUNT(tipo), ' dados de ', GROUP_CONCAT(de ORDER BY de SEPARATOR ', '), ' sobre o identificador ', identificador) AS texto
  FROM tabela
 GROUP BY tipo,
          identificador

Where:

  

COUNT counts the occurrences of a row based on the GROUP BY grouping function

     

CONCAT concatenates the listed fields (include text between ' )

     

GROUP_CONCAT concatenates list columns based on grouping function

    
16.11.2016 / 01:48