How do I include certain conditions in SQL Count - sql
`CREATE TABLE PERSON
(
Person_id NUMBER(3) PRIMARY KEY,
Surname VARCHAR2(20),
First_name VARCHAR2(20),
Sex CHAR(1),
Birth_date DATE,
Street VARCHAR2(40),
Town CHAR(20),
Postcode NUMBER(4),
Next_of_kin NUMBER(3)
);
CREATE TABLE STAFF
(
Person_id NUMBER(3) PRIMARY KEY,
Start_date DATE,
Staff_type VARCHAR2(15),
Charges NUMBER(10,2),
Resign_date DATE,
FOREIGN KEY (Person_id) references PERSON(Person_id)
);
CREATE TABLE WARD (
Ward_code CHAR(3) PRIMARY KEY,
Ward_name VARCHAR2(20),
Bed_count NUMBER(4),
Opened_date DATE,
Last_painted_date DATE,
Daily_charge NUMBER(10,2)
);
CREATE TABLE OPERATION_TYPE (
Op_code CHAR(3) PRIMARY KEY,
Operation_name VARCHAR2(50),
Theatre_fee NUMBER(10,2),
Days_in NUMBER(5)
);
CREATE TABLE ADMISSION (
Admission_id NUMBER(3) PRIMARY KEY,
Patient_id NUMBER(3),
Admission_date DATE NOT NULL,
Expected_op CHAR(3),
Admitted_by NUMBER(3),
Ward_code CHAR(3),
Discharge_date DATE,
FOREIGN KEY (Patient_id) references PERSON(Person_id),
FOREIGN KEY (Expected_op) references OPERATION_TYPE(Op_code),
FOREIGN KEY (Admitted_by) references PERSON(Person_id),
FOREIGN KEY (Ward_code) references WARD(Ward_code)
);
CREATE TABLE OPERATION (
Operation_id NUMBER(3) PRIMARY KEY,
Actual_op CHAR(3),
Admission_id NUMBER(3),
Op_date DATE,
Surgeon NUMBER(3),
Anaesthetist NUMBER(3),
FOREIGN KEY (Surgeon) references PERSON(Person_id),
FOREIGN KEY (Anaesthetist) references PERSON(Person_id),
FOREIGN KEY (Actual_op) references OPERATION_TYPE(Op_code),
FOREIGN KEY (Admission_id) references ADMISSION(Admission_id)
);
CREATE TABLE OBSERVATION(
Admission_id NUMBER(3),
Observ_date DATE,
Observ_time NUMBER(4),
Observ_type CHAR(10),
Observ_value NUMBER(4),
Staff_id NUMBER(3),
PRIMARY KEY (Admission_id,Observ_date,Observ_time,Observ_type),
FOREIGN KEY (Admission_id) references ADMISSION(Admission_id),
FOREIGN KEY (Staff_id) references STAFF(Person_id)
);
REM***********************
REM PERSON TABLE
REM***********************
INSERT INTO PERSON VALUES (101,'Black','Barry','M','31/12/1959','11 High St.','Cooma',2630,102);
INSERT INTO PERSON VALUES (102,'Black','Mary','F','11/04/1965','11 High St.','Cooma',2630,NULL);
INSERT INTO PERSON VALUES (103,'Strathclyde','Albert','M','15/5/1955','3 The Mews','Hawthorne',3171,104);
INSERT INTO PERSON VALUES (104,'Strathclyde','Alice','F','17/7/1955','3 The Mews','Hawthorne',3171,103);
INSERT INTO PERSON VALUES (105,'Green','Gill','F','16/6/1966','124 Main St.','Young',2594,106);
INSERT INTO PERSON VALUES (106,'Green','Graham','M','24/4/1967','124 Main St.','Young',2594,105);
INSERT INTO PERSON VALUES (107,'Gray','Lesley','F','12/9/1972','130 Main St.','Young',2594,109);
INSERT INTO PERSON VALUES (109,'Gray','John','M','14/4/1972','130 Main St.','Young',2594,107);
INSERT INTO PERSON VALUES (110,'Samuelson','Thomas','M','1/1/1964','17 The Mews','Hawthorne',3171,NULL);
INSERT INTO PERSON VALUES (111,'Abrahams','Mary','F','15/5/1967','2177A The Esplanade','Ivanhoe',3878,NULL);
INSERT INTO PERSON VALUES (112,'Aumann','Monica','F','25/5/1955','29 The Esplanade','Ivanhoe',3878,NULL);
INSERT INTO PERSON VALUES (113,'Brown','Melissa','F','8/8/1984','11 East St.','Cooma',2630,NULL);
INSERT INTO PERSON VALUES (114,'Napier','Mary','F','1/1/1971','163 New Rd.','Henty',2658,NULL);
INSERT INTO PERSON VALUES (115,'Nelson','Nigel','M','2/2/1972','165 Young Rd.','Temora',2666,NULL);
INSERT INTO PERSON VALUES (116,'Newman','Olive','F','3/3/1973','21 Olympic Way','Henty',2658,NULL);
INSERT INTO PERSON VALUES (117,'Gray','Lesley','M','31/12/1989','130 Andres St.','Young',2594,105);
INSERT INTO PERSON VALUES (118,'Hon','Tasuku','M','13/3/1953','21 Silcon Height','Henty',2658,NULL);
INSERT INTO PERSON VALUES (119,'Livingstone','Frank','M','3/3/2003','21 Sun Height','Henty',2658,122);
INSERT INTO PERSON VALUES (120,'Giggle','Frank','M','23/3/1975','21 Albrige Close','Cooma',2630,121);
INSERT INTO PERSON VALUES (121,'Giggle','Felicia','F','3/3/1980','21 Albrige Close','Cooma',2630,120);
INSERT INTO PERSON VALUES (122,'Black','Frank Jr','M','13/3/2011','21 Stun Height','Henty',2658,123);
INSERT INTO PERSON VALUES (123,'Black','Frances','F','12/12/2005','21 Stun Height','Henty',2658,122);
INSERT INTO PERSON VALUES (124,'Smith','Buddy','M','11/12/1979','101 High St.','Cooma',2630,NULL);
INSERT INTO PERSON VALUES (125,'Smith','Maxime','F','31/12/1979','101 High St.','Cooma',2630,124);
INSERT INTO PERSON VALUES (126,'Smith','Issac','M','1/12/2007','101 High St.','Cooma',2630,124);
INSERT INTO PERSON VALUES (127,'Smith','Ronny','M','3/12/2009','101 High St.','Cooma',2630,124);
INSERT INTO PERSON VALUES (128,'Giggle','Fanny','F','3/12/2007','121 Close Rose','Hillo',2330,120);
INSERT INTO PERSON VALUES (129,'Murad','Nadia','F','3/3/2000','121 Close Rose', 'Hillo',2330,130);
INSERT INTO PERSON VALUES (130,'Murad','Tange','M','3/3/1999','7711 Albrige Close','Cooma',2630,NULL);
INSERT INTO PERSON VALUES (131,'Rome','Paula','F','23/9/1965','21 Height Close','Cooma',2630,132);
INSERT INTO PERSON VALUES (132,'Rome','Paul','M','13/3/1966','21 Height Close','Cooma',2630,NULL);
INSERT INTO PERSON VALUES (133,'Rome','Fay','M','3/3/2017','21 Height Close','Cooma',2630,132);
INSERT INTO PERSON VALUES (134,'Murad','Michelle','F','3/3/2001','1 Height Close','Cooma',2630,NULL);
INSERT INTO PERSON VALUES (135,'Trump','Donald','M','13/3/1956','222 White House Avenue','Cooma',2630,NULL);
INSERT INTO PERSON VALUES (136,'Trump','Melania','F','3/3/1992','222 White House Avenue','Cooma',2630,135);
INSERT INTO PERSON VALUES (137,'Trump','Baron','M','3/6/2005','222 White House Avenue','Cooma',2630,135);
INSERT INTO PERSON VALUES (138,'Johnson','Boris','M','23/9/1965','10 Downing Street','Wagga Wagga',2999,NULL);
INSERT INTO PERSON VALUES (139,'Cordeiro','Wayne','M','3/3/1965','777 Hawaii Close Rose', 'Hillo',7770, NULL);
INSERT INTO PERSON VALUES (140,'Cordeiro','Anne','F','23/4/1968','777 Hawaii Close Rose', 'Hillo',7770, 139);
INSERT INTO PERSON VALUES (141,'Thatcher','Margaret','F','21/1/1955','120 Main Sq.','Wagga Wagga',2999,142);
INSERT INTO PERSON VALUES (142,'Thatcher','Denis','M','23/9/1955','120 Main Sq.','Wagga Wagga',2999,NULL);
INSERT INTO PERSON VALUES (143,'Thatcher','Carols','F','1/9/1985','120 Main Sq.','Wagga Wagga',2999,142);
INSERT INTO PERSON VALUES (144,'Nelson','Nigel','M','22/2/1992','15 Young Rd.','Temora',2666,NULL);
INSERT INTO PERSON VALUES (145,'Neon','Gela','F','2/2/1972','1465 Main Rd.','Temora',2666,NULL);
INSERT INTO PERSON VALUES (146,'Twain','Shane','F','21/1/1995','A-129 Main Rose Sq.','Wagga Wagga',2650,147);
INSERT INTO PERSON VALUES (147,'Twain','Dens','M','23/9/1985','A-129 Main Rose Sq.','Wagga Wagga',2650,NULL);
INSERT INTO PERSON VALUES (148,'Trump','Ivanka','F','5/3/1985','222 White House Avenue','Cooma',2630,135);
INSERT INTO PERSON VALUES (149,'Trump','Eric','M','3/12/1975','222 White House Avenue','Cooma',2630,135);
INSERT INTO PERSON VALUES (150,'Gates','Bill','M','5/3/1975','2 Rosey Lane','Cooma',2630,NULL);
INSERT INTO PERSON VALUES (151,'Bucket','Eric','M','3/1/1985','11 Oserey Avenue','Cooma',2630,NULL);
REM*******************************************************
REM STAFF TABLE
REM nursing services will not be charged to the patients
REM*******************************************************
INSERT INTO STAFF VALUES (103,'1/1/2009','Surgeon',4525,NULL);
INSERT INTO STAFF VALUES (110,'5/5/2009','Surgeon',5600,NULL);
INSERT INTO STAFF VALUES (118,'1/5/2016','Surgeon',7890,NULL);
INSERT INTO STAFF VALUES (111,'1/1/2009','Anaesthetist',5900,NULL);
INSERT INTO STAFF VALUES (112,'3/3/2009','Anaesthetist',4788,NULL);
INSERT INTO STAFF VALUES (114,'1/1/2009','Senior Nurse',NULL,NULL);
INSERT INTO STAFF VALUES (115,'2/2/2016','Nurse',NULL,NULL);
INSERT INTO STAFF VALUES (116,'3/3/2014','Nurse',NULL,NULL);
INSERT INTO STAFF VALUES (125,'23/3/2014','Nurse',NULL,NULL);
INSERT INTO STAFF VALUES (135,'1/1/2019','Senior Nurse',NULL,NULL);
INSERT INTO STAFF VALUES (136,'2/2/2016','Nurse',NULL,NULL);
INSERT INTO STAFF VALUES (138,'23/3/2019','Nurse',NULL,NULL);
INSERT INTO STAFF VALUES (139,'23/3/2020','Senior Nurse',NULL,NULL);
INSERT INTO STAFF VALUES (150,'1/11/2019','Anaesthetist',6900,NULL);
INSERT INTO STAFF VALUES (151,'31/1/2019','Anaesthetist',5900,NULL);
REM***********************
REM WARD TABLE
REM***********************
INSERT INTO WARD VALUES ('C','Covid Bay',90,'31/12/2019',NULL,150.00);
INSERT INTO WARD VALUES ('A','Abraham',80,'1/12/2015','12/05/2011',350.00);
INSERT INTO WARD VALUES ('N','Nightingale',50,'1/12/2017','12/05/2012',450.00);
INSERT INTO WARD VALUES ('F','Flemming',75,'15/11/2009',NULL,230.00);
INSERT INTO WARD VALUES ('L','Lister',80,'1/1/2009','20/12/2013',200.00);
INSERT INTO WARD VALUES ('P','Pasteur',60,'1/12/2009','12/12/2011',250.00);
REM***********************
REM OPERATION_TYPE TABLE
REM***********************
INSERT INTO OPERATION_TYPE VALUES ('LO','Lobotomy',700.00,10);
INSERT INTO OPERATION_TYPE VALUES ('CS','Caesarean',5700.00,3);
INSERT INTO OPERATION_TYPE VALUES ('CT','Cataract',670.00,1);
INSERT INTO OPERATION_TYPE VALUES ('AP','Appendicectomy',500.00,5);
INSERT INTO OPERATION_TYPE VALUES ('HB','Heart Bypass',2000.00,14);
INSERT INTO OPERATION_TYPE VALUES ('HT','Heart Transplant',5000.00,30);
INSERT INTO OPERATION_TYPE VALUES ('HY','Hysterectomy',800.00,7);
INSERT INTO OPERATION_TYPE VALUES ('LA','Leg Amputation',1500.00,10);
INSERT INTO OPERATION_TYPE VALUES ('TS','Tonsillectomy',700.00,7);
INSERT INTO OPERATION_TYPE VALUES ('LP','Laparoscopy',500.00,1);
INSERT INTO OPERATION_TYPE VALUES ('AR','Arthroscopy ',700.00,17);
REM***********************
REM ADMISSION TABLE
REM***********************
INSERT INTO ADMISSION VALUES (205,101,'2/2/2011','HB',114,'P','21/2/2011');
INSERT INTO ADMISSION VALUES (275,101,'1/9/2010','HY',115,'L','1/11/2010');
INSERT INTO ADMISSION VALUES (286,101,'3/5/2016','AR',116,'A','3/7/2016');
INSERT INTO ADMISSION VALUES (303,101,'3/4/2018','LA',125,'F', '13/5/2018');
INSERT INTO ADMISSION VALUES (298,103,'23/1/2016','TS',114,'L','24/04/2016');
INSERT INTO ADMISSION VALUES (299,103,'23/3/2018','AP',114,'L','23/4/2018');
INSERT INTO ADMISSION VALUES (305,103,'23/4/2018','HT',125,'F','29/5/2018');
INSERT INTO ADMISSION VALUES (321,103,'13/8/2018','AR',125,'F', '23/10/2018');
INSERT INTO ADMISSION VALUES (283,105,'3/12/2015','AR',116,'A','5/12/2015');
INSERT INTO ADMISSION VALUES (278,105,'1/1/2011','HB',115,'P','30/1/2011');
INSERT INTO ADMISSION VALUES (307,105,'3/4/2018','TS',125,'F', '13/5/2018');
INSERT INTO ADMISSION VALUES (276,106,'24/8/2010','LA',114,'P','15/9/2010');
INSERT INTO ADMISSION VALUES (287,106,'3/5/2016','TS',114,'A','3/6/2016');
INSERT INTO ADMISSION VALUES (274,109,'1/9/2019','AP',114,'P','9/9/2019');
INSERT INTO ADMISSION VALUES (288,109,'23/5/2016','LO',114,'F','3/07/2016');
INSERT INTO ADMISSION VALUES (301,112,'13/4/2018','AP',125,'F','16/4/2018');
INSERT INTO ADMISSION VALUES (304,112,'23/4/2019','LO',114,'L','23/5/2019');
INSERT INTO ADMISSION VALUES (279,113,'3/9/2010','TS',115,'F','10/9/2010');
INSERT INTO ADMISSION VALUES (284,113,'3/12/2015','HY',116,'A','03/01/2016');
INSERT INTO ADMISSION VALUES (285,113,'3/5/2016','HT',116,'A','3/6/2016');
INSERT INTO ADMISSION VALUES (300,113,'23/4/2018','AR',114,'L','25/6/2018');
INSERT INTO ADMISSION VALUES (306,113,'13/8/2018','AP',125,'L', '13/9/2018');
INSERT INTO ADMISSION VALUES (277,114,'20/9/2010','AP',115,'P','30/9/2010');
INSERT INTO ADMISSION VALUES (289,115,'11/4/2016','LO',114,'L','3/6/2016');
INSERT INTO ADMISSION VALUES (290,115,'5/7/2016','TS',114,'L','3/09/2016');
INSERT INTO ADMISSION VALUES (308,115,'23/3/2018','AR',114,'L','25/3/2018');
INSERT INTO ADMISSION VALUES (280,117,'13/9/2010','AP',115,'F','25/9/2010');
INSERT INTO ADMISSION VALUES (281,117,'3/9/2014','HB',116,'A','21/9/2014');
INSERT INTO ADMISSION VALUES (282,117,'3/12/2015','LA',116,'A','14/12/2015');
INSERT INTO ADMISSION VALUES (309,126,'23/3/2018','TS',125,'L', '13/5/2018');
INSERT INTO ADMISSION VALUES (310,127,'13/5/2018','AP',125,'L', '28/5/2018');
INSERT INTO ADMISSION VALUES (311,124,'3/5/2018','LO',125,'A', '23/5/2018');
INSERT INTO ADMISSION VALUES (312,127,'21/6/2019','LO',125,'L','22/8/2019');
INSERT INTO ADMISSION VALUES (313,124,'22/6/2019','AP',125,'A','22/7/2019');
INSERT INTO ADMISSION VALUES (314,109,'21/6/2019','LO',125,'L','22/7/2019');
INSERT INTO ADMISSION VALUES (315,126,'12/6/2019','AP',125,'A','22/9/2019');
INSERT INTO ADMISSION VALUES (316,114,'22/7/2019','HB',125,'A','12/12/2019');
INSERT INTO ADMISSION VALUES (318,128,'3/5/2019','LA',116,'A','4/6/2019');
INSERT INTO ADMISSION VALUES (319,129,'23/3/2019','TS',125,'L', '13/4/2019');
INSERT INTO ADMISSION VALUES (320,130,'3/5/2019','LA',116,'F','4/6/2019');
INSERT INTO ADMISSION VALUES (328,119,'3/3/2019','TS',115,'N', '3/4/2019');
INSERT INTO ADMISSION VALUES (322,132,'3/5/2019','LA',116,'A','4/6/2019');
INSERT INTO ADMISSION VALUES (323,133,'23/3/2019','TS',125,'L', '13/4/2019');
INSERT INTO ADMISSION VALUES (324,131,'3/5/2018','LA',116,'F','4/5/2018');
INSERT INTO ADMISSION VALUES (325,118,'3/6/2019','TS',115,'P', '3/7/2019');
INSERT INTO ADMISSION VALUES (326,102,'3/5/2018','LA',116,'F','4/5/2018');
INSERT INTO ADMISSION VALUES (327,104,'3/6/2019','TS',115,'P', '3/7/2019');
INSERT INTO ADMISSION VALUES (339,107,'3/6/2019','TS',115,'P', '3/7/2019');
INSERT INTO ADMISSION VALUES (329,110,'3/6/2019','TS',115,'P', '3/7/2019');
INSERT INTO ADMISSION VALUES (330,111,'3/6/2019','TS',115,'P', '3/7/2019');
INSERT INTO ADMISSION VALUES (331,116,'3/6/2019','TS',115,'P', '3/7/2019');
INSERT INTO ADMISSION VALUES (332,121,'21/6/2019','TS',125,'L','22/7/2019');
INSERT INTO ADMISSION VALUES (333,123,'22/7/2019','AP',125,'A','22/9/2019');
INSERT INTO ADMISSION VALUES (334,134,'21/8/2019','AP',115,'L','22/12/2019');
INSERT INTO ADMISSION VALUES (335,128,'12/8/2020','AP',115,'A',NULL);
INSERT INTO ADMISSION VALUES (336,125,'22/7/2020','HB',115,'A',NULL);
INSERT INTO ADMISSION VALUES (337,120,'21/8/2020','AP',116,'L',NULL);
INSERT INTO ADMISSION VALUES (338,130,'22/7/2020','AP',125,'N',NULL);
INSERT INTO ADMISSION VALUES (340,131,'22/8/2020','AP',125,'N',NULL);
INSERT INTO ADMISSION VALUES (341,122,'22/8/2020','AP',125,'N',NULL);
INSERT INTO ADMISSION VALUES (490,139,'11/8/2020','AP',125,'F',NULL);
INSERT INTO ADMISSION VALUES (491,140,'7/9/2020','AP',138,'F',NULL);
REM***********************
REM OPERATION TABLE
REM***********************
INSERT INTO OPERATION VALUES (317,'HB',205,'4/2/2011',110,111);
INSERT INTO OPERATION VALUES (355,'LA',276,'25/8/2010',103,111);
INSERT INTO OPERATION VALUES (363,'AP',276,'3/9/2010',103,111);
INSERT INTO OPERATION VALUES (360,'AP',277,'22/9/2010',103,111);
INSERT INTO OPERATION VALUES (361,'AP',274,'2/9/2010',103,111);
INSERT INTO OPERATION VALUES (362,'HT',275,'3/9/2010',110,112);
INSERT INTO OPERATION VALUES (364,'AP',279,'3/9/2010',103,111);
INSERT INTO OPERATION VALUES (365,'AP',280,'6/9/2010',103,111);
INSERT INTO OPERATION VALUES (366,'HB',281,'6/9/2014',103,111);
INSERT INTO OPERATION VALUES (367,'HY',284,'6/12/2015',103,111);
INSERT INTO OPERATION VALUES (368,'LO',285,'6/5/2016',118,112);
INSERT INTO OPERATION VALUES (369,'AP',286,'6/5/2016',118,112);
INSERT INTO OPERATION VALUES (370,'TS',287,'7/5/2016',118,112);
INSERT INTO OPERATION VALUES (371,'LA',288,'26/5/2016',110,111);
INSERT INTO OPERATION VALUES (372,'LO',289,'16/4/2016',110,111);
INSERT INTO OPERATION VALUES (373,'TS',290,'6/5/2016',110,111);
INSERT INTO OPERATION VALUES (381,'TS',298,'26/2/2016',110,111);
INSERT INTO OPERATION VALUES (382,'AP',299,'26/3/2018',110,111);
INSERT INTO OPERATION VALUES (383,'TS',300,'26/5/2018',110,111);
INSERT INTO OPERATION VALUES (384,'TS',309,'16/4/2018',110,111);
INSERT INTO OPERATION VALUES (385,'LO',309,'26/3/2018',110,111);
INSERT INTO OPERATION VALUES (386,'AP',310,'18/5/2018',110,111);
INSERT INTO OPERATION VALUES (387,'AP',311,'4/5/2018',110,111);
INSERT INTO OPERATION VALUES (388,'LO',311,'11/5/2018',110,111);
INSERT INTO OPERATION VALUES (389,'AP',315,'13/6/2019',118,111);
INSERT INTO OPERATION VALUES (390,'HB',316,'23/7/2019',118,111);
INSERT INTO OPERATION VALUES (391,'LO',312,'22/6/2019',118,111);
INSERT INTO OPERATION VALUES (392,'HT',341,'28/8/2019',118,112);
INSERT INTO OPERATION VALUES (393,'HT',340,'27/8/2019',118,112);
INSERT INTO OPERATION VALUES (394,'LO',491,'26/4/2020',103,112);
REM***********************
REM OBSERVATION TABLE
REM***********************
INSERT INTO OBSERVATION VALUES (205,'2/2/2011',1500,'Temp',38,114);
INSERT INTO OBSERVATION VALUES (274,'4/9/2010',0601,'Temp',39,116);
INSERT INTO OBSERVATION VALUES (275,'1/9/2010',1400,'Pulse',64,115);
INSERT INTO OBSERVATION VALUES (275,'1/9/2010',1400,'Temp',38,115);
INSERT INTO OBSERVATION VALUES (275,'3/9/2010',1800,'Temp',40,116);
INSERT INTO OBSERVATION VALUES (275,'3/9/2010',2200,'Temp',40,116);
INSERT INTO OBSERVATION VALUES (275,'4/9/2010',0610,'Pulse',82,116);
INSERT INTO OBSERVATION VALUES (275,'4/9/2010',0610,'Temp',38,116);
INSERT INTO OBSERVATION VALUES (277,'24/9/2010',0600,'Temp',39,115);
INSERT INTO OBSERVATION VALUES (284,'5/12/2015',0600,'Temp',39,115);
INSERT INTO OBSERVATION VALUES (284,'5/12/2015',0600,'Pulse',89,115);
INSERT INTO OBSERVATION VALUES (284,'6/12/2015',0600,'Temp',37,115);
INSERT INTO OBSERVATION VALUES (284,'6/12/2015',0600,'Pulse',87,115);
INSERT INTO OBSERVATION VALUES (285,'7/5/2016',0600,'Temp',38,115);
INSERT INTO OBSERVATION VALUES (285,'8/5/2016',0600,'Temp',38,115);
INSERT INTO OBSERVATION VALUES (285,'8/5/2016',0600,'Pulse',82,115);
INSERT INTO OBSERVATION VALUES (285,'9/5/2016',0600,'Temp',37,115);
INSERT INTO OBSERVATION VALUES (286,'7/5/2016',0600,'Temp',39,125);
INSERT INTO OBSERVATION VALUES (286,'8/5/2016',0600,'Temp',33,125);
INSERT INTO OBSERVATION VALUES (286,'8/5/2016',0600,'Pulse',86,125);
INSERT INTO OBSERVATION VALUES (286,'9/5/2016',0600,'Temp',36,125);
INSERT INTO OBSERVATION VALUES (287,'7/5/2016',0600,'Temp',35,115);
INSERT INTO OBSERVATION VALUES (287,'8/5/2016',0600,'Temp',37,115);
INSERT INTO OBSERVATION VALUES (287,'8/5/2016',0600,'Pulse',82,125);
INSERT INTO OBSERVATION VALUES (287,'9/5/2016',0600,'Temp',37,125);
INSERT INTO OBSERVATION VALUES (289,'19/5/2016',0600,'Temp',39,115);
INSERT INTO OBSERVATION VALUES (289,'20/5/2016',1400,'Temp',38,115);
INSERT INTO OBSERVATION VALUES (289,'22/5/2016',0800,'Temp',37,115);
INSERT INTO OBSERVATION VALUES (289,'26/5/2016',0700,'Temp',37,115);
INSERT INTO OBSERVATION VALUES (289,'29/5/2016',0700,'Temp',38,115);
INSERT INTO OBSERVATION VALUES (300,'23/4/2018',1400,'Pulse',74,115);
INSERT INTO OBSERVATION VALUES (300,'23/4/2018',1400,'Temp',38,115);
INSERT INTO OBSERVATION VALUES (300,'23/4/2018',1800,'Temp',30,116);
INSERT INTO OBSERVATION VALUES (300,'23/4/2018',2200,'Temp',40,116);
INSERT INTO OBSERVATION VALUES (300,'24/4/2018',0610,'Pulse',82,116);
INSERT INTO OBSERVATION VALUES (300,'24/4/2018',0610,'Temp',38,116);
INSERT INTO OBSERVATION VALUES (303,'13/4/2018',1400,'Pulse',64,125);
INSERT INTO OBSERVATION VALUES (303,'13/4/2018',1400,'Temp',38,125);
INSERT INTO OBSERVATION VALUES (303,'13/4/2018',1800,'Temp',30,125);
INSERT INTO OBSERVATION VALUES (303,'23/4/2018',2200,'Temp',40,116);
INSERT INTO OBSERVATION VALUES (303,'23/4/2018',0610,'Pulse',82,116);
INSERT INTO OBSERVATION VALUES (303,'23/4/2018',0610,'Temp',38,116);
INSERT INTO OBSERVATION VALUES (311,'3/5/2018',1400,'Pulse',54,115);
INSERT INTO OBSERVATION VALUES (311,'4/5/2018',1400,'Temp',38,115);
INSERT INTO OBSERVATION VALUES (311,'3/5/2018',1800,'Temp',40,116);
INSERT INTO OBSERVATION VALUES (311,'4/5/2018',2200,'Temp',41,116);
INSERT INTO OBSERVATION VALUES (311,'4/5/2018',1800,'Temp',39,116);
INSERT INTO OBSERVATION VALUES (311,'5/5/2018',0610,'Pulse',72,116);
INSERT INTO OBSERVATION VALUES (311,'5/5/2018',0610,'Temp',38,116);
INSERT INTO OBSERVATION VALUES (310,'13/5/2018',1400,'Pulse',84,125);
INSERT INTO OBSERVATION VALUES (310,'13/5/2018',1400,'Temp',28,125);
INSERT INTO OBSERVATION VALUES (310,'14/5/2018',1800,'Temp',60,125);
INSERT INTO OBSERVATION VALUES (310,'15/5/2018',2200,'Temp',40,116);
INSERT INTO OBSERVATION VALUES (310,'16/5/2018',0610,'Pulse',92,116);
INSERT INTO OBSERVATION VALUES (310,'16/5/2018',0610,'Temp',35,116);
INSERT INTO OBSERVATION VALUES (341,'22/8/2019',0610,'Temp',39,116);
INSERT INTO OBSERVATION VALUES (340,'25/8/2019',0610,'Temp',39,116);
INSERT INTO OBSERVATION VALUES (340,'25/8/2019',0610,'Pulse',75,116);
INSERT INTO OBSERVATION VALUES (340,'26/8/2019',0610,'Temp',37,116);
INSERT INTO OBSERVATION VALUES (340,'26/8/2019',0610,'Pulse',70,116);
INSERT INTO OBSERVATION VALUES (340,'27/8/2019',0610,'Pulse',68,116);
INSERT INTO OBSERVATION VALUES (340,'27/8/2019',0610,'Temp',37,116);
INSERT INTO OBSERVATION VALUES (340,'29/8/2019',0610,'Pulse',73,116);
INSERT INTO OBSERVATION VALUES (490,'17/4/2020',0600,'Temp',38,125);
INSERT INTO OBSERVATION VALUES (490,'18/4/2020',0600,'Temp',37,125);
INSERT INTO OBSERVATION VALUES (490,'19/4/2020',0600,'Temp',38,125);
select town, c.count(2021- to_char(birth_date,'YYYY'))100/count() as "Population"
from person, person c
group by town
Using Apex Oracle here, I need to set a condition for c.count where it only takes into account >60, but whenever I put the condition in, errors pop up, would greatly appreciate any help possible.
I'm assuming you don't really need a cross join of 'person' with 'person'. So I'm dropping that logic from the sources.
I don't have and Oracle instance to work with, but I believe the code below, which uses a case statement to achieve your conditional need, should work for you:
select town,
c.count(
case when ap.age > 60 then ap.age end
) 100 / count() as "Population"
from person
cross apply (select age = 2021 - to_char(birth_date,'YYYY')) ap
group by town
Related
SQL Query: To list the majors with more than 2 students in it
I am trying to figure out a query that will list the major that has more than 2 students in it (CS) but I am not sure how to go about it. This is the script to create the table for SQL! REM drop all the tables. Note that you need to drop the REM dependent table first before dropping the base tables. drop table Reg; drop table Student; drop table Course; REM Now create all the tables. create table Student ( sid char(10) primary key, sname varchar(20) not null, gpa float, major char(10), dob DATE ); create table Course ( cno char(10) primary key, cname varchar(20) not null, credits int, dept char(10) ); create table Reg ( sid references Student(sid) on delete cascade, cno references Course(cno) on delete cascade, grade char(2), primary key (sid, cno) ); REM Now insert all the rows. insert into Student values ('111', 'Joe', 3.5 , 'MIS', '01-AUG-2000'); insert into Student values ('222', 'Jack', 3.4 , 'MIS', '12-JAN-1999'); insert into Student values ('333', 'Jill', 3.2 , 'CS', '15-MAY-1998'); insert into Student values ('444', 'Mary', 3.7 , 'CS', '17-DEC-2001'); insert into Student values ('555', 'Peter', 3.8 , 'CS', '19-MAR-1999'); insert into Student values ('666', 'Pat', 3.9, 'Math', '31-MAY-2000'); insert into Student values ('777', 'Tracy', 4.0, 'Math', '18-JUL-1997'); insert into Course values ('c101', 'intro', 3 , 'CS'); insert into Course values ('m415', 'database', 4 , 'Bus'); insert into Course values ('m215', 'programming', 4 , 'Bus'); insert into Course values ('a444', 'calculus', 3 , 'Math'); insert into Reg values ('111', 'c101', 'A'); insert into Reg values ('111', 'm215', 'B'); insert into Reg values ('111', 'm415', 'A'); insert into Reg values ('222', 'm215', 'A'); insert into Reg values ('222', 'm415', 'B'); insert into Reg values ('333', 'c101', 'A'); insert into Reg values ('444', 'm215', 'C'); insert into Reg values ('444', 'm415', 'B'); insert into Reg values ('555', 'c101', 'B'); insert into Reg values ('555', 'm215', 'A'); insert into Reg values ('555', 'm415', 'A'); insert into Reg values ('666', 'c101', 'A'); Thank you so much!
this will list group by major within CS and having more than 2 select major ,count(1) from Student where major ='CS' group by major having count(1)>2
How can I fix the error in duplicate tuples and sort correctly?
Can anyone help me understand the error? My query: select s1.name, s1.grade, s2.name, s2.grade from student s1, student s2, likes l1, likes l2 where (s1.id_student = l1.id_student1 and s2.id_student = l1.id_student2) and (s2.id_student = l2.id_student1 and s1.id_student = l2.id_student2) and s1.name <= s2.name order by s1.name, s2.name; Return: Cassandra|9 Gabriel|9 Gabriel 11|Gabriel|9 Gabriel|9|Gabriel|11 Jessica 11|Kyle|12 But the correct thing would be [exactly in that order]: Cassandra|9|Gabriel|9 Jessica|11|Kyle|12 Gabriel|9|Gabriel|11 RS3 - For every pair of students who both like each other, return the name and grade of both students. Include each pair only once, with the two names in alphabetical order. CREATE SCHEMA socialnetworkschema; /* Create the schema for our tables */ SET SEARCH_PATH=socialnetworkschema; CREATE TABLE socialnetworkschema.student ( id_student SERIAL PRIMARY KEY, name varchar(255) DEFAULT NULL, grade int DEFAULT NULL ); CREATE TABLE socialnetworkschema.friend ( id_student1 int DEFAULT NULL, id_student2 int DEFAULT NULL, FOREIGN KEY (id_student1) REFERENCES socialnetworkschema.student (id_student) ON DELETE CASCADE ON UPDATE CASCADE, FOREIGN KEY (id_student2) REFERENCES socialnetworkschema.student (id_student) ON DELETE CASCADE ON UPDATE CASCADE ); CREATE TABLE socialnetworkschema.likes ( id_student1 int DEFAULT NULL, id_student2 int DEFAULT NULL, FOREIGN KEY (id_student1) REFERENCES socialnetworkschema.student (id_student) ON DELETE CASCADE ON UPDATE CASCADE, FOREIGN KEY (id_student2) REFERENCES socialnetworkschema.student (id_student) ON DELETE CASCADE ON UPDATE CASCADE ); /* Populate the tables with our data */ insert into socialnetworkschema.student values (1689, 'Gabriel', 9); insert into socialnetworkschema.student values (1510, 'Jordan', 9); insert into socialnetworkschema.student values (1381, 'Tiffany', 9); insert into socialnetworkschema.student values (1709, 'Cassandra', 9); insert into socialnetworkschema.student values (1101, 'Haley', 10); insert into socialnetworkschema.student values (1782, 'Andrew', 10); insert into socialnetworkschema.student values (1468, 'Kris', 10); insert into socialnetworkschema.student values (1641, 'Brittany', 10); insert into socialnetworkschema.student values (1247, 'Alexis', 11); insert into socialnetworkschema.student values (1316, 'Austin', 11); insert into socialnetworkschema.student values (1911, 'Gabriel', 11); insert into socialnetworkschema.student values (1501, 'Jessica', 11); insert into socialnetworkschema.student values (1304, 'Jordan', 12); insert into socialnetworkschema.student values (1025, 'John', 12); insert into socialnetworkschema.student values (1934, 'Kyle', 12); insert into socialnetworkschema.student values (1661, 'Logan', 12); insert into socialnetworkschema.friend values (1510, 1381); insert into socialnetworkschema.friend values (1510, 1689); insert into socialnetworkschema.friend values (1689, 1709); insert into socialnetworkschema.friend values (1381, 1247); insert into socialnetworkschema.friend values (1709, 1247); insert into socialnetworkschema.friend values (1689, 1782); insert into socialnetworkschema.friend values (1782, 1468); insert into socialnetworkschema.friend values (1782, 1316); insert into socialnetworkschema.friend values (1782, 1304); insert into socialnetworkschema.friend values (1468, 1101); insert into socialnetworkschema.friend values (1468, 1641); insert into socialnetworkschema.friend values (1101, 1641); insert into socialnetworkschema.friend values (1247, 1911); insert into socialnetworkschema.friend values (1247, 1501); insert into socialnetworkschema.friend values (1911, 1501); insert into socialnetworkschema.friend values (1501, 1934); insert into socialnetworkschema.friend values (1316, 1934); insert into socialnetworkschema.friend values (1934, 1304); insert into socialnetworkschema.friend values (1304, 1661); insert into socialnetworkschema.friend values (1661, 1025); insert into socialnetworkschema.friend select id_student2, id_student1 from friend; insert into socialnetworkschema.likes values(1689, 1709); insert into socialnetworkschema.likes values(1709, 1689); insert into socialnetworkschema.likes values(1782, 1709); insert into socialnetworkschema.likes values(1911, 1247); insert into socialnetworkschema.likes values(1247, 1468); insert into socialnetworkschema.likes values(1641, 1468); insert into socialnetworkschema.likes values(1316, 1304); insert into socialnetworkschema.likes values(1501, 1934); insert into socialnetworkschema.likes values(1934, 1501); insert into socialnetworkschema.likes values(1025, 1101); insert into socialnetworkschema.likes values(1689, 1911); insert into socialnetworkschema.likes values(1911, 1689);
First of all, you should consider rewriting your query to use explicit modern joins. After this, your logic for connecting two student records needs to change a bit. with cte as ( select s1.name as name1, s1.grade as grade1, s2.name as name2, s2.grade as grade2, row_number() over (partition by least(s1.name, s2.name), greatest(s1.name, s2.name) order by s1.name) rn from student s1 inner join likes l1 on s1.id_student = l1.id_student1 inner join student s2 on l1.id_student2 = s2.id_student inner join likes l2 on l2.id_student1 = s2.id_student and l2.id_student2 = s1.id_student where s1.id_student <> s2.id_student order by s1.name, s2.name ) select name1, grade1, name2, grade2 from cte where rn = 1; Demo Since your requirement is to find all students who mutually liked each other, we approach this by making a round trip to and from the student table. The likes table is what is known as a junction table in SQL. It exists to represent a one directional liked from one student to another. We join as follows: student -> likes -> student -> likes --- ^ | |___________________________________| That is, a mutual like means that there exists a path we can find matching the above cycle.
How do I find the count of distinct records since a certain date (eg: 2018 till current day)?
So I just recently started Oracle and I have to essentially find the patients that have been admitted in the hospital at least twice in the given hospital. Here are the tables I'm working with. Table Person:- CREATE TABLE PERSON ( Person_id NUMBER(3) PRIMARY KEY, Surname VARCHAR2(20), First_name VARCHAR2(20), Sex CHAR(1), Birth_date DATE, Street VARCHAR2(40), Town CHAR(9), Postcode NUMBER(4), Next_of_kin NUMBER(3) ); INSERT INTO PERSON VALUES (101,'Black','Barry','M','31/12/1959','11 High St.','Cooma',2630,102); INSERT INTO PERSON VALUES (102,'Black','Mary','F','11/1/1963','11 High St.','Cooma',2630,NULL); INSERT INTO PERSON VALUES (103,'Strathclyde','Albert','M','15/5/1955','3 The Mews','Hawthorne',3171,104); INSERT INTO PERSON VALUES (104,'Strathclyde','Alice','F','17/7/1955','3 The Mews','Hawthorne',3171,103); INSERT INTO PERSON VALUES (105,'Green','Gill','F','16/6/1966','124 Main St.','Young',2594,106); INSERT INTO PERSON VALUES (106,'Green','Graham','M','24/4/1967','124 Main St.','Young',2594,105); INSERT INTO PERSON VALUES (107,'Gray','Lesley','F','12/9/1972','130 Main St.','Young',2594,109); INSERT INTO PERSON VALUES (109,'Gray','John','M','14/4/1972','130 Main St.','Young',2594,107); INSERT INTO PERSON VALUES (110,'Samuelson','Thomas','M','1/1/1964','17 The Mews','Hawthorne',3171,NULL); INSERT INTO PERSON VALUES (111,'Abrahams','Mary','F','15/5/1967','2177A The Esplanade','Ivanhoe',3878,NULL); INSERT INTO PERSON VALUES (112,'Aumann','Monica','F','25/5/1955','29 The Esplanade','Ivanhoe',3878,NULL); INSERT INTO PERSON VALUES (113,'Brown','Melissa','F','8/8/1984','11 East St.','Cooma',2630,NULL); INSERT INTO PERSON VALUES (114,'Napier','Mary','F','1/1/1971','163 New Rd.','Henty',2658,NULL); INSERT INTO PERSON VALUES (115,'Nelson','Nigel','M','2/2/1972','165 Young Rd.','Temora',2666,NULL); INSERT INTO PERSON VALUES (116,'Newman','Olive','F','3/3/1973','21 Olympic Way','Henty',2658,NULL); INSERT INTO PERSON VALUES (117,'Gray','Lesley','M','31/12/1989','130 Andres St.','Young',2594,105); INSERT INTO PERSON VALUES (118,'Hon','Tasuku','M','13/3/1953','21 Silcon Height','Henty',2658,NULL); INSERT INTO PERSON VALUES (119,'Livingstone','Frank','M','3/3/2003','21 Sun Height','Henty',2658,122); INSERT INTO PERSON VALUES (120,'Giggle','Frank','M','23/3/1975','21 Albrige Close','Cooma',2630,121); INSERT INTO PERSON VALUES (121,'Giggle','Felicia','F','3/3/1980','21 Albrige Close','Cooma',2630,120); INSERT INTO PERSON VALUES (122,'Gigg','Frank Jr','M','13/3/2011','21 Stun Height','Henty',2658,123); INSERT INTO PERSON VALUES (123,'Gigg','Frances','F','12/12/2005','21 Stun Height','Henty',2658,122); INSERT INTO PERSON VALUES (124,'Smith','Buddy','M','11/12/1979','101 High St.','Cooma',2630,NULL); INSERT INTO PERSON VALUES (125,'Smith','Maxime','F','31/12/1979','101 High St.','Cooma',2630,124); INSERT INTO PERSON VALUES (126,'Smith','Issac','M','1/12/2007','101 High St.','Cooma',2630,124); INSERT INTO PERSON VALUES (127,'Smith','Ronny','M','3/12/2009','101 High St.','Cooma',2630,124); INSERT INTO PERSON VALUES (128,'Giggle','Fanny','F','3/12/2007','121 Close Rose','Hillo',2330,120); INSERT INTO PERSON VALUES (129,'Murad','Nadia','F','3/3/2000','121 Close Rose', 'Hillo',2330,130); INSERT INTO PERSON VALUES (130,'Murad','Tange','M','3/3/1999','7711 Albrige Close','Cooma',2630,NULL); INSERT INTO PERSON VALUES (131,'Rome','Paula','F','23/9/1965','21 Height Close','Cooma',2630,132); INSERT INTO PERSON VALUES (132,'Rome','Paul','M','13/3/1966','21 Height Close','Cooma',2630,NULL); INSERT INTO PERSON VALUES (133,'Rome','Fay','M','3/3/2017','21 Height Close','Cooma',2630,132); INSERT INTO PERSON VALUES (134,'Murad','Michelle','F','3/3/2001','1 Height Close','Cooma',2630,NULL); INSERT INTO PERSON VALUES (135,'Trump','Donald','M','13/3/1966','222 White House Avenue','Cooma',2630,NULL); INSERT INTO PERSON VALUES (136,'Trump','Melania','F','3/3/1992','222 White House Avenue','Cooma',2630,135); INSERT INTO PERSON VALUES (137,'Trump','Baron','M','3/3/2005','222 White House Avenue','Cooma',2630,135); INSERT INTO PERSON VALUES (138,'Johnson','Boris','M','23/9/1965','10 Downing Street','London',2999,NULL); INSERT INTO PERSON VALUES (139,'Cordeiro','Wayne','M','3/3/1965','777 Hawaii Close Rose', 'Hillo',7770, NULL); INSERT INTO PERSON VALUES (140,'Cordeiro','Wayne','M','3/3/1965','777 Hawaii Close Rose', 'Hillo',7770, NULL); Table Admission :- CREATE TABLE ADMISSION ( Admission_id NUMBER(3) PRIMARY KEY, Patient_id NUMBER(3), Admission_date DATE NOT NULL, Expected_op CHAR(3), Admitted_by NUMBER(3), Ward_code CHAR(3), Discharge_date DATE, FOREIGN KEY (Patient_id) references PERSON(Person_id), FOREIGN KEY (Expected_op) references OPERATION_TYPE(Op_code), FOREIGN KEY (Admitted_by) references PERSON(Person_id), FOREIGN KEY (Ward_code) references WARD(Ward_code) ); INSERT INTO ADMISSION VALUES (205,101,'2/2/2011','HB',114,'P','21/2/2011'); INSERT INTO ADMISSION VALUES (275,101,'1/9/2010','HY',115,'L','1/11/2010'); INSERT INTO ADMISSION VALUES (286,101,'3/5/2016','AR',116,'A','3/7/2016'); INSERT INTO ADMISSION VALUES (303,101,'3/4/2018','LA',125,'F', '13/5/2018' ); INSERT INTO ADMISSION VALUES (298,103,'23/1/2016','TS',114,'L','24/04/2016'); INSERT INTO ADMISSION VALUES (299,103,'23/3/2016','AP',114,'L','23/4/2016'); INSERT INTO ADMISSION VALUES (305,103,'23/4/2018','HT',125,'F','29/5/2018'); INSERT INTO ADMISSION VALUES (321,103,'13/5/2018','AR',125,'F', '23/5/2018' ); INSERT INTO ADMISSION VALUES (283,105,'3/12/2015','AR',116,'A','5/12/2015'); INSERT INTO ADMISSION VALUES (278,105,'1/1/2011','HB',115,'P','30/1/2011'); INSERT INTO ADMISSION VALUES (307,105,'3/4/2018','TS',125,'F', '13/5/2018'); INSERT INTO ADMISSION VALUES (276,106,'24/8/2010','LA',114,'P','15/9/2010'); INSERT INTO ADMISSION VALUES (287,106,'3/5/2016','AP',114,'A','3/6/2016'); INSERT INTO ADMISSION VALUES (274,109,'1/9/2010','AP',114,'P','9/9/2010'); INSERT INTO ADMISSION VALUES (288,109,'23/5/2016','LO',114,'F','3/07/2016'); INSERT INTO ADMISSION VALUES (301,112,'13/4/2018','AP',125,'F','16/4/2018'); INSERT INTO ADMISSION VALUES (304,112,'23/4/2019','LO',114,'L','23/5/2019'); INSERT INTO ADMISSION VALUES (279,113,'3/9/2010','TS',115,'F','10/9/2010'); INSERT INTO ADMISSION VALUES (284,113,'3/12/2015','HY',116,'A','03/01/2016'); INSERT INTO ADMISSION VALUES (285,113,'3/5/2016','HT',116,'A','3/6/2016'); INSERT INTO ADMISSION VALUES (300,113,'23/4/2018','AR',114,'L','25/6/2018'); INSERT INTO ADMISSION VALUES (306,113,'13/8/2018','AP',125,'L', '13/9/2018'); INSERT INTO ADMISSION VALUES (277,114,'20/9/2010','AP',115,'P','30/9/2010'); INSERT INTO ADMISSION VALUES (289,115,'11/4/2016','TS',114,'L','3/6/2016'); INSERT INTO ADMISSION VALUES (290,115,'5/5/2016','LO',114,'L','3/06/2016'); INSERT INTO ADMISSION VALUES (308,115,'23/3/2018','AR',114,'L','25/3/2018'); INSERT INTO ADMISSION VALUES (280,117,'3/9/2010','AP',115,'F','25/9/2010'); INSERT INTO ADMISSION VALUES (281,117,'3/9/2014','HB',116,'A','21/9/2014'); INSERT INTO ADMISSION VALUES (282,117,'3/12/2015','LA',116,'A','4/12/2015'); INSERT INTO ADMISSION VALUES (309,126,'23/3/2018','TS',125,'L', '13/5/2018'); INSERT INTO ADMISSION VALUES (310,127,'13/5/2018','AP',125,'L', '28/5/2018'); INSERT INTO ADMISSION VALUES (311,124,'3/5/2018','LO',125,'A', '23/5/2018' ); INSERT INTO ADMISSION VALUES (312,127,'21/6/2019','LO',125,'L','22/8/2019'); INSERT INTO ADMISSION VALUES (313,124,'22/6/2019','AP',125,'A','22/7/2019'); INSERT INTO ADMISSION VALUES (314,109,'21/6/2019','LO',125,'L','22/7/2019'); INSERT INTO ADMISSION VALUES (315,126,'12/6/2019','AP',125,'A','22/9/2019'); INSERT INTO ADMISSION VALUES (316,114,'22/7/2019','HB',125,'A','12/12/2019'); INSERT INTO ADMISSION VALUES (318,128,'3/5/2019','LA',116,'A','4/6/2019'); INSERT INTO ADMISSION VALUES (319,129,'23/3/2019','TS',125,'L', '13/4/2019'); INSERT INTO ADMISSION VALUES (320,130,'3/5/2019','LA',116,'F','4/6/2019'); INSERT INTO ADMISSION VALUES (328,119,'3/3/2019','TS',115,'N', '3/4/2019'); INSERT INTO ADMISSION VALUES (322,132,'3/5/2019','LA',116,'A','4/6/2019'); INSERT INTO ADMISSION VALUES (323,133,'23/3/2019','TS',125,'L', '13/4/2019'); INSERT INTO ADMISSION VALUES (324,131,'3/5/2018','LA',116,'F','4/5/2018'); INSERT INTO ADMISSION VALUES (325,118,'3/6/2019','TS',115,'P', '3/7/2019'); INSERT INTO ADMISSION VALUES (326,102,'3/5/2018','LA',116,'F','4/5/2018'); INSERT INTO ADMISSION VALUES (327,104,'3/6/2019','TS',115,'P', '3/7/2019'); INSERT INTO ADMISSION VALUES (339,107,'3/6/2019','TS',115,'P', '3/7/2019'); INSERT INTO ADMISSION VALUES (329,110,'3/6/2019','TS',115,'P', '3/7/2019'); INSERT INTO ADMISSION VALUES (330,111,'3/6/2019','TS',115,'P', '3/7/2019'); INSERT INTO ADMISSION VALUES (331,116,'3/6/2019','TS',115,'P', '3/7/2019'); INSERT INTO ADMISSION VALUES (332,121,'21/6/2019','TS',125,'L','22/7/2019'); INSERT INTO ADMISSION VALUES (333,123,'22/7/2019','AP',125,'A','22/9/2019'); INSERT INTO ADMISSION VALUES (334,134,'21/8/2019','AP',115,'L','22/12/2019'); INSERT INTO ADMISSION VALUES (335,128,'12/8/2019','AP',115,'A','22/10/2019'); INSERT INTO ADMISSION VALUES (336,125,'22/7/2018','HB',115,'A','22/8/2018'); INSERT INTO ADMISSION VALUES (337,120,'21/8/2019','AP',116,'L','22/10/2019'); INSERT INTO ADMISSION VALUES (338,130,'22/7/2019','AP',125,'N','22/8/2019'); INSERT INTO ADMISSION VALUES (340,131,'22/8/2019','AP',125,'N','22/10/2019'); INSERT INTO ADMISSION VALUES (341,122,'22/8/2019','AP',125,'N','22/11/2019'); INSERT INTO ADMISSION VALUES (404,112,'23/4/2020','CO',135,'C',NULL); INSERT INTO ADMISSION VALUES (400,113,'23/4/2020','CO',135,'C',NULL); INSERT INTO ADMISSION VALUES (406,115,'13/2/2020','CO',135,'C',NULL); INSERT INTO ADMISSION VALUES (432,121,'21/4/2020','CO',135,'C',NULL); INSERT INTO ADMISSION VALUES (433,123,'22/4/2020','CO',138,'C',NULL); INSERT INTO ADMISSION VALUES (434,134,'21/4/2020','CO',138,'C',NULL); INSERT INTO ADMISSION VALUES (435,128,'12/4/2020','CO',138,'C',NULL); INSERT INTO ADMISSION VALUES (412,127,'21/4/2020','CO',138,'C',NULL); INSERT INTO ADMISSION VALUES (414,109,'21/4/2020','CO',138,'C',NULL); INSERT INTO ADMISSION VALUES (415,126,'12/4/2020','CO',135,'C',NULL); INSERT INTO ADMISSION VALUES (416,114,'22/3/2020','CO',135,'C',NULL); INSERT INTO ADMISSION VALUES (437,120,'21/2/2020','CO',136,'C',NULL); INSERT INTO ADMISSION VALUES (438,130,'22/2/2020','CO',135,'C',NULL); INSERT INTO ADMISSION VALUES (440,131,'22/2/2020','CO',135,'C',NULL); INSERT INTO ADMISSION VALUES (441,122,'22/2/2020','CO',135,'C',NULL); INSERT INTO ADMISSION VALUES (479,118,'3/4/2020','CO',135,'C',NULL); INSERT INTO ADMISSION VALUES (484,111,'3/3/2020','CO',136,'C',NULL); INSERT INTO ADMISSION VALUES (485,116,'3/3/2020','CO',136,'C',NULL); INSERT INTO ADMISSION VALUES (489,119,'11/4/2020','CO',136,'C',NULL); INSERT INTO ADMISSION VALUES (490,139,'11/4/2020','AP',125,'F',NULL); INSERT INTO ADMISSION VALUES (491,140,'21/4/2020','AP',138,'F',NULL); I thought using a subquery would work SELECT DISTINCT p.First_name ||' '|| p.Surname AS "Full Name", CONCAT(CONCAT(CONCAT(p.Street,', '),p.Town), p.Postcode) AS "Address", TRUNC((SYSDATE-p.Birth_date)/365.25) AS Age FROM PERSON p JOIN ADMISSION a ON p.Person_id = a.Patient_id WHERE p.Person_id IN( SELECT Patient_id FROM ADMISSION GROUP BY Patient_id HAVING (COUNT(Patient_id)>=2) ) AND a.Admission_date>='1/1/2018'; but the error I'm coming across is that one of the patients "Barry Black" shows up in the output despite having only been admitted once since 2018. Output Table Would appreciate any help in this thanks.
This query provides you the result in expected format, select first_name ||' '|| surname as full_name, street ||' '|| town ||' '|| postcode as address, to_char(birth_date, 'YYYY') - to_char(sysdate, 'YYYY') as age from person; This query provides you the result in where patient admitted at least twice, select first_name ||' '|| surname as full_name, street ||' '|| town ||' '|| postcode as address, to_char(birth_date, 'YYYY') - to_char(sysdate, 'YYYY') as age from person where id in (select id from (select a.id, count(1) from person p inner join admission a on (p.persion_id = a.patient_id) group by a.id having count(1) >= 2));
If you want to remove the patient "Barry Black" on account of him being admitted only once since 2018 then we can apply a filter on the group by subquery in order to qualify only patients with multiple visits since 2018. Note that, by the same criteria, this disqualifies "Gill Green" patient id 105 also who is present in your output results, not just "Barry Black". alter session set NLS_DATE_FORMAT = 'DD/MM/YYYY'; SELECT DISTINCT p.First_name ||' '|| p.Surname AS "Full Name", CONCAT(CONCAT(CONCAT(p.Street,', '),p.Town), p.Postcode) AS "Address", TRUNC((SYSDATE-p.Birth_date)/365.25) AS Age FROM PERSON p JOIN ADMISSION a ON p.Person_id = a.Patient_id WHERE p.Person_id IN ( SELECT Patient_id FROM ADMISSION WHERE Admission_date>='1/1/2018' GROUP BY Patient_id HAVING (COUNT(Patient_id)>=2) ) AND a.Admission_date>='1/1/2018' order by 1,2,3 ;
Find the order number and the number of book titles, total number of books, and average book
For my CIS class, I have SQL project, I'm still very new to SQL and trying to learn it. Any help would be greatly appreciated. SQL FIddle Query: Based on #9 (below). Find the order number and the number of book titles, total number of books, and average book (unit) price for each order. Query #9: Write query to show order number and the number of books titles (you may use bno) in order. For example, in order 1020 there are 4 book titles. This is what I tried and giving me an error: select ono, count(orderline.bno), sum(quantity), round(avg(price) from orderline, book where orderline.bno=book.bno group by ono The error I'm getting is: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'from orderline, book where orderline.bno=book.bno group by ono' at line 2 My query #9: select ono, count(bno) from orderline group by ono Table: Table Database codes: create table zipcode ( zip integer(5) primary key, city varchar(30), State varchar(20)); create table employee ( eno varchar(10) primary key, ename varchar(30), zip integer(5) references zipcode(zip), hire_date date); create table book ( bno integer(5) primary key, bname varchar(30), qoh integer(5) not null, price dec(6,2) not null); create table customer ( cno integer(5) primary key, cname varchar(30), street varchar(30), zip integer(5) references zipcode(zip), phone char(12)); create table salesOrder ( ono integer(5) primary key, cno integer(5) references customer(cno), eno varchar(10) references employees(Eno), received date, shipped date ); create table orderLine ( ono integer(5) references salesOrder(ono), bno integer(5) references book(bno), quantity integer(10) not null, primary key (ono, bno)); insert into zipcode values (98225, 'Bellingham', 'WA'); insert into zipcode values (95388, 'Winton', 'CA'); insert into zipcode values (44242, 'Stow', 'OH'); insert into zipcode values (61536, 'Hanna city', 'IL'); insert into zipcode values (01254, 'Richmond', 'MA'); insert into zipcode values (95124, 'San Jose', 'CA'); insert into zipcode values (95382, 'Turlock', 'CA'); insert into zipcode values (95380, 'Turlock', 'CA'); insert into zipcode values (98102, 'Seattle', 'WA'); insert into employee values ('P0239401', 'Jones Hoffer',98225, '2000-12-12'); insert into employee values ('P0239402', 'Jeffrey Prescott',95388, '2016-11-07'); insert into employee values ('P0239403', 'Fred NcFaddeb',95124, '2008-09-01'); insert into employee values ('P0239404', 'Karen Ives',98102, '2014-05-21'); insert into book values (10501, 'Forensic Accounting',200, 229.99); insert into book values (10502, 'SAP Business One',159, 149.99); insert into book values (10503, 'Fraud Cases',190, 179.99); insert into book values (10504, 'CPA Review',65, 279.99); insert into book values (10605, 'Quickbooks for Business',322, 59.99); insert into book values (10704, 'Financial Accounting',129, 164.99); insert into book values (10879, 'Managerial Accounting',155, 114.99); insert into book values (10933, 'Cost Accounting',122, 219.99); insert into book values (10948, 'Intermediate Accounting',123, 164.99); insert into book values (10965, 'Accounting Information Systems',211, 259.99); insert into book values (10988, 'XBRL in Nutshell',124, 109.99); insert into customer values (23511, 'Michelle Kuan', '123 Main St.',98225, '360-636-5555'); insert into customer values (23512, 'George Myer', '237 Ash Ave.',95124, '312-678-5555'); insert into customer values (23513, 'Richard Gold', '111 Inwood St.',95124, '312-883-7337'); insert into customer values (23514, 'Robert Smith', '54 Gate Dr.',95388, '206-832-1221'); insert into customer values (23515, 'Christopher David', '777 Loto St.',98225, '360-458-9878'); insert into customer values (23516, 'Adam Beethoven', '234 Park Rd.',95380, '209-546-7299'); insert into customer values (23517, 'Ludwig Bach', '5790 Walnut St.',95382, '209-638-2712'); insert into customer values (23518, 'Kathleen Pedersen', '1233 Federal Ave E', 98102, '360-573-7239'); insert into salesOrder values (1020, 23511, 'P0239403', '2018-01-13', '2018-01-15'); insert into salesOrder values (1021, 23513, 'P0239401', '2018-01-13', '2018-01-16'); insert into salesOrder values (1022, 23513, 'P0239402', '2018-01-15', '2018-01-17'); insert into salesOrder values (1023, 23512, 'P0239403', '2018-01-16', '2018-01-18'); insert into salesOrder values (1024, 23511, 'P0239402', '2018-01-18', '2018-01-20'); insert into salesOrder values (1025, 23511, 'P0239403', '2018-01-29', '2017-01-31'); insert into salesOrder values (1026, 23512, 'P0239404', '2018-01-30', '2018-01-31'); insert into salesOrder values (1027, 23512, 'P0239402', '2018-01-30', '2018-01-31'); insert into salesOrder values (1028, 23512, 'P0239404', '2018-01-30', '2018-01-31'); insert into salesOrder (ONO, CNO, ENO, RECEIVED) values (1029, 23513, 'P0239402', '2018-01-31'); insert into salesOrder (ONO, CNO, ENO, RECEIVED) values (1030, 23511, 'P0239401', '2018-01-31'); insert into orderLine values (1020, 10501,7); insert into orderLine values (1020, 10502,15); insert into orderLine values (1020, 10504,3); insert into orderLine values (1020, 10503,6); insert into orderLine values (1021, 10605,4); insert into orderLine values (1022, 10605,2); insert into orderLine values (1022, 10704,4); insert into orderLine values (1023, 10879,4); insert into orderLine values (1023, 10988,19); insert into orderLine values (1024, 10502,7); insert into orderLine values (1024, 10988,2); insert into orderLine values (1025, 10502,4); insert into orderLine values (1025, 10988,3); insert into orderLine values (1025, 10948,2); insert into orderLine values (1026, 10965,15); insert into orderLine values (1026, 10933,5); insert into orderLine values (1027, 10933,21); insert into orderLine values (1028, 10933,9); insert into orderLine values (1028, 10965,11); insert into orderLine values (1029, 10933,4); insert into orderLine values (1029, 10965,10); insert into orderLine values (1029, 10988,3); insert into orderLine values (1030, 10965,6);
How to properly link 2 sql tables with a referential table
In SQL+, I've created 2 tables to store data and 1 to cross reference them. The goal is to be able to write a single query that will show me what car is in which dealership, and which dealership offers which car, etc. The problem is that when I query anything, each car is in each dealership. drop table car_dealer; drop table car; drop table dealer; create table dealer( did NUMBER PRIMARY KEY, dname VARCHAR2(20) ); create table car( cid NUMBER PRIMARY KEY, cname VARCHAR2(20) ); create table car_dealer( cid NUMBER REFERENCES car(cid), did NUMBER REFERENCES dealer(did), CONSTRAINT pk_car_dealer PRIMARY KEY (cid,did) ); INSERT INTO car VALUES (1,'Buick Skylark'); INSERT INTO car VALUES (2,'H2 Hummer'); INSERT INTO car VALUES (3,'Chevy Suburban'); INSERT INTO car VALUES (4,'BMW Z3'); INSERT INTO car VALUES (5,'BMW 328i'); INSERT INTO car VALUES (6,'Jeep Wrangler'); INSERT INTO car VALUES (7,'Ford Focus'); INSERT INTO car VALUES (8,'Range Rover'); INSERT INTO car VALUES (9,'Toyota Supra'); INSERT INTO car VALUES (10,'Ferrari Testarosa'); INSERT INTO car VALUES (11,'Jaguar X12'); INSERT INTO car VALUES (12,'Ford Fairmont'); INSERT INTO dealer VALUES (100,'Midnight Movers'); INSERT INTO dealer VALUES (200,'Bubbas Imports'); INSERT INTO dealer VALUES (300,'Import Motors'); INSERT INTO car_dealer (cid,did) VALUES (1,100); INSERT INTO car_dealer (cid,did) VALUES (2,100); INSERT INTO car_dealer (cid,did) VALUES (3,100); INSERT INTO car_dealer (cid,did) VALUES (4,100); INSERT INTO car_dealer (cid,did) VALUES (5,100); INSERT INTO car_dealer (cid,did) VALUES (6,100); INSERT INTO car_dealer (cid,did) VALUES (1,200); INSERT INTO car_dealer (cid,did) VALUES (2,200); INSERT INTO car_dealer (cid,did) VALUES (3,200); INSERT INTO car_dealer (cid,did) VALUES (4,200); INSERT INTO car_dealer (cid,did) VALUES (5,200); INSERT INTO car_dealer (cid,did) VALUES (6,200); INSERT INTO car_dealer (cid,did) VALUES (7,300); INSERT INTO car_dealer (cid,did) VALUES (8,300); INSERT INTO car_dealer (cid,did) VALUES (9,300); INSERT INTO car_dealer (cid,did) VALUES (10,300); INSERT INTO car_dealer (cid,did) VALUES (11,300); INSERT INTO car_dealer (cid,did) VALUES (12,300); select dname, cname from dealer,car where did = 200; select dname, cname from dealer,car where cid = 4; Midnight Movers(100) and Bubba's Imports(200) should both have cars 1-6 only, and Import Motors(300) should have cars 7-12 only. Instead all dealerships have all cars, and I would like some help figuring out why.
This is what happens when you don't specify the joins. To get proper results you should change your queries to the following: SELECT d.dname, c.cname FROM car_dealer cd JOIN car c ON cd.cid = c.cid JOIN dealer d ON cd.did = d.did WHERE cd.did = 200; SELECT d.dname, c.cname FROM car_dealer cd JOIN car c ON cd.cid = c.cid JOIN dealer d ON cd.did = d.did WHERE cd.cid = 4; Here SQL Fiddle http://sqlfiddle.com/#!4/98833/4