Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- CREATE OR REPLACE FUNCTION nome_funzione ( <tipo_dato_in> ) RETURNS <tipo_dato_out> AS $$
- -- DECLARE
- -- BEGIN
- -- END;
- -- $$ language 'plpgsql';
- -- <tipo_dato_in> : tipo di dato della "colonna" che mi interessa come input
- -- <tipo_dato_out> : tipo di dato della "colonna" che mi interessa come output
- -- dato un film, restituisce il rating più alto (score) ricevuto
- -- input: titolo di un film
- -- output: valore dello score più alto ricevuto da quel film
- CREATE OR REPLACE FUNCTION get_movie_top_rating ( varchar(200) ) RETURNS numeric AS $$
- DECLARE
- -- <nome_variabile> <tipo_dato>
- a_score rating.score%TYPE;
- BEGIN
- SELECT max(score) INTO a_score
- FROM movie JOIN rating ON movie.id = rating.movie
- WHERE trim(lower(official_title)) = trim(lower($1));
- RETURN a_score;
- END;
- $$ language 'plpgsql';
- SELECT * FROM get_movie_top_rating('Inception');
- -- -- tutti i rating associati al film 'Inception'
- -- SELECT id FROM movie WHERE official_title = 'Inception'; -- >> 1375666
- -- SELECT score FROM rating WHERE movie = '1375666';
- -- -- dato che fa schifo scritto così, uso un bel join va'
- -- SELECT score
- -- FROM movie JOIN rating ON movie.id = rating.movie
- -- WHERE official_title = 'Inception';
- insert into rating(check_date, source, movie, score) values('2019-11-11', 'Flavio', '1375666', 9.2);
- insert into rating(check_date, source, movie, score) values('2019-11-04', 'Azz', '1375666', 7.5);
- -- 11/11/2019
- -- dato un film, restituisce il rating più alto (score) ricevuto usando un cursore
- -- input: titolo di un film
- -- output: valore dello score più alto ricevuto da quel film
- CREATE OR REPLACE FUNCTION get_movie_top_rating_cursor ( varchar(200) ) RETURNS numeric AS $$
- DECLARE
- -- <nome_variabile> <tipo_dato>
- a_score rating.score%TYPE;
- -- puntatore al risultato della query
- movie_ratings AS CURSOR FOR
- SELECT score
- FROM movie JOIN rating ON movie.id = rating.movie
- WHERE trim(lower(official_title)) = trim(lower($1));
- BEGIN
- RETURN a_score;
- END;
- $$ language 'plpgsql';
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement