From 10c2c4cd953d4f37cd614a0e876f411e13d34ed0 Mon Sep 17 00:00:00 2001
From: Luca Ferrari <fluca1978@gmail.com>
Date: Mon, 14 Nov 2022 13:07:43 +0100
Subject: Task 1 PLPGSQL done

---
 challenge-191/luca-ferrari/postgresql/ch-1.sql | 33 ++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)
 create mode 100644 challenge-191/luca-ferrari/postgresql/ch-1.sql

(limited to 'challenge-191/luca-ferrari/postgresql/ch-1.sql')

diff --git a/challenge-191/luca-ferrari/postgresql/ch-1.sql b/challenge-191/luca-ferrari/postgresql/ch-1.sql
new file mode 100644
index 0000000000..a1378c4676
--- /dev/null
+++ b/challenge-191/luca-ferrari/postgresql/ch-1.sql
@@ -0,0 +1,33 @@
+-- Perl Weekly Challenge 191
+-- Task 1
+
+CREATE SCHEMA IF NOT EXISTS pwc191;
+
+CREATE OR REPLACE FUNCTION
+pwc191.task1_plpgsql( l int[] )
+RETURNS int
+AS $CODE$
+DECLARE
+	current_max int;
+	wrong int := 0;
+BEGIN
+	-- compute the max
+	SELECT max( v )
+	INTO   current_max
+	FROM unnest( l ) v;
+
+	SELECT count(*)
+	INTO   wrong
+	FROM unnest( l ) v
+	WHERE ( v * 2 ) > current_max
+	AND   v <> current_max;
+
+	IF wrong > 0 THEN
+	   RETURN -1;
+        ELSE
+          RETURN 1;
+        END IF;
+
+END
+$CODE$
+LANGUAGE plpgsql;
-- 
cgit 

