Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. CREATE FUNCTION livre_emprunt_tranche (integer)
  2. RETURNS int AS $$
  3. DECLARE
  4. nbr_livre INTEGER;
  5. BEGIN
  6. SELECT COUNT(*) INTO nbr_livre from emprunt e WHERE e.id_livre = $1;
  7. RETURN nbr_livre;
  8. END;
  9. $$
  10. LANGUAGE plpgsql;
  11.  
  12.  
  13. CREATE FUNCTION age_tranche ()
  14. RETURNS TABLE (
  15. tranche_age varchar
  16. ) AS $$
  17. BEGIN
  18. RETURN QUERY SELECT trunc(date_part( 'year', age(date_naissance) ) / 10) || '0-' ||
  19. trunc(date_part( 'year', age(date_naissance) ) / 10)+1 || '0 ans' as tranche,
  20. count(id)
  21. from abonne
  22. group by tranche
  23. order by tranche ;
  24. END;
  25. $$
  26. LANGUAGE plpgsql;
  27.  
  28.  
  29.  
  30. CREATE FUNCTION liste_top5 ()
  31. RETURNS TABLE (
  32. tranche_age varchar,
  33. id_livre int,
  34. nb_emprunt int
  35. ) AS $$
  36. BEGIN
  37. RETURN QUERY SELECT age_tranche() ,emprunt.id_livre, livre_emprunt_tranche(emprunt.id_livre) as nb from emprunt join livre on livre.id = emprunt.id_livre group by emprunt.id_livre ORDER by nb DESC LIMIT 5;
  38. END;
  39. $$
  40. LANGUAGE plpgsql;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement