Sunteți pe pagina 1din 5

1) Cati pacienti sunt femei si cati barbati sunt?

SELECT COUNT(*) FROM test1 where gender='male'

SELECT COUNT(*) FROM test1 WHERE gender="female"

2) Porniti de la ideea ca pacientii care au “otherdisease” ce incepe cu


litera “l” sau “k” sunt din Bucuresti; cati pacienti sunt din Bucuresti?

SELECT COUNT(*) FROM test1 WHERE otherdisease LIKE "k%" OR otherdiseas


e LIKE "l%"

# 3) Acceptand ca regula de la nr. 2 este reala, adaugati o coloana, numita


'Oras' si populati-o conform regulii. Coloana sa fie de tip caracter, cu un
camp, valori 'Bucuresti' sau 'Alte orase';

ALTER TABLE test1 ADD city varchar(15)

ALTER TABLE test1 ADD city varchar(15)

UPDATE test1 SET city="Alte orase" WHERE otherdisease LIKE 'm%'

4) Scrieti o comanda care sa numere cati pacienti sunt din Bucuresti. La fel
pentru numarul de pacienti din alte orase. Folosind in ambele cazuri coloana
'Oras'.

SELECT COUNT(*) FROM test1 where city=”Bucuresti”;

SELECT COUNT(*) FROM test1 WHERE city=”Alte orase”;

# 5) Care este varsta maxima a pacientilor (sex masculin). Dar a pacientelor


(sex feminin) ?

SELECT MAX(age) FROM test1 WHERE gender="male"


SELECT MAX(age) FROM test1 WHERE gender="female"
# 6) Creati un tabel cu 4 coloane: 'Gen', 'Varsta medie', 'Varsta minima',
'Varsta maxima'.

CREATE TABLE IF NOT EXISTS info_pac ( Gen varchar(10), Varsta_medie in


t(3), Varsta_minima int(3), Varsta_maxima int(3))

7) Calculati varsta medie a pacientelor si a pacientilor.

SELECT AVG(age) FROM test1

# 8) Calculati media diferentei dintre “viruslevel2” – “viruslevel1” pe


fiecare “treatmenttype”. Cum a-ti pondera-o si cu timpul de tratament?

SELECT AVG(viruslevel1-
viruslevel2) FROM test1 WHERE treatmenttype="drug2"

SELECT AVG(viruslevel1-
viruslevel2) FROM test1 WHERE treatmenttype="drug1"

SELECT AVG(viruslevel1-
viruslevel2) FROM test1 WHERE treatmenttype="placebo"

SELECT SUM((viruslevel2-viruslevel1)*(time2-time1))/sum(time2-time1)
FROM test1 WHERE treatmenttype="drug2"

SELECT SUM((viruslevel2-viruslevel1)*(time2-time1))/sum(time2-time1)
FROM test1 WHERE treatmenttype="drug1"

SELECT SUM((viruslevel2-viruslevel1)*(time2-time1))/sum(time2-time1)
FROM test1 WHERE treatmenttype="placebo"

# 9) Modificati tabela “test1” astfel:

# - creati tabelele “tPacient”, “tGen”, “tTratament” si “tAlteboli”;

# - stabiliti relatiile corecte (references) intre noile tabele. Atentie,


tabela “test1” trebuie si ea refactorizata; eventual, pentru simplitate,
lucrati cu o copie a lui “test1” pentru noua structura.

# - populati aceste tabele cu informatiile din tabela “test1”;

# - cate “treatmenttype” diferite sunt pentru fiecare “otherdisease” ?


CREATE TABLE IF NOT EXISTS test1_copy
(id int primary key auto_increment not null,
gender varchar(6),
age smallint(3),
treatmenttype varchar(10),
time1 time,
viruslevel1 float(11,8),
time2 time,
viruslevel2 float(11,8),
otherdisease varchar(20),
city varchar(15));

INSERT INTO test1_copy SELECT * FROM test1

CREATE TABLE IF NOT EXISTS tGen

(id_gen int primary key AUTO_INCREMENT not null,

gender varchar(6));

CREATE TABLE IF NOT EXISTS tTratament

(id_tratament int primary key AUTO_INCREMENT not null,

treatmenttype varchar(10));

CREATE TABLE IF NOT EXISTS tAlteboli

(id_boli int PRIMARY KEY AUTO_INCREMENT NOT NULL,

otherdisease varchar(20));

CREATE TABLE IF NOT EXISTS tPacient

(id_pacient int primary key AUTO_INCREMENT not null,

age smallint(3),

gen varchar(6),

tratament varchar(20),

boala varchar(20),
id_gen int,

id_tratament int,

id_boli int,

FOREIGN KEY (id_gen) REFERENCES tGen(id_gen),

FOREIGN KEY (id_tratament) REFERENCES tTratament (id_tratament),

FOREIGN KEY (id_boli) REFERENCES tAlteboli(id_boli));

INSERT INTO tGen (gender) SELECT DISTINCT gender FROM test1;

INSERT INTO tTratament (treatmenttype) SELECT DISTINCT treatmenttype


FROM test1_copy;

INSERT INTO tAlteboli (otherdisease) SELECT DISTINCT otherdisease FROM


test1_copy;

INSERT INTO tPacient (age, gen, tratament, boala) SELECT DISTINCT


age, gender, treatmenttype, otherdisease FROM test1_copy;

UPDATE tPacient JOIN tGen SET tPacient.id_gen=tGen.id_gen WHERE tPacie


nt.gen=tGen.gender

UPDATE tPacient JOIN tTratament SET tPacient.id_tratament=tTratament.i


d_tratament WHERE tPacient.tratament=tTratament.treatmenttype

UPDATE tPacient JOIN tAlteboli SET tPacient.id_boli=tAlteboli.id_boli


WHERE tPacient.boala=tAlteboli.otherdisease

# - cate “treatmenttype” diferite sunt pentru fiecare “otherdisease” ?

SELECT COUNT(*) FROM tPacient WHERE boala like 'l%';

SELECT COUNT(*) FROM tPacient WHERE boala LIKE 'k%';

SELECT COUNT(*) FROM tPacient WHERE boala LIKE 'm%';

# 10) Pentru un anumit “otherdisease” (la alegere), exportati cateva


rezultate statistice:
# - cati pacienti au varsta pana in 50 de ani;

SELECT COUNT(*) FROM test1 WHERE (age<50) AND otherdisease=”kidney


failure”;

# - care dintre sexe (feminin sau masculin) are media de varsta cea mai
ridicata; dar cea mai scazuta?

# - modificati “treatmenttype”, daca pacientii au intre 20 si 30 ani, din


“drug1” in “placebo”. Compuneti si comanda pentru a face schimbarea invers.

UPDATE test1 SET treatmenttype ="placebo" WHERE age BETWEEN 20 AND 30


AND treatmenttype="drug1"

S-ar putea să vă placă și