I have build my database ofcourse etc but I have problem with the code.I try to solve that
"Names of marines(one time each one) that have for reservation red boat and their have tonnage bigger than 200." and I did that.
Select distinct m.name
From marina m join reservation r on (m.mid, r.mid)
Where r.bid = (select b.bid from boat b where b.color = "red")
I tryed use that https://senseful.github.io/text-table/ but I failed so I post here if someone can help edited.That is my database.
drop table if exists reservation;
drop table if exists marina;
drop table if exists boat;
drop table if exists sailor;
create table boat
(bid integer not null constraint c_bid primary key,
bname varchar(40),
color varchar(40)
constraint c_color check (color in ('Red','Blue','Light Green','Yellow')));
create table marina
(mid integer not null constraint m_key primary key,
name varchar(40) not null,
capacity integer);
create table sailor
(sid integer not null constraint c_sid primary key,
sname varchar(40),
rating integer
constraint c_rating check (rating between 1 and 10),
age real constraint
c_age check (age < 18 OR age = 18));
create table reservation
(sid integer not null constraint f_key1 references sailor(sid) on delete cascade,
bid integer not null constraint f_key2 references boat(bid) on delete restrict
constraint c_bid check (bid not in (999)),
mid integer constraint f_key3 references marina(mid) on delete set null,
r_date date not null constraint c_date check (r_date > '02/04/1998'),
constraint p_key primary key(sid,bid,r_date));
INSERT INTO sailor(sid,sname,rating,age) VALUES (2, 'John', 6, 17);
INSERT INTO sailor(sid,sname,rating,age) VALUES (11, 'Mary', 10, 18);
INSERT INTO sailor(sid,sname,rating,age) VALUES (12, 'TH', 7, 14);
INSERT INTO sailor(sid,sname,rating,age) VALUES (13, 'John', 9, 18);
INSERT INTO sailor(sid,sname,rating,age) VALUES (1, 'Christin', 10, 17);
INSERT INTO sailor(sid,sname,rating,age) VALUES (15, 'Thod', 10, 13);
INSERT INTO sailor(sid,sname,rating,age) VALUES (16, 'Leonid', 5, 13);
INSERT INTO sailor(sid,sname,age) VALUES (17,'Left',17);
INSERT INTO sailor(sid,sname,rating,age) VALUES (19,'Polu',1,16);
INSERT INTO sailor(sid,sname,rating,age) VALUES (27,'Marinin',8,15);
INSERT INTO sailor(sid,sname,rating,age) VALUES (37,'Cos',8,14);
INSERT INTO marina(mid,name,capacity) VALUES(33,'Porto',300);
INSERT INTO marina(mid,name,capacity) VALUES(5,'Calam',105);
INSERT INTO marina(mid,name,capacity) VALUES(1,'Plat',32);
INSERT INTO marina(mid,name,capacity) VALUES(7,'Pos',19);
INSERT INTO marina(mid,name,capacity) VALUES(2,'Our',105);
INSERT INTO boat(bid,bname,color) VALUES(88,'Sof','Blue');
INSERT INTO boat(bid,bname,color) VALUES(17,'Ag','Light Green');
INSERT INTO boat(bid,bname,color) VALUES(13,'Panag','Yellow');
INSERT INTO boat(bid,bname,color) VALUES(1,'Αg.N','Red');
INSERT INTO boat(bid,bname,color) VALUES(72,'Christin','Red');
INSERT INTO boat(bid,bname,color) VALUES(19,'Dil','Light Green');
INSERT INTO boat(bid,bname,color) VALUES(77,'Αg.G','Blue');
INSERT INTO reservation(sid,bid,mid,r_date) VALUES(2,88,7,'1999-02-17');
INSERT INTO reservation(sid,bid,mid,r_date) VALUES(12,17,2,'1998-05-17');
INSERT INTO reservation(sid,bid,mid,r_date) VALUES(11,17,2,'1999-01-17');
INSERT INTO reservation(sid,bid,mid,r_date) VALUES(13,13,7,'2003-01-13');
INSERT INTO reservation(sid,bid,mid,r_date) VALUES(11,13,33,'2000-05-05');
INSERT INTO reservation(sid,bid,mid,r_date) VALUES(1,1,33,'2000-05-05');
INSERT INTO reservation(sid,bid,mid,r_date) VALUES(1,13,33,'2000-05-06');
INSERT INTO reservation(sid,bid,mid,r_date) VALUES(1,17,33,'2000-05-07');
INSERT INTO reservation(sid,bid,mid,r_date) VALUES(1,19,33,'2000-05-08');
INSERT INTO reservation(sid,bid,mid,r_date) VALUES(1,72,33,'2000-05-09');
INSERT INTO reservation(sid,bid,mid,r_date) VALUES(1,88,33,'2000-05-10');
INSERT INTO reservation(sid,bid,mid,r_date) VALUES(1,77,2,'2000-08-10');
INSERT INTO reservation(sid,bid,mid,r_date) VALUES(19,13,33,'1999-10-12');
INSERT INTO reservation(sid,bid,mid,r_date) VALUES(27,88,7,'2000-06-11');
INSERT INTO reservation(sid,bid,mid,r_date) VALUES(37,72,2,'2001-04-27');
This can be solved with an EXISTS sub-query:
select m.*
from marina m
where m.capacity > 200
and exists (select *
from reservation r
join boat b on r.bid = b.bid
where mid = m.mid
and b.color = 'Red')
where m.capacity > 200 only selects marinas with a capacity that is bigger than 200 (this is how I understand the "have tonnage bigger than 200")
The (co-related) sub-query then checks for reservations for each marina that were made for a red boat (that's how I understand the "that have for reservation red boat" part).
Online example
Related
I need my SQL db to be able to hold more than one reservation for a customer.
However I get some duplicate pk errors.
Here is my SQL script: (I wanna be able to let one customer have more than 1 reserv.)
/*
db_name: Restaurant DB
update_timestamp: 6.10.2021 # 09:48
sign: The Actual Daniel
*/
DROP DATABASE IF EXISTS restaurantDB;
CREATE DATABASE restaurantDB;
USE restaurantDB;
CREATE TABLE `bill` (
`billID` int NOT NULL AUTO_INCREMENT,
`payAmount` varchar(255) NOT NULL,
PRIMARY KEY (`billID`)
);
INSERT INTO `bill` VALUES (1, '$258');
INSERT INTO `bill` VALUES (2, '$420');
INSERT INTO `bill` VALUES (3, '$114');
INSERT INTO `bill` VALUES (4, '$88');
INSERT INTO `bill` VALUES (5, '$137');
INSERT INTO `bill` VALUES (6, '$286');
INSERT INTO `bill` VALUES (7, '$97');
INSERT INTO `bill` VALUES (8, '$110');
INSERT INTO `bill` VALUES (9, '$358');
INSERT INTO `bill` VALUES (10, '$190');
CREATE TABLE `customer` (
`customerID` int NOT NULL AUTO_INCREMENT,
`firstName` varchar(255) NOT NULL,
`lastName` varchar(255) NOT NULL,
`phone` varchar(20) NOT NULL,
PRIMARY KEY (`customerID`)
);
INSERT INTO `customer` VALUES (1, 'Nero', 'Lee', '420420589');
INSERT INTO `customer` VALUES (2, 'Lucius', 'Malt', '578632221');
INSERT INTO `customer` VALUES (3, 'Aurelian', 'Keen', '639456773');
INSERT INTO `customer` VALUES (4, 'Jasmine', 'Wang', '227453891');
INSERT INTO `customer` VALUES (5, 'Sophie', 'Clinck', '905674321');
INSERT INTO `customer` VALUES (6, 'Augustus', 'Smith', '523764112');
INSERT INTO `customer` VALUES (7, 'Isabelle', 'Niaom', '786554330');
INSERT INTO `customer` VALUES (8, 'Alix', 'Black', '009886775');
INSERT INTO `customer` VALUES (9, 'Leia', 'Mudworth', '345667228');
INSERT INTO `customer` VALUES (10, 'Claudius', 'Reed', '427654221');
CREATE TABLE `meal` (
`mealID` int NOT NULL AUTO_INCREMENT,
`sides` varchar(255) NULL,
`main` varchar(255) NULL,
`beverage` varchar(255) NOT NULL,
PRIMARY KEY (`mealID`)
);
INSERT INTO `meal` VALUES (1, 'potatos', 'steak', 'wine');
INSERT INTO `meal` VALUES (2, 'ice', 'cube steak', 'spring water');
INSERT INTO `meal` VALUES (3, 'rice', 'cake', 'beer');
INSERT INTO `meal` VALUES (4, 'salad', 'pasta', 'wine');
INSERT INTO `meal` VALUES (5, 'radish', 'noodles', 'soju');
INSERT INTO `meal` VALUES (6, 'rice', 'rice', 'pumpkin juice');
INSERT INTO `meal` VALUES (7, 'butter, honey', 'steak', 'wine');
INSERT INTO `meal` VALUES (8, 'ice', 'salad', 'mineral water');
INSERT INTO `meal` VALUES (9, 'sweet potatos', 'pork', 'beer');
INSERT INTO `meal` VALUES (10, 'sourdough bread', 'fruit bowl', 'mineral water');
CREATE TABLE `order` (
`orderID` int NOT NULL,
`time` datetime NOT NULL,
PRIMARY KEY (`orderID`)
);
INSERT INTO `order` VALUES (1, '21-09-27 13:36:06');
INSERT INTO `order` VALUES (2, '21-02-14 17:18:03');
INSERT INTO `order` VALUES (3, '21-03-27 11:34:16');
INSERT INTO `order` VALUES (4, '21-07-14 15:58:32');
INSERT INTO `order` VALUES (5, '21-02-27 18:16:33');
INSERT INTO `order` VALUES (6, '21-01-14 19:28:12');
INSERT INTO `order` VALUES (7, '21-08-27 16:27:07');
INSERT INTO `order` VALUES (8, '21-06-14 11:21:09');
INSERT INTO `order` VALUES (9, '21-11-27 16:30:03');
INSERT INTO `order` VALUES (10, '21-03-14 17:22:11');
CREATE TABLE `reservation` (
`reservationID` int NOT NULL AUTO_INCREMENT,
`checkIn` datetime NOT NULL,
`checkOut` datetime NOT NULL,
`customerForeign` int NOT NULL,
PRIMARY KEY (`reservationID`),
FOREIGN KEY (`customerForeign`) REFERENCES customer(customerID)
);
INSERT INTO `reservation` VALUES (1, '21-09-27 13:24:06', '21-09-27 14:17:23', 1);
INSERT INTO `reservation` VALUES (2, '21-02-14 17:25:03', '21-02-14 19:22:14', 2);
INSERT INTO `reservation` VALUES (3, '21-03-27 11:34:16', '21-03-27 13:34:16', 3);
INSERT INTO `reservation` VALUES (4, '21-07-14 15:58:32', '21-07-14 16:58:32', 4);
INSERT INTO `reservation` VALUES (5, '21-02-27 18:16:33', '21-02-27 19:16:33', 5);
INSERT INTO `reservation` VALUES (6, '21-01-14 19:28:12', '21-01-14 20:28:12', 6);
INSERT INTO `reservation` VALUES (7, '21-08-27 16:27:07', '21-08-27 17:27:07', 7);
INSERT INTO `reservation` VALUES (8, '21-06-14 11:21:09', '21-06-14 14:21:09', 8);
INSERT INTO `reservation` VALUES (9, '21-11-27 16:30:03', '21-11-27 17:30:03', 9);
INSERT INTO `reservation` VALUES (10, '21-03-14 17:22:11', '21-03-14 19:22:11', 10);
CREATE TABLE `table` (
`tableID` int NOT NULL,
`numOfSeats` int NOT NULL,
PRIMARY KEY (`tableID`)
);
INSERT INTO `table` VALUES (1, 1);
INSERT INTO `table` VALUES (2, 2);
INSERT INTO `table` VALUES (3, 2);
INSERT INTO `table` VALUES (4, 2);
INSERT INTO `table` VALUES (5, 4);
INSERT INTO `table` VALUES (6, 4);
INSERT INTO `table` VALUES (7, 6);
INSERT INTO `table` VALUES (8, 6);
INSERT INTO `table` VALUES (9, 8);
INSERT INTO `table` VALUES (10, 12);
ALTER TABLE `bill` ADD CONSTRAINT `fk_bill_order_1` FOREIGN KEY (`billID`) REFERENCES `order` (`orderID`);
ALTER TABLE `meal` ADD CONSTRAINT `fk_meal_order_1` FOREIGN KEY (`mealID`) REFERENCES `order` (`orderID`);
ALTER TABLE `order` ADD CONSTRAINT `fk_order_customer_1` FOREIGN KEY (`orderID`) REFERENCES `customer` (`customerID`);
ALTER TABLE `reservation` ADD CONSTRAINT `fk_reservation_customer_1` FOREIGN KEY (`reservationID`) REFERENCES `customer` (`customerID`);
ALTER TABLE `table` ADD CONSTRAINT `fk_table_reservation_1` FOREIGN KEY (`tableID`) REFERENCES `reservation` (`reservationID`);
This is error I get when trying to insert new reservation for customer with ID of 1
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
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.
I have four tables, one of them named matches and another one named teams. I need to display the teamCode in teams with the number of point they have that we can calculate in matches. Each win is 2 points each draw one point and each lost zero.
CREATE TABLE divisions
(
codediv CHAR(1) NOT NULL,
nomdiv VARCHAR2(40)
);
ALTER TABLE divisions
ADD CONSTRAINT divisions_pk PRIMARY KEY (codediv);
CREATE TABLE teams
(
teamCode CHAR(3) NOT NULL,
teamName VARCHAR2(50),
codediv CHAR(1) NOT NULL,
ville VARCHAR2(40),
nbcoupes NUMBER
);
ALTER TABLE teams ADD CHECK (nbcoupes >= 0);
ALTER TABLE teams ADD CONSTRAINT teams_pk PRIMARY KEY (teamCode);
CREATE TABLE joueurs
(
numjoueur NUMBER(3) NOT NULL,
nom VARCHAR2(30),
prenom VARCHAR2(30),
codeequipe CHAR(3)
);
ALTER TABLE joueurs ADD CONSTRAINT joueurs_pk PRIMARY KEY (numjoueur);
CREATE TABLE matchs
(
MatchNumber NUMBER(4) NOT NULL,
datematch DATE,
codeVisitingTeam CHAR(3) NOT NULL,
codeReceivingTeam CHAR(3) NOT NULL,
scoreVisitingTeam NUMBER(2),
scoreReceivingTeam NUMBER(2)
);
ALTER TABLE matchs ADD CONSTRAINT matchs_pk PRIMARY KEY (MatchNumber);
CREATE TABLE statistiques
(
nummatch NUMBER(4) NOT NULL,
numjoueur NUMBER(3) NOT NULL,
nbbuts NUMBER(3),
nbpasse NUMBER(3)
);
ALTER TABLE statistiques
ADD CONSTRAINT statistiques_pk PRIMARY KEY (numjoueur, nummatch);
ALTER TABLE matchs
ADD CONSTRAINT codeVisitingTeam FOREIGN KEY ( codeVisitingTeam)
REFERENCES teams ( teamCode );
ALTER TABLE teams
ADD CONSTRAINT teams_divisions_fk FOREIGN KEY ( codediv )
REFERENCES divisions ( codediv );
ALTER TABLE joueurs
ADD CONSTRAINT joueurs_equipes_fk FOREIGN KEY ( teamCode )
REFERENCES equipes ( teamCode );
ALTER TABLE matchs
ADD CONSTRAINT matchs_equipes_fk FOREIGN KEY ( CodeReceivingTeam )
REFERENCES equipes ( teamCode );
ALTER TABLE statistiques
ADD CONSTRAINT statistiques_joueurs_fk FOREIGN KEY ( numjoueur )
REFERENCES joueurs ( numjoueur );
ALTER TABLE statistiques
ADD CONSTRAINT statistiques_matchs_fk FOREIGN KEY ( nummatch )
REFERENCES matchs ( nummatch );
insert into divisions values('O', 'OUEST');
insert into divisions values('E', 'EST');
insert into equipes values('MTL', 'LES CANADIENS DE MONTRÉAl', 'E', 'MONTRÉAl', 24);
insert into equipes values('TOR', 'LES MAPLE LEAFS', 'E', 'TORONTO', 22);
insert into equipes values('OTT', 'LES SÉNATEURS', 'E', 'OTTAWA', 4);
insert into equipes values('AVL', 'LES AVALANCHES', 'O', 'COLORADO', 2);
insert into equipes values('VAN', 'LES CANUKS', 'O', 'VANCOUVER', 1);
insert into equipes values('BRU', 'LES BRUNS DE BOSTON', 'E', 'BOSTON', 13);
insert into Joueurs values(1, 'PRICE', 'CAREY', 'MTL');
insert into Joueurs values(2, 'MARKOV', 'ANDRÉ', 'MTL');
insert into Joueurs values(3, 'SUBBAN', 'KARL', 'MTL');
insert into Joueurs values(4, 'PATIORETTY', 'MAX', 'MTL');
insert into Joueurs values(10, 'HAMMOND', 'ANDREW', 'OTT');
insert into Joueurs values(6, 'STONE', 'MARC', 'OTT');
insert into Joueurs values(9, 'TURIS', 'KYLE', 'OTT');
insert into Joueurs values(7, 'GALLAGHER', 'BRANDON', 'MTL');
insert into Joueurs values(8, 'TANGUAY', 'ALEX', 'AVL');
insert into Joueurs values(11, 'THOMAS', 'BIL', 'AVL');
insert into Joueurs values(5, 'PATOCHE', 'ALAIN', NULL);
insert into Joueurs values(12, 'POIRIER', 'JUTEUX', NULL);
insert into Matchs values(100, TO_DATE('18-10-30', 'YY/MM/DD'), 'MTL', 'TOR', 3 , 4);
insert into Matchs values(101, TO_DATE('18-11-10', 'YY/MM/DD'), 'TOR', 'MTL', 3 , 3);
insert into Matchs values(102, TO_DATE('18-10-12', 'YY/MM/DD'), 'MTL', 'OTT', 2 , 0);
insert into Matchs values(103, TO_DATE('18-10-20', 'YY/MM/DD'), 'OTT', 'MTL', 0 , 1);
insert into Matchs values(104, TO_DATE('18-11-30', 'YY/MM/DD'), 'MTL', 'AVL', 3 , 4);
insert into Matchs values(105, TO_DATE('18-11-10', 'YY/MM/DD'), 'AVL', 'MTL', 0 , 0);
insert into Matchs values(106, TO_DATE('18-12-12', 'YY/MM/DD'), 'MTL', 'VAN', 2 , 0);
insert into Matchs values(107, TO_DATE('18-03-17', 'YY/MM/DD'), 'VAN', 'MTL', 3 , 1);
insert into Matchs values(108, TO_DATE('18-11-30', 'YY/MM/DD'), 'OTT', 'VAN', 0 , 0);
insert into Matchs values(109, TO_DATE('18-11-10', 'YY/MM/DD'), 'OTT', 'TOR', 0 , 4);
insert into Matchs values(114, TO_DATE('18-10-30', 'YY/MM/DD'), 'BRU', 'TOR', 3 , 4);
insert into Matchs values(115, TO_DATE('19-02-15', 'YY/MM/DD'), 'AVL', 'TOR', null , null);
insert into Matchs values(120, TO_DATE('18-02-26', 'YY/MM/DD'), 'MTL', 'AVL', null , null);
insert into Matchs values(121, TO_DATE('19-02-20', 'YY/MM/DD'), 'MTL', 'OTT', null , null);
Insert into statistiques values(100,3,2,2);
Insert into statistiques values(100,7,1,1);
Insert into statistiques values(101,3,1,0);
Insert into statistiques values(101,7,0,1);
Insert into statistiques values(101,4,1,2);
Insert into statistiques values(101,2,1,2);
Insert into statistiques values(100,4,0,2);
Insert into statistiques values(102,3,1,1);
Insert into statistiques values(102,7,1,2);
Insert into statistiques values(102,9,0,1);
Insert into statistiques values(106,4,1,1);
Insert into statistiques values(106,3,0,2);
Insert into statistiques values(106,2,1,0);
Insert into statistiques values(100,1,null,null);
Insert into statistiques values(101,1,null,null);
Insert into statistiques values(103,1,null,null);
Insert into statistiques values(102,1,null,null);
I've started with this to get the winning teams
select *
from
(select v.teamcode, count(m.codeVisitingTeam) as WonMatches
from matchs m
join teams v on v.teamCode = m.codeVisitingTeam
where m.scoreVisitingTeam > m.scoreReceivingTeam
group by v.teamCode
union all
select r.codeequipe, count(m.codeequiper) as WonMatches
from matchs m
join teams r on r.teamCode = m.codeReceivingTeam
where m.scoreReceivingTeam > m.scoreVisitingTeam
group by r.teamCode));
The only problem with the code above is that it returns twice the team 'MTL' as so :
VAN 1
MTL 2
AVL 1
TOR 3
MTL 1
So I was having a bit of fun with your query and did a bit more than you asked. I wrote the following query to calculated the full complement of wins, losses, and ties. I then ordered the results by points (3 points for a win and 1 for a draw). I hope this helps you a bit:
SELECT *
FROM (SELECT TeamCode,
CASE
WHEN (team = 'VISTING' AND scoreVisitingTeam > SCORERECEIVINGTEAM) OR (team = 'RECEIVING' AND SCORERECEIVINGTEAM > SCOREVISITINGTEAM) THEN 'WIN'
WHEN (team = 'VISTING' AND scoreVisitingTeam < SCORERECEIVINGTEAM) OR (team = 'RECEIVING' AND SCORERECEIVINGTEAM < SCOREVISITINGTEAM) THEN 'LOSS'
ELSE 'DRAW'
END AS RESULT
FROM matchs
UNPIVOT (TeamCode FOR Team IN (CodeVisitingTeam AS 'VISTING', CodeReceivingTeam AS 'RECEIVING')))
PIVOT(COUNT(*) FOR RESULT IN ('WIN' AS WINS, 'LOSS' AS LOSSES, 'DRAW' AS DRAWS))
ORDER BY (wins*3)+draws DESC;
If you have questions, please let me know.
I also created a SQLFiddle. (Link) Note: I removed your constraints from the SQLFiddle. They were throwing errors and I didn't feel like debugging it.
You can do it in a single query with conditional aggregation:
select e.teamCode,
sum(
case
when (e.teamCode = m.codeReceivingTeam and m.scoreReceivingTeam > m.scoreVisitingTeam)
or (e.teamCode = m.codeVisitingTeam and m.scoreReceivingTeam < m.scoreVisitingTeam) then 2
when m.scoreReceivingTeam = m.scoreVisitingTeam then 1
else 0
end
) total_points
from equipes e inner join matchs m
on e.teamCode in (m.codeReceivingTeam, m.codeVisitingTeam)
group by e.teamCode
I am trying to show amount paid for each tutor sorted by month and then by tutor id. I have the first part correct and can sort by month but cannot sort by tutor id because it is from a different table.
Here is the script for my tables:
create table match_history
(match_id number(3),
tutor_id number(3),
student_id number(4),
start_date date,
end_date date,
constraint pk_match_history primary key (match_id),
constraint fk1_match_history foreign key (tutor_id) references tutor(tutor_id),
constraint fk2_match_history foreign key (student_id) references student(student_id));
create table tutor_report
(match_id number(3),
month date,
hours number(3),
lessons number(3),
constraint pk_tutor_report primary key (match_id, month),
constraint fk1_tutor_report foreign key (match_id) references match_history(match_id));
insert into tutor values (100, '05-JAN-2017', 'Active');
insert into tutor values (101, '05-JAN-2017', 'Temp Stop');
insert into tutor values (102, '05-JAN-2017', 'Dropped');
insert into tutor values (103, '22-MAY-2017', 'Active');
insert into tutor values (104, '22-MAY-2017', 'Active');
insert into tutor values (105, '22-MAY-2017', 'Temp Stop');
insert into tutor values (106, '22-MAY-2017', 'Active');
insert into student values (3000, 2.3);
insert into student values (3001, 5.6);
insert into student values (3002, 1.3);
insert into student values (3003, 3.3);
insert into student values (3004, 2.7);
insert into student values (3005, 4.8);
insert into student values (3006, 7.8);
insert into student values (3007, 1.5);
insert into match_history values (1, 100, 3000, '10-JAN-2017', null);
insert into match_history values (2, 101, 3001, '15-JAN-2017', '15-MAY-2017');
insert into match_history values (3, 102, 3002, '10-FEB-2017', '01-MAR-2017');
insert into match_history values (4, 106, 3003, '28-MAY-2017', null);
insert into match_history values (5, 103, 3004, '01-JUN-2017', '15-JUN-2017');
insert into match_history values (6, 104, 3005, '01-JUN-2017', '28-JUN-2017');
insert into match_history values (7, 104, 3006, '01-JUN-2017', null);
insert into tutor_report values (1, '01-JUN-2017', 8, 4);
insert into tutor_report values (4, '01-JUN-2017', 8, 6);
insert into tutor_report values (5, '01-JUN-2017', 4, 4);
insert into tutor_report values (4, '01-JUL-2017', 10, 5);
insert into tutor_report values (1, '01-JUL-2017', 4, 2);
This is what I have so far:
Select (hours * 10) as amount paid from tutor_report group by month, tutor_id
however obviously I cannot just say tutor_id at the end.
You can join match_history to get the tutor_id.
But your statement and the query don't match. If you want to sort use ORDER BY.
SELECT tr.hours * 10 amount_paid
FROM tutor_report tr
INNER JOIN match_history mh
ON mh.match_id = tr.match_id
ORDER BY tr.month,
mh.tutor_id;
If you want to aggregate, hours needs to be argument to some aggregation function. Maybe you're after the sum of hours?
SELECT sum(tr.hours) * 10 amount_paid
FROM tutor_report tr
INNER JOIN match_history mh
ON mh.match_id = tr.match_id
GROUP BY tr.month,
mh.tutor_id;
If you are grouping based on columns on two tables,you need to join them on the matching Id and then use group by
Select (hours * 10) as amount paid
from tutor_report a
join match_history b on a. match_id = b.match_id
group by month, tutor_id