2
I am trying to assemble a query (MYSQL) to return me all the clients that a particular seller can view. The rules to show the list of customers for this seller are: - Customers with contracts expired or without contracts. - Customers with bids expired or without bids. - Customers with existing contracts or offers still valid provided that they have been created by the consulting seller himself.
In short: I can only show customers available to this seller.
My scenario in the database is:
I arrived at the following query, but it is returning duplicate customers the problem happens by the fact that a customer may have been served by more than one seller, ie an expired offer from seller 1 and a valid contract by seller 2, in this case if seller 2 does the search, this customer appears twice in the result, because the Groupby clause has included both the registration linked to the seller of the table "Proposal" and the registration linked to the seller of the table "Contract".
View the query:
select DISTINCT `clientes`.`id`, `clientes`.`pessoa_tipo`, `clientes`.`razao_nome`, `clientes`.`nome_fantasia`, `clientes`.`email`, `clientes`.`logradouro`, `clientes`.`numero`, `clientes`.`complemento`, `clientes`.`bairro`, `clientes`.`cidade`, `clientes`.`uf`, `clientes`.`cep`, `clientes`.`telefone`, `clientes`.`celular`, `clientes`.`cnpj_cpf`, `clientes`.`inscricao_rg`, `clientes`.`contato_nome`, `clientes`.`contato_funcao`, `clientes`.`representante_nome`, `clientes`.`representante_rg`, `clientes`.`representante_cpf`, `clientes`.`atividade_rf`, `clientes`.`atividades_comerciais_id`, `contratos`.`vendedores_id` as `contrato_vendedor`, `propostas`.`vendedores_id` as `proposta_vendedor`
from `clientes` left join `contratos` on `contratos`.`clientes_id` = `clientes`.`id` left join `propostas` on `propostas`.`clientes_id` = `clientes`.`id`
group by `clientes`.`id`, `clientes`.`pessoa_tipo`, `clientes`.`razao_nome`, `clientes`.`nome_fantasia`, `clientes`.`email`, `clientes`.`logradouro`, `clientes`.`numero`, `clientes`.`complemento`, `clientes`.`bairro`, `clientes`.`cidade`, `clientes`.`uf`, `clientes`.`cep`, `clientes`.`telefone`, `clientes`.`celular`, `clientes`.`cnpj_cpf`, `clientes`.`inscricao_rg`, `clientes`.`contato_nome`, `clientes`.`contato_funcao`, `clientes`.`representante_nome`, `clientes`.`representante_rg`, `clientes`.`representante_cpf`, `clientes`.`atividade_rf`, `clientes`.`atividades_comerciais_id`, `contratos`.`vendedores_id`, `propostas`.`vendedores_id`
having (Max(contratos.vigencia_final) IS NULL AND Max(contratos.vigencia_final) IS NULL)
OR
(Max(contratos.vigencia_final) <= '2017-11-23' AND Max(propostas.validade) <= '2017-11-23')
OR
(Max(contratos.vigencia_final) <= '2017-11-23' AND Max(propostas.validade) IS NULL)
OR
(Max(propostas.validade) <= '2017-11-23' AND Max(contratos.vigencia_final) IS NULL)
OR
(contratos.vendedores_id = '3' OR propostas.vendedores_id = '3')
What would be your ideal scenario? You want no bids, only contracts, if there is a valid contract for the customer?
– nunks
I want to return customers without duplicating, in which I don’t need the seller ID in the results (which is outlined in blue, green and red)
– Daniel Lopes
So your problem will be solved if you remove
contrato_vendedor
andproposta_vendedor
of yourselect
and ofgroup by
, since you don’t need such data in the result. It duplicates because there is data that makes the record unique (sellers Ids).– nunks
but if I don’t include them in select I can’t use them in HAVING comparisons
– Daniel Lopes
truth, I have traveled! the @marconcilio-souza response makes sense in this case, but as there is comparison of equality in the case of sellers' ids, I think the best way out should be at the base of subselect: a
select distinct [campos do cliente] from ([toda essa sua query]) as alias;
– nunks