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

151
Visualizações
Nested SQL Query where id from one table matches to another table where two specific entries exist

I have two tables: person_demographics, person_social_profile, that are linked by column person_id

p_d represents unique persons, p_s_p represents their social network accounts

p_d will only have one entry per person_id, but p_s_p will have many rows per same person_id

I need to get a count of how many people in the db live in germany, extracted from 'country' column in p_d where there also exists a social account in p_s_p from facebook, AND there also exists a social account for twitter.

I have so far

select person_id from person_demographics pd
where pd.country like '%Germany%' or pd.country = 'DE'

for selecting the set of person_id's for users living in Germany, and

select * from person_social_profiles psp where psp.person_id <is in previous results> and (psp.source = 'facebook' or psp.source = 'twitter')

I then have the idea of doing groupbykey on person_id and counting the groups with > 1 entries, to get the count of unique users that live in germany and have both facebook and twitter, but am having trouble chaining it all together in one query. Any suggestions would be much appreciated, thanks.

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

0

I would suggest two levels of aggregation:

select count(*)
from (select pd.person_id
      from person_demographics pd join
           person_social_profiles psp
           on psp.person_id = pd.pser_id
      where (pd.country like '%Germany%' or pd.country = 'DE') and   
            psp.source in ('facebook', 'twitter')
      group by pd.person_id
      having count(distinct psp.source) = 2
     ) pd;
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