COMP3311 Final Exam 20T3 The University of New South Wales
COMP3311 Database Systems
Final Exam 20T3
Database Systems
[Front Page] [Notes] [Database] [Course Website] [Cheat Sheets]
[Q1] [Q2] [Q3] [Q4] [Q5] [Q6] [Q7] [Q8] [Q9] [Q10] [Q11] [Q12]

Question 4 (8 marks)

Write a PLpgSQL function that reports the number of "short" and "long" songs by each group over their whole recording career. A "short" song is defined to be one whose length is strictly less that 3 minutes (180 seconds). A "long" song is defined to be one whose length is strictly greater than 6 minutes (360 seconds).

create type SongCounts as ( "group" text, nshort integer, nlong integer );

create or replace function q4() returns setof SongCounts ...

The attributes in SongCounts are as follows:

Note that the function has no parameters, so will always produce the same result, e.g.

music=# select * from q4() order by "group";
        group         | nshort | nlong 
----------------------+--------+-------
 After the Sea        |      4 |    14
 After the Sky        |      0 |     0
 All of the Floyd     |      3 |     5
 All of the Sea       |      2 |    15
 Black Eyed Boy       |      4 |    26
 Black Eyed Nails     |      6 |    30
 Black Mountains      |      3 |     7
 Black Vanilla        |      6 |    18
 Blue Floyd           |     10 |    18
 Blue Mountains       |      4 |    11
... etc etc etc ...

Warning: you may have a different collation setting in your PostgreSQL server to the one that produced the expected results. This affects the ordering of certain characters, such as space. If you get a message that your test failed with "Different order", then your solution is most likely correct.

Instructions:

End of Question