That's my first day in SQL using PostgreSQL 9.4 and I'm lost with some things. I think that I'm close but not enough:
Table definition:
CREATE TABLE DOCTOR (
Doc_Number INTEGER,
Name VARCHAR(50) NOT NULL,
Specialty VARCHAR(50) NOT NULL,
Address VARCHAR(50) NOT NULL,
City VARCHAR(30) NOT NULL,
Phone VARCHAR(10) NOT NULL,
Salary DECIMAL(8,2) NOT NULL,
DNI VARCHAR(10) UNIQUE
CONSTRAINT pk_Doctor PRIMARY KEY (Doc_Number)
);
CREATE TABLE VISIT (
Doc_Number INTEGER,
Pat_Number INTEGER,
Visit_Date DATE,
Price DECIMAL(7,2),
CONSTRAINT Visit_pk PRIMARY KEY (Doc_Number, Pat_Number, Visit_Date),
CONSTRAINT Visit_Doctor_fk FOREIGN KEY (Doc_Number) REFERENCES DOCTOR(Doc_Number),
CONSTRAINT Visit_PATIENT_fk FOREIGN KEY (Pat_Number) REFERENCES PATIENT(Pat_Number)
);
I need how to join these two queries into one:
SELECT d.City, d.Name
FROM DOCTOR d, VISIT v
WHERE d.Specialty = 'family and comunity'
ORDER BY d.Name;
SELECT * FROM VISIT
WHERE DATE (Visit_Date)<'01/01/2012'
OR DATE(Visit_Date)>'31/12/2013';
I tried something like this but it doesn't work. I need the doctors of that specialty that didn't do any visit in 2012 and 2013.
SELECT City, Name
FROM DOCTOR d
WHERE d.Specialty = 'family and comunity'
AND NOT IN(SELECT *
FROM VISIT
WHERE Visit_Date BETWEEN '2012-01-01' and '2013-12-31')
ORDER BY d.Name;
Can anyone help?