I want to make a query to update the salary of all doctors that have been practicing for more than 10 years, the new salary should correspond to a raise of 10 percent over the original salary, in the case of doctors with more than 100 consultations in the current year.
My database has these tables:
create table employee (
employee_VAT varchar(255),
employee_name varchar(255),
employee_birth_date date,
employee_street varchar(255),
employee_city varchar(255),
employee_ZIP varchar(255),
employee_IBAN varchar(255),
employee_salary numeric(20,2),
primary key(employee_VAT),
unique(employee_IBAN)
);
create table doctor (
employee_VAT varchar(255),
specialization varchar(255),
biography text,
email varchar(255) not null,
primary key(employee_VAT),
foreign key(employee_VAT) references employee(employee_VAT),
unique(email)
);
create table appointment (
doctor_VAT varchar(255),
date_timestamp datetime,
appointment_description text,
client_VAT varchar(255),
primary key(doctor_VAT, date_timestamp),
foreign key(doctor_VAT) references doctor(employee_VAT),
foreign key(client_VAT) references client(client_VAT)
);
create table consultation (
doctor_VAT varchar(255),
date_timestamp datetime,
SOAP_S text,
SOAP_O text,
SOAP_A text,
SOAP_P text,
primary key(doctor_VAT, date_timestamp),
foreign key(doctor_VAT, date_timestamp) references appointment(doctor_VAT, date_timestamp)
);
I have come close with this query I wrote:
update employee
set employee_salary = employee_salary + 0.1 * employee_salary
where
employee_VAT in (
select pd.employee_VAT
from permanent_doctor as pd
where years >= 10
)
and employee_VAT in (
select doctor_VAT
from consultation
where doctor_VAT in (
select doctor_VAT
from consultation
group by doctor_VAT having count(*) > 100
)
);
However this doesn't account for the consultations being in the current year like I want. I know that I'll probably have to incorporate year(date_timestamp) = year(now())
somewhere, but I don't know how. What am I missing here?