Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

185
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda