Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

185
Visualizações
Finding rows that have the same value with Postgres

Not sure if the title is descriptive enough (sorry about the bad english), but my problem is that I need to find the rows that have the same value in all rows.

Possibly easier to understand what I actually mean with an example: I have an table that's called Catalogue with the attributes motortype, motorpart and partnumber where motortype and motorpart are VARCHAR and partnumber are int.

Some partnumbers are used in all the motortypes, and it's those partnumbers that I am interested in finding.

I'm having a hard time seeing how to solve this, so any help would be greatly appreciated!

EDIT: Sorry if the question was lackful or bad. I have updated with the table schema, some sample data and my desired results under:

CREATE TABLE Catalogue (
motortype VARCHAR,
motorpart VARCHAR,
partnumber int,
PRIMARY KEY (partnumber)
);

Sample data:

INSERT INTO Catalogue VALUES ('M1', 'Brakes', 1);
INSERT INTO Catalogue VALUES ('M2', 'Brakes', 1);
INSERT INTO Catalogue VALUES ('M3', 'Brakes', 1);
INSERT INTO Catalogue VALUES ('M1', 'Gasket', 2);
INSERT INTO Catalogue VALUES ('M2', 'Gasket', 2);
INSERT INTO Catalogue VALUES ('M3', 'Pedal', 3);

Desired result:

| motorpart | partnumber |
|-----------|------------|
| Brakes    |          1 |

Hope this was better formulated!

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

If there are 3 different types you can use next query:

select   motorpart
from     Catalogue
group by motorpart
having   count(distinct motortype) >= 3;

If you don't know the number:

select   motorpart
from     Catalogue
group by motorpart
having   count(distinct motortype) = (select distinct motortype 
                                      from   Catalogue);

Rextester here

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda