0
I have an Employee table with the fields Name, Age, Gender and Company. And each employee may or may not be in more than one company.
With that I made the following select in this table.
select Nome, Empresa from funcionario group by Nome, Empresa
It’s just that obviously when I have the same employee for more than one company, the registration is duplicated, and I didn’t want that to happen, thus, I made a back-end approach to solve this problem by taking only one of the records.
var listaFuncionarios = funcionarios.GroupBy(e => e.Nome).Select(e => e.FirstOrDefault()).ToList();
Question: How to resolve this in the query itself without having to change the back-end the way I did ?
The idea is to use only the first company?
– Jéf Bueno
@LINQ Yes, can not bring more than one company to the employee, only one, regardless of which company is.
– Samuel Renan Gonçalves Vaz
Your SQL command does exactly what you said you got. If you want Name to appear only once you can use, for example:
select Nome, MAX(Empresa) from funcionario group by Nome
.– anonimo
Have you tried using LISTAGG ? can be a visualization solution https://docs.oracle.com/cd/E11882_01/server.112/e41084/functions089.htm#SQLRF30030
– Motta