|
| 1 | +set check_function_bodies = off; |
| 2 | + |
| 3 | +DROP FUNCTION IF EXISTS public.get_total_cadenas ( |
| 4 | + highline_ids UUID[], |
| 5 | + page_number INTEGER, |
| 6 | + page_size INTEGER |
| 7 | +); |
| 8 | + |
| 9 | +CREATE OR REPLACE FUNCTION public.get_total_cadenas(highline_ids uuid[], page_number integer, page_size integer, start_date timestamp with time zone DEFAULT NULL::timestamp with time zone, end_date timestamp with time zone DEFAULT NULL::timestamp with time zone) |
| 10 | + RETURNS TABLE(instagram text, total_cadenas integer, profile_picture text) |
| 11 | + LANGUAGE plpgsql |
| 12 | +AS $function$ |
| 13 | +BEGIN |
| 14 | +RETURN QUERY |
| 15 | +SELECT e.instagram, SUM(e.cadenas) AS total_cadenas, COALESCE(p.profile_picture, '') AS profile_picture |
| 16 | +FROM public.entry e |
| 17 | +LEFT JOIN public.profiles p ON e.instagram = p.username |
| 18 | +WHERE e.highline_id = ANY(get_total_cadenas.highline_ids) |
| 19 | +AND (e.created_at >= COALESCE(start_date, '1970-01-01'::timestamp) OR start_date IS NULL) |
| 20 | +AND (e.created_at <= COALESCE(end_date, now()) OR end_date IS NULL) |
| 21 | +GROUP BY e.instagram, p.profile_picture |
| 22 | +HAVING SUM(e.cadenas) > 0 |
| 23 | +ORDER BY total_cadenas DESC |
| 24 | +OFFSET (get_total_cadenas.page_number - 1) * get_total_cadenas.page_size |
| 25 | +LIMIT get_total_cadenas.page_size; |
| 26 | +END; |
| 27 | +$function$ |
| 28 | +; |
| 29 | + |
| 30 | +DROP FUNCTION IF EXISTS public.get_total_full_lines ( |
| 31 | + highline_ids UUID[], |
| 32 | + page_number INTEGER, |
| 33 | + page_size INTEGER |
| 34 | +); |
| 35 | + |
| 36 | +CREATE OR REPLACE FUNCTION public.get_total_full_lines(highline_ids uuid[], page_number integer, page_size integer, start_date timestamp with time zone DEFAULT NULL::timestamp with time zone, end_date timestamp with time zone DEFAULT NULL::timestamp with time zone) |
| 37 | + RETURNS TABLE(instagram text, total_full_lines integer, profile_picture text) |
| 38 | + LANGUAGE plpgsql |
| 39 | +AS $function$ |
| 40 | +BEGIN |
| 41 | +RETURN QUERY |
| 42 | +SELECT e.instagram, SUM(e.full_lines) AS total_full_lines, COALESCE(p.profile_picture, '') AS profile_picture |
| 43 | +FROM public.entry e |
| 44 | +LEFT JOIN public.profiles p ON e.instagram = p.username |
| 45 | +WHERE e.highline_id = ANY(get_total_full_lines.highline_ids) |
| 46 | +AND (e.created_at >= COALESCE(start_date, '1970-01-01'::timestamp) OR start_date IS NULL) |
| 47 | +AND (e.created_at <= COALESCE(end_date, now()) OR end_date IS NULL) |
| 48 | +GROUP BY e.instagram, p.profile_picture |
| 49 | +HAVING SUM(e.full_lines) > 0 |
| 50 | +ORDER BY total_full_lines DESC |
| 51 | +OFFSET (get_total_full_lines.page_number - 1) * get_total_full_lines.page_size |
| 52 | +LIMIT get_total_full_lines.page_size; |
| 53 | +END; |
| 54 | +$function$ |
| 55 | +; |
| 56 | + |
| 57 | +DROP FUNCTION IF EXISTS public.get_total_walked ( |
| 58 | + highline_ids UUID[], |
| 59 | + page_number INTEGER, |
| 60 | + page_size INTEGER |
| 61 | +); |
| 62 | + |
| 63 | +CREATE OR REPLACE FUNCTION public.get_total_walked(highline_ids uuid[], page_number integer, page_size integer, start_date timestamp with time zone DEFAULT NULL::timestamp with time zone, end_date timestamp with time zone DEFAULT NULL::timestamp with time zone) |
| 64 | + RETURNS TABLE(instagram text, total_distance_walked integer, profile_picture text) |
| 65 | + LANGUAGE plpgsql |
| 66 | +AS $function$ |
| 67 | +BEGIN |
| 68 | +RETURN QUERY |
| 69 | +SELECT e.instagram, SUM(e.distance_walked) AS total_distance_walked, COALESCE(p.profile_picture, '') AS profile_picture |
| 70 | +FROM public.entry e |
| 71 | +LEFT JOIN public.profiles p ON e.instagram = p.username |
| 72 | +WHERE e.highline_id = ANY(get_total_walked.highline_ids) |
| 73 | +AND (e.created_at >= COALESCE(start_date, '1970-01-01'::timestamp) OR start_date IS NULL) |
| 74 | +AND (e.created_at <= COALESCE(end_date, now()) OR end_date IS NULL) |
| 75 | +AND e.distance_walked IS NOT NULL |
| 76 | +GROUP BY e.instagram, p.profile_picture |
| 77 | +ORDER BY total_distance_walked DESC |
| 78 | +OFFSET (get_total_walked.page_number - 1) * get_total_walked.page_size |
| 79 | +LIMIT page_size; |
| 80 | +END; |
| 81 | +$function$ |
| 82 | +; |
| 83 | + |
| 84 | + |
0 commit comments