CREATE TABLE Department(
DNumber integer NOT NULL PRIMARY KEY);
CREATE TABLE Patient(
PID integer NOT NULL PRIMARY KEY,
P_DNumber integer NOT NULL REFERENCES Department (DNumber)
);
CREATE TABLE Appointment(
AppNumber integer NOT NULL PRIMARY KEY,
AppDate date NOT NULL,
App_DNumber integer NOT NULL REFERENCES Department (DNumber)
);
CREATE TABLE Attend(
A_PID integer NOT NULL REFERENCES Patient (PID),
A_AppNumber integer NOT NULL REFERENCES Appointment(AppNumber)
);
HEllo, I have these tables and I want to find for each PID the last Date he attended an appointment. I tried with joins but nothing worked. I am using Postgres. Does anyone have an idea about it?
Thank you very much
Give this a try, joining through
AttendandAppointment.For patients attending 2+ appointments per month you need
COUNT()inside aHAVINGclause.