How to use group by to group similar results for the same table record?

Asked

Viewed 37 times

0

I have a database with the tables: contract, broker, buyer, seller, product and harvest.

In this relationship, a contract will have only 1 relationship with the tables product and harvest, 1 or more of a relationship with the tables contrato_buyer and contrato_seller(ie a contract may have 1 or more buyers, and 1 or more seller)and no or only a relationship with the table contrato_broker(which makes a contract may or may not have a broker).

I am running the following query to return values to fill a table in java desktop software.

select codigo,
case
when comprador.tipo_cliente = '0' then comprador.nome_empresarial 
when comprador.tipo_cliente = '1' then comprador.nome_fantasia
end as compradores,
case
when vendedor.tipo_cliente = '0' then vendedor.nome_empresarial 
when vendedor.tipo_cliente = '1' then vendedor.nome_fantasia
end as vendedores,
case
when corretor.tipo_cliente = '0' then corretor.nome_empresarial 
when corretor.tipo_cliente = '1' then corretor.nome_fantasia
end as corretores
from contrato ct
LEFT JOIN contrato_comprador on contrato_comprador.id_contrato = ct.id
LEFT JOIN cliente comprador on comprador.id_cliente = contrato_comprador.id_cliente

LEFT JOIN contrato_vendedor on contrato_vendedor.id_contrato = ct.id
LEFT JOIN cliente vendedor on vendedor.id_cliente = contrato_vendedor.id_cliente

LEFT JOIN contrato_corretor on contrato_corretor.id_contrato = ct.id
LEFT JOIN cliente corretor on corretor.id_cliente = contrato_corretor.id_cliente

;

In the image below, we see that the contract 3512.5.7.4595 has 1 buyer, 2 sellers and 1 broker, from here how can I proceed to group all sellers only in a single row in the sellers column?

inserir a descrição da imagem aqui

 select codigo, quantidade, medida, pd.nome_produto,sf.descricao_safra, 
 sf.ano_plantio,
 sf.ano_colheita, valor_produto, valor_a_pagar, 

 GROUP_CONCAT(distinct
 case
 when comprador.tipo_cliente = '0' then comprador.nome_empresarial 
 when comprador.tipo_cliente = '1' then comprador.nome_fantasia
 end
 separator ',') as compradores,
 GROUP_CONCAT(distinct
 case
 when vendedor.tipo_cliente = '0' then vendedor.nome_empresarial 
 when vendedor.tipo_cliente = '1' then vendedor.nome_fantasia
 end
 separator ',') as vendedores,

 GROUP_CONCAT(distinct
 case
 when corretor.tipo_cliente = '0' then corretor.nome_empresarial 
 when corretor.tipo_cliente = '1' then corretor.nome_fantasia
 end
 separator ',') as corretores,
 ct.data_contrato, ct.status_contrato


from contrato ct
LEFT JOIN  safra sf on sf.id_safra = ct.id_safra
LEFT JOIN produto pd on pd.id_produto = sf.id_produto
LEFT JOIN contrato_comprador on contrato_comprador.id_contrato = ct.id
LEFT JOIN cliente comprador on comprador.id_cliente = 
contrato_comprador.id_cliente

LEFT JOIN contrato_vendedor on contrato_vendedor.id_contrato = ct.id
LEFT JOIN cliente vendedor on vendedor.id_cliente =         
contrato_vendedor.id_cliente

LEFT JOIN contrato_corretor on contrato_corretor.id_contrato = ct.id
LEFT JOIN cliente corretor on corretor.id_cliente =         
contrato_corretor.id_cliente

;

  • Assess whether the use of the aggregation function GROUP_CONCAT(DISTINCT ...), together with the clause GROUP BY, meets your needs.

  • Thank you for contributing. I am trying to use group_concat. In this query I used the case feature so that when according to the type of client it takes one of the Camps(name_fantasia for pj and business name_for pf) and use an alias to identify whether it is a seller, buyer or broker. It turns out that group_concat does not recognize the alias as a valid column, would you tell me why? Another thing, I could not then, make this select a sub query of another select, and in the 'select parent' use the group_concat resource?

  • I edited the post and put a query, it even works correctly, but I do not know why it only returns the first contract registered in the database, where I am missing?

  • Try adding Group By at the end

No answers

Browser other questions tagged

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