Skip to content
Snippets Groups Projects
Commit f2f3fe0e authored by Clemens Kupke's avatar Clemens Kupke
Browse files

lab5

parent 077d2445
No related branches found
No related tags found
No related merge requests found
...@@ -14,7 +14,7 @@ BEGIN ...@@ -14,7 +14,7 @@ BEGIN
LOOP LOOP
FETCH c_pop INTO popul; FETCH c_pop INTO popul;
EXIT WHEN NOT FOUND; EXIT WHEN NOT FOUND;
maxpop = GREATEST(maxpop,popul); maxpop = <ENTER>;
END LOOP; END LOOP;
CLOSE c_pop; CLOSE c_pop;
RETURN maxpop; RETURN maxpop;
......
ALTER TABLE TEAM
ADD Manager_name CHAR(20);
create table team (
TNO NUMERIC (2,0) CONSTRAINT PK_TEAM PRIMARY KEY,
TNA CHAR (20),
GRP CHAR (1)
);
create table game (
GNO NUMERIC CONSTRAINT PK_GAME PRIMARY KEY,
GAME_DATE DATE,
TEAM_1 NUMERIC (2,0) CONSTRAINT REF_GAME_1 REFERENCES TEAM(TNO),
TEAM_1_GOALS NUMERIC (2,0),
TEAM_2 NUMERIC (2,0) CONSTRAINT REF_GAME_2 REFERENCES TEAM(TNO),
TEAM_2_GOALS NUMERIC (2,0),
ROUND CHAR (7)
);
insert into game values(1,'09-Jun-06',11,4,5,2,'first');
insert into game values(2,'09-Jun-06',20,0,8,2,'first');
insert into game values(3,'10-Jun-06',9,1,19,0,'first');
insert into game values(4,'10-Jun-06',29,0,26,0,'first');
insert into game values(5,'10-Jun-06',2,2,15,1,'first');
insert into game values(6,'11-Jun-06',23,0,18,1,'first');
insert into game values(7,'11-Jun-06',17,3,13,1,'first');
insert into game values(8,'11-Jun-06',1,0,21,1,'first');
insert into game values(9,'12-Jun-06',3,3,16,1,'first');
insert into game values(10,'12-Jun-06',32,0,7,3,'first');
insert into game values(11,'12-Jun-06',14,2,12,0,'first');
insert into game values(12,'13-Jun-06',24,2,28,1,'first');
insert into game values(13,'13-Jun-06',10,0,27,0,'first');
insert into game values(14,'13-Jun-06',4,1,6,0,'first');
insert into game values(15,'14-Jun-06',25,4,31,0,'first');
insert into game values(16,'14-Jun-06',30,2,22,2,'first');
insert into game values(17,'14-Jun-06',11,1,20,0,'first');
insert into game values(18,'15-Jun-06',8,3,5,0,'first');
insert into game values(19,'15-Jun-06',9,2,29,0,'first');
insert into game values(20,'15-Jun-06',26,1,19,0,'first');
insert into game values(21,'16-Jun-06',2,6,23,0,'first');
insert into game values(22,'16-Jun-06',18,2,15,1,'first');
insert into game values(23,'16-Jun-06',17,2,1,1,'first');
insert into game values(24,'17-Jun-06',21,2,13,0,'first');
insert into game values(25,'17-Jun-06',7,0,12,2,'first');
insert into game values(26,'17-Jun-06',14,1,32,1,'first');
insert into game values(27,'18-Jun-06',16,0,6,0,'first');
insert into game values(28,'18-Jun-06',4,2,3,0,'first');
insert into game values(29,'18-Jun-06',10,1,24,1,'first');
insert into game values(30,'19-Jun-06',28,0,27,2,'first');
insert into game values(31,'19-Jun-06',22,0,31,4,'first');
insert into game values(32,'19-Jun-06',25,3,30,1,'first');
insert into game values(33,'20-Jun-06',8,0,11,3,'first');
insert into game values(34,'20-Jun-06',5,1,20,2,'first');
insert into game values(35,'20-Jun-06',26,2,9,2,'first');
insert into game values(36,'20-Jun-06',19,2,29,0,'first');
insert into game values(37,'21-Jun-06',21,2,17,1,'first');
insert into game values(38,'21-Jun-06',13,1,1,1,'first');
insert into game values(39,'21-Jun-06',15,3,23,2,'first');
insert into game values(40,'21-Jun-06',18,0,2,0,'first');
insert into game values(41,'22-Jun-06',7,0,14,2,'first');
insert into game values(42,'22-Jun-06',12,2,32,1,'first');
insert into game values(43,'22-Jun-06',16,1,4,4,'first');
insert into game values(44,'22-Jun-06',6,2,3,2,'first');
insert into game values(45,'23-Jun-06',22,0,25,1,'first');
insert into game values(46,'23-Jun-06',31,1,30,0,'first');
insert into game values(47,'23-Jun-06',28,0,10,2,'first');
insert into game values(48,'23-Jun-06',27,2,24,0,'first');
insert into game values(49,'24-Jun-06',11,2,26,0,'second');
insert into game values(50,'24-Jun-06',2,2,17,1,'second');
insert into game values(51,'25-Jun-06',9,1,8,0,'second');
insert into game values(52,'25-Jun-06',21,1,18,0,'second');
insert into game values(53,'26-Jun-06',14,1,3,0,'second');
insert into game values(54,'26-Jun-06',27,0,31,0,'second');
insert into game values(55,'27-Jun-06',4,3,12,0,'second');
insert into game values(56,'27-Jun-06',25,1,10,3,'second');
insert into game values(57,'30-Jun-06',11,1,2,1,'quarter');
insert into game values(58,'30-Jun-06',14,3,31,0,'quarter');
insert into game values(59,'01-Jul-06',9,0,21,0,'quarter');
insert into game values(60,'01-Jul-06',4,0,10,1,'quarter');
insert into game values(61,'04-Jul-06',11,0,14,2,'semi');
insert into game values(62,'05-Jul-06',21,0,10,1,'semi');
insert into game values(63,'08-Jul-06',11,3,21,1,'playoff');
insert into game values(64,'09-Jul-06',14,1,10,1,'final');
insert into game values(65,'23-Jun-06',14,1,4,5,'first');
create view t1_groupA
as
select t1.tna name1, game_date, team_1_goals, t2.tna as name2, team_2_goals
from team t1, team t2, game
where t1.tno = team_1 AND t2.tno = team_2
and t1.grp = 'A'
and round = 'first';
create index t1index on game( team_1 );
create index t2index on game (team_2);
select game.*
from game,team
where tno = team_2
and tna like 'Ang%'
and game_date = '16-JUN-06';
/* The first view created counts the number of games which have been
played. The count() function counts the number of records in each
group (ie the number of games each team has played) and returns it
in a column headed 'p' which is given as an alias after the function */
drop view final_tables;
drop view played;
drop view wv;
drop view lv;
drop view dv;
drop view gv;
drop view goals_scored;
create view played as
select tna, count(*) p
from team, game
where (tno = team_1
or tno = team_2)
and round = 'first'
group by tna;
/* The next three views hold the following counts:
wins (w)
draws (d)
looses (l)
In addition for wins and draws the number of points gained is calculated
using Postgres arithmetic operators. The results of these calculations
are stored in the following columns:
wpts - points from wins
dpts - points from draws
*/
create view wv as
select tna, count(*) w , count(*) * 3 wpts
from team, game
where ((tno = team_1
and team_1_goals > team_2_goals)
or (tno = team_2
and team_2_goals > team_1_goals))
and round = 'first'
group by tna;
create view dv as
select tna, count(*) d , count(*) * 1 dpts
from team, game
where ((tno = team_1
and team_1_goals = team_2_goals)
or (tno = team_2
and team_2_goals = team_1_goals))
and round = 'first'
group by tna;
create view lv as
select tna, count(*) l , count(*) * 0 lpts
from team, game
where ((tno = team_1
and team_1_goals < team_2_goals)
or (tno = team_2
and team_2_goals < team_1_goals))
and round = 'first'
group by tna;
/*
The next view uses the Postgres sum() function to calculate the
number of goals each team has scored. Records in the game table are
grouped by team name and the sum() function calculates the sum of goals
scored in each team's games as follows:
goals for - f
goals against - a
*/
create view goals_scored as
select tna,game_date ,team_1_goals f, team_2_goals a
from team, game
where tno = team_1 and round = 'first'
union
select tna,game_date ,team_2_goals f, team_1_goals a
from team, game
where tno = team_2 and round = 'first';
create view gv as
select tna, sum(f) f, sum(a) a from goals_scored group by tna;
/* The last view uses the preceeding nine views to create an overall
summary which represents the league table. The first part (ie the select
part) identifies the attribute names to display. Most of these attributes
have the Postgres function coalesce() applied to them. This function is used to convert
any null values to a numeric zero - the result being headed by the name
shown at the end of each of these lines. The last line in the select part
uses a similar approach and Postgres arithmetic operators to calculate the
total number of points.
The conditions in the where clause arrange that each of the views
is joined to the played view. The (+) at the end of each line ensures that
if a team had not for example scored any away wins
then its record would not be lost when performing the join. In other words, it
computes an OUTER JOIN.
*/
create view final_tables as
select
played.tna,
p,
coalesce(w, 0) w,
coalesce(d,0) d,
coalesce( l, 0) l,
coalesce(f,0) f,
coalesce(a,0) a,
( coalesce(wpts,0)+coalesce(dpts,0)) pts,
grp
from played FULL OUTER JOIN wv ON played.tna = wv.tna
FULL OUTER JOIN dv ON played.tna = dv.tna
FULL OUTER JOIN lv ON played.tna = lv.tna
FULL OUTER JOIN gv ON played.tna = gv.tna
FULL OUTER JOIN team ON played.tna = team.tna;
insert into team values(1,'Angola','D');
insert into team values(10,'France','G');
insert into team values(11,'Germany','B');
insert into team values(12,'Ghana','E');
insert into team values(13,'Iran','D');
insert into team values(14,'Italy','E');
insert into team values(15,'Ivory Coast','C');
insert into team values(16,'Japan','F');
insert into team values(17,'Mexico','D');
insert into team values(18,'Netherlands','C');
insert into team values(19,'Paraguay','B');
insert into team values(2,'Argentina','C');
insert into team values(20,'Poland','A');
insert into team values(21,'Portugal','D');
insert into team values(22,'Saudi Arabia','H');
insert into team values(23,'Serbia + Montenegro','C');
insert into team values(24,'South Korea','G');
insert into team values(25,'Spain','H');
insert into team values(26,'Sweden','B');
insert into team values(27,'Switzerland','G');
insert into team values(28,'Togo','G');
insert into team values(29,'Trinidad + Tobago','B');
insert into team values(3,'Australia','F');
insert into team values(30,'Tunisia','H');
insert into team values(31,'Ukraine','H');
insert into team values(32,'United States','E');
insert into team values(4,'Brazil','F');
insert into team values(5,'Costa Rica','A');
insert into team values(6,'Croatia','F');
insert into team values(7,'Czech Republic','E');
insert into team values(8,'Ecuador','A');
insert into team values(9,'England','B');
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment