aboutsummaryrefslogtreecommitdiff
path: root/challenge-246/luca-ferrari/postgresql/ch-1.sql
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-246/luca-ferrari/postgresql/ch-1.sql')
-rw-r--r--challenge-246/luca-ferrari/postgresql/ch-1.sql42
1 files changed, 42 insertions, 0 deletions
diff --git a/challenge-246/luca-ferrari/postgresql/ch-1.sql b/challenge-246/luca-ferrari/postgresql/ch-1.sql
new file mode 100644
index 0000000000..18b201248f
--- /dev/null
+++ b/challenge-246/luca-ferrari/postgresql/ch-1.sql
@@ -0,0 +1,42 @@
+--
+-- Perl Weekly Challenge 246
+-- Task 1
+--
+-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-246/>
+--
+
+CREATE SCHEMA IF NOT EXISTS pwc246;
+
+CREATE OR REPLACE FUNCTION
+pwc246.task1_plpgsql()
+RETURNS SETOF INT
+AS $CODE$
+DECLARE
+ counting int;
+ current_value int;
+BEGIN
+ CREATE TEMPORARY TABLE IF NOT EXISTS lottery( v int, CHECK( v <= 49 ), PRIMARY KEY( v ) );
+ TRUNCATE lottery;
+
+ counting := 0;
+ WHILE counting < 6 LOOP
+ current_value := ( random() * 100 )::int;
+
+ IF current_value <= 49 THEN
+ INSERT INTO lottery( v )
+ VALUES( current_value )
+ ON CONFLICT ( v ) DO NOTHING;
+ END IF;
+
+
+ SELECT count(*)
+ INTO counting
+ FROM lottery;
+
+ END LOOP;
+
+ RETURN QUERY
+ SELECT * FROM lottery;
+END
+$CODE$
+LANGUAGE plpgsql;