如何在SQL中获取每个元素的最大值
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)
);
你好,我有这些表,我想找到每个 PID 他参加约会的最后日期。我尝试过连接,但没有任何效果。我正在使用 Postgres。有人对此有什么想法吗?
非常感谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试一下,通过
出席
和预约
加入。对于每月参加 2 次以上预约的患者,您需要在
HAVING
子句中添加COUNT()
。Give this a try, joining through
Attend
andAppointment
.For patients attending 2+ appointments per month you need
COUNT()
inside aHAVING
clause.