aboutsummaryrefslogtreecommitdiff
path: root/challenge-238/luca-ferrari/postgresql/ch-2.2.sql
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-238/luca-ferrari/postgresql/ch-2.2.sql')
-rw-r--r--challenge-238/luca-ferrari/postgresql/ch-2.2.sql53
1 files changed, 53 insertions, 0 deletions
diff --git a/challenge-238/luca-ferrari/postgresql/ch-2.2.sql b/challenge-238/luca-ferrari/postgresql/ch-2.2.sql
new file mode 100644
index 0000000000..d1456ab3ee
--- /dev/null
+++ b/challenge-238/luca-ferrari/postgresql/ch-2.2.sql
@@ -0,0 +1,53 @@
+CREATE OR REPLACE FUNCTION pwc238.reduce( n int )
+RETURNS int
+AS $CODE$
+DECLARE
+ current_value int;
+ step_counter int;
+ digit text;
+ multiplication int;
+
+BEGIN
+ current_value := n;
+ step_counter := 0;
+
+ WHILE current_value > 9 LOOP
+ multiplication := 1;
+ step_counter := step_counter + 1;
+
+ FOREACH digit IN ARRAY regexp_split_to_array( current_value::text, '' ) LOOP
+ multiplication := multiplication * digit::int;
+ END LOOP;
+
+ current_value := multiplication;
+ END LOOP;
+
+ RETURN step_counter;
+END
+$CODE$
+LANGUAGE plpgsql;
+
+
+
+--
+-- Function task2_plpgsql
+-- Schema pwc238
+--
+-- Description:
+--
+--
+-- Return Type: SETOF INT
+--
+CREATE OR REPLACE FUNCTION
+pwc238.task2_plpgsql( nums int[] )
+RETURNS SETOF INT
+AS $CODE$
+
+SELECT v
+FROM unnest( nums ) v
+ORDER BY pwc238.reduce( v ), v;
+
+$CODE$
+LANGUAGE sql
+VOLATILE
+;