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

191
Visualizações
PostgreSQL querying through schemas

I want a query that lists all Customers who's status is "active". This query would return a list of customers who are marked as active. My problem is that I am lost on querying tables that reference other tables. Here is my schema.

 CREATE TABLE Customer (
  ID BIGSERIAL PRIMARY KEY NOT NULL,
  fNAME TEXT NOT NULL,
  lNAME TEXT NOT NULL,
  create_date DATE NOT NULL DEFAULT NOW() 
 );

 CREATE TABLE CustomerStatus (
  recordID BIGSERIAL NOT NULL,
  ID BIGSERIAL REFERENCES Customer NOT NULL,
  status TEXT NOT NULL,
  create_date DATE NOT NULL DEFAULT NOW()
 );

 INSERT INTO Customer (fNAME, lNAME) VALUES ('MARK', 'JOHNSON'), ('ERICK', 'DAWN'), ('MAY', 'ERICKSON'), ('JESS', 'MARTIN');

 INSERT INTO CustomerStatus (ID, status) VALUES (1, 'pending'), (1, 'active');     

 INSERT INTO CustomerStatus (ID, status) VALUES (2, 'pending'), (2, 'active'), (2, 'cancelled');     

 INSERT INTO CustomerStatus (ID, status) VALUES (3, 'pending'), (3, 'active');  

 INSERT INTO CustomerStatus (ID, status) VALUES (4, 'pending'); 
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

I took courage to assume that record_id is serial => the latest id would be the last, to produce this qry:

t=# with a as (
  select *, max(recordid) over (partition by cs.id)
  from Customer c
  join CustomerStatus cs on cs.id = c.id
)
select *
from a
where recordid=max and status = 'active';
 id | fname |  lname   | create_date | recordid | id | status | create_date | max
----+-------+----------+-------------+----------+----+--------+-------------+-----
  1 | MARK  | JOHNSON  | 2017-04-27  |        2 |  1 | active | 2017-04-27  |   2
  3 | MAY   | ERICKSON | 2017-04-27  |        7 |  3 | active | 2017-04-27  |   7
(2 rows)

Time: 0.450 ms
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