From 44a92ee6dd0ca577889cc1489e1237d5084a1efc Mon Sep 17 00:00:00 2001
From: Luca Ferrari <fluca1978@gmail.com>
Date: Thu, 4 Apr 2024 13:03:27 +0200
Subject: PWC 263

Task 1 Raku done
Task 2 Raku done

Task 1 PL/Perl done
Task 2 PL/Perl done

Task 1 PL/PgSQL done
Task 2 PL/PgSQL done

Task 1 PL/Java done
Task 2 PL/Java done

Task 1 Python done
Task 2 PYthon done
---
 challenge-263/luca-ferrari/plpgsql/ch-2.sql | 45 +++++++++++++++++++++++++++++
 1 file changed, 45 insertions(+)
 create mode 100644 challenge-263/luca-ferrari/plpgsql/ch-2.sql

(limited to 'challenge-263/luca-ferrari/plpgsql/ch-2.sql')

diff --git a/challenge-263/luca-ferrari/plpgsql/ch-2.sql b/challenge-263/luca-ferrari/plpgsql/ch-2.sql
new file mode 100644
index 0000000000..ed18e9b8e2
--- /dev/null
+++ b/challenge-263/luca-ferrari/plpgsql/ch-2.sql
@@ -0,0 +1,45 @@
+--
+-- Perl Weekly Challenge 263
+-- Task 2
+-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-263>
+--
+
+CREATE SCHEMA IF NOT EXISTS pwc263;
+
+CREATE OR REPLACE FUNCTION
+pwc263.task2_plpgsql( i int[], j int[] )
+RETURNS TABLE( item int, qty int )
+AS $CODE$
+DECLARE
+
+BEGIN
+
+	CREATE TEMPORARY TABLE IF NOT EXISTS q( item int, qty int );
+	TRUNCATE TABLE q;
+
+	FOR x IN 1 .. array_length( i, 1 ) LOOP
+	    IF mod( x, 2 ) = 0 THEN
+	       CONTINUE;
+	    END IF;
+	    INSERT INTO q( item, qty )
+	    VALUES ( i[ x ], i[ x + 1 ] );
+
+	END LOOP;
+
+	FOR x IN 1 .. array_length( j, 1 ) LOOP
+	     IF mod( x, 2 ) = 0 THEN
+	       CONTINUE;
+	    END IF;
+	    INSERT INTO q( item, qty )
+	    VALUES ( j[ x ], j[ x + 1 ] );
+
+	END LOOP;
+
+	RETURN QUERY
+	SELECT q.item, sum( q.qty )::int
+	FROM q
+	GROUP BY q.item;
+
+END
+$CODE$
+LANGUAGE plpgsql;
-- 
cgit 

