Advertisement
Guest User

eeeeee

a guest
Nov 11th, 2019
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. -- CREATE OR REPLACE FUNCTION nome_funzione ( <tipo_dato_in> ) RETURNS <tipo_dato_out> AS $$
  2. -- DECLARE
  3.  
  4. -- BEGIN
  5.  
  6. -- END;
  7. -- $$ language 'plpgsql';
  8.  
  9. -- <tipo_dato_in> : tipo di dato della "colonna" che mi interessa come input
  10. -- <tipo_dato_out> : tipo di dato della "colonna" che mi interessa come output
  11.  
  12.  
  13.  
  14.  
  15. -- dato un film, restituisce il rating più alto (score) ricevuto
  16. -- input: titolo di un film
  17. -- output: valore dello score più alto ricevuto da quel film
  18.  
  19. CREATE OR REPLACE FUNCTION get_movie_top_rating ( varchar(200) ) RETURNS numeric AS $$
  20. DECLARE
  21. -- <nome_variabile> <tipo_dato>
  22. a_score rating.score%TYPE;
  23. BEGIN
  24. SELECT max(score) INTO a_score
  25. FROM movie JOIN rating ON movie.id = rating.movie
  26. WHERE trim(lower(official_title)) = trim(lower($1));
  27.  
  28. RETURN a_score;
  29. END;
  30. $$ language 'plpgsql';
  31.  
  32. SELECT * FROM get_movie_top_rating('Inception');
  33.  
  34. -- -- tutti i rating associati al film 'Inception'
  35. -- SELECT id FROM movie WHERE official_title = 'Inception'; -- >> 1375666
  36. -- SELECT score FROM rating WHERE movie = '1375666';
  37. -- -- dato che fa schifo scritto così, uso un bel join va'
  38. -- SELECT score
  39. -- FROM movie JOIN rating ON movie.id = rating.movie
  40. -- WHERE official_title = 'Inception';
  41.  
  42. insert into rating(check_date, source, movie, score) values('2019-11-11', 'Flavio', '1375666', 9.2);
  43. insert into rating(check_date, source, movie, score) values('2019-11-04', 'Azz', '1375666', 7.5);
  44.  
  45.  
  46.  
  47. -- 11/11/2019
  48.  
  49. -- dato un film, restituisce il rating più alto (score) ricevuto usando un cursore
  50. -- input: titolo di un film
  51. -- output: valore dello score più alto ricevuto da quel film
  52.  
  53. CREATE OR REPLACE FUNCTION get_movie_top_rating_cursor ( varchar(200) ) RETURNS numeric AS $$
  54. DECLARE
  55. -- <nome_variabile> <tipo_dato>
  56. a_score rating.score%TYPE;
  57. -- puntatore al risultato della query
  58. movie_ratings AS CURSOR FOR
  59. SELECT score
  60. FROM movie JOIN rating ON movie.id = rating.movie
  61. WHERE trim(lower(official_title)) = trim(lower($1));
  62.  
  63. BEGIN
  64.  
  65.  
  66. RETURN a_score;
  67. END;
  68. $$ language 'plpgsql';
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement