Group Duplicate Records in a Single Line

4

I'm having a problem with a query, in which I need to transform rows into columns. In the case I thought about using the pivot function, but it did not work very well, since mysql does not give me such a function. But the tables are as follows:

Mygoalistodisplaytheresultsasfollows

Howevermyqueryisabitmessy,becauseitreturnsthesameproductsondifferentrowsandIcannotdisplayitthatway,eventryingtogroupthecolumns.

SELECTpq.id_produto,pd.descricao,CASEWHENpl.id_loja='1'THENpq.produto_quantidadeENDLOJA1,CASEWHENpl.id_loja='2'THENpq.produto_quantidadeENDLOJA2,CASEWHENpl.id_loja='3'THENpq.produto_quantidadeENDLOJA3,CASEWHENpl.id_loja='4'THENpq.produto_quantidadeENDLOJA4FROMpedidos_produtos_quantidadepqLEFTJOINpedidos_lojasplONpl.id=pq.id_pedido_lojaLEFTJOINlojaslONpl.id_loja=l.idLEFTJOINprodutospdONpq.id_produto=pd.id_produtoGROUPBYpd.descricao,pq.produto_quantidade

Thissqlgeneratesthisresult,whichIcannotclusterinthewayImentioned:

Would anyone help me with this grouping?

    
asked by anonymous 01.11.2016 / 00:56

1 answer

3

Using the following link I got the following result:

SELECT pq.id_produto,
       pd.descricao,
       sum(pq.produto_quantidade*(1-abs(sign(pl.id_loja-1)))) as loja1,
       sum(pq.produto_quantidade*(1-abs(sign(pl.id_loja-2)))) as loja2,
       sum(pq.produto_quantidade*(1-abs(sign(pl.id_loja-3)))) as loja3,
       sum(pq.produto_quantidade*(1-abs(sign(pl.id_loja-4)))) as loja4
FROM pedidos_produtos_quantidade pq
LEFT JOIN pedidos_lojas pl ON pl.id = pq.id_pedido_loja
LEFT JOIN lojas l ON pl.id_loja = l.id
LEFT JOIN produtos pd ON pq.id_produto = pd.id_produto
GROUP BY pq.id_produto,
         pd.descricao

EDIT 1

The creation of this bank follows the following script:

CREATE TABLE pedidos_lojas('id' integer, 'id_loja' integer);
CREATE TABLE pedidos_produtos_quantidade('id' integer, 'id_produto' integer, 'produto_quantidade' integer, 'id_pedido_loja' integer);
CREATE TABLE produtos('id_produto' integer, 'descricao' varchar(100));
CREATE TABLE lojas('id' integer, 'nome' varchar(100));

INSERT INTO pedidos_lojas('id', 'id_loja')
VALUES (1, 2),
       (11, 3),
       (12, 4);

INSERT INTO pedidos_produtos_quantidade('id', 'id_produto', 'produto_quantidade', 'id_pedido_loja')
VALUES (1, 1, 10, 1),
       (2, 2, 20, 1),
       (12, 1, 15, 11),
       (13, 38, 18, 1),
       (14, 44, 12 ,1),
       (15, 44, 22, 11),
       (16, 44, 10, 12),
       (17, 38, 9, 11),
       (18, 38, 29, 12),
       (19, 1, 15, 12),
       (20, 2, 18, 11);

INSERT INTO produtos('id_produto', 'descricao')
VALUES (1, 'Abacate'),
       (2, 'Abacaxi'),
       (38, 'Laranja'),
       (44, 'Manga');

INSERT INTO lojas('id', 'nome')
VALUES (1, 'Centro'),
       (2, 'Lagoa'),
       (3, 'Gloria'),
       (4, 'Serra');

EDIT 2

The following explanation is in the link quoted above:

  

"pivot table" or "crosstab report"   Function SQL feature: Do this without "if", "case", or "GROUP_CONCAT".

     

The secret and also the reason it works in almost all databases are the following functions:

     
  • sign (x) returns -1.0, +1 for values x < 0, x = 0, x > 0 respectively;
  •   
  • abs (sign (x)) returns 0 if x = 0 if not, 1 if x> 0 or x < 0;
  •   
  • 1-abs (sign (x)) complements the above, since it returns 1 only if x = 0.
  •   
    
01.11.2016 / 12:42