Created
March 15, 2022 07:35
-
-
Save mistificator/f0d9cec5624c5fb43142a33f8f496fd0 to your computer and use it in GitHub Desktop.
PL/pgSQL procedure that makes view of data with rows transponded to columns. In this code %1 is placeholder for table suffix and %2 is placeholder for object number.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DO $$ | |
DECLARE | |
objs INTEGER[]; | |
r RECORD; | |
i INTEGER; | |
subquery_str TEXT; | |
with_str TEXT; | |
from_str TEXT; | |
query_str TEXT; | |
where_str TEXT; | |
subqueries TEXT[]; | |
BEGIN | |
i := 0; | |
FOR r IN | |
WITH obj_subquery AS ( | |
WITH ptype_subquery AS ( | |
SELECT "ObjNum", "Num" AS "PNum", "Description" FROM object_cards_overview, system_types WHERE object_cards_overview."NodeType" = 2 AND object_cards_overview."SystemType" = system_types."ID" | |
) | |
SELECT "ParentID", ptype_subquery."ObjNum" FROM network_object_cards, ptype_subquery WHERE ptype_subquery."ObjNum" = network_object_cards."ObjNum" AND ptype_subquery."PNum" = network_object_cards."PNum" | |
) | |
SELECT DISTINCT obj_subquery."ObjNum" FROM network_cards, obj_subquery WHERE obj_subquery."ParentID" = network_cards."ID" AND network_cards."ObjNum" = '%2' ORDER BY obj_subquery."ObjNum" | |
LOOP | |
i := i + 1; | |
objs[i] := r."ObjNum"; | |
SELECT 'subquery_' || r."ObjNum"::TEXT || ' AS (SELECT "CmdID" AS "_CmdID_' || r."ObjNum"::TEXT || E'", string_agg("P", \', \') AS "' || r."ObjNum"::TEXT | |
|| E'" FROM results_with_subresults_%1 WHERE "ObjNum" = \'' || r."ObjNum"::TEXT || E'\' GROUP BY "CmdID" UNION SELECT \'{00000000-0000-0000-0000000000000000}\'::UUID, \'\'::TEXT)' INTO subquery_str; | |
-- RAISE NOTICE '%', subquery_str; | |
subqueries[i] := subquery_str; | |
END LOOP; | |
-- RAISE NOTICE '%', objs; | |
SELECT 'WITH ' || string_agg(subquery_lines, ', ') FROM (SELECT unnest(subqueries) AS subquery_lines) AS unnest_subquery INTO with_str; | |
-- RAISE NOTICE '%', with_str; | |
SELECT string_agg('subquery_' || subquery_lines || ' ', ', ') FROM (SELECT unnest(objs) AS subquery_lines) AS unnest_subquery INTO from_str; | |
-- RAISE NOTICE '%', from_str; | |
SELECT 'WHERE ' || string_agg('("_CmdID_' || subquery_lines || '" = "CmdID" OR ("_CmdID_' || subquery_lines || E'" = \'{00000000-0000-0000-0000000000000000}\'::UUID AND "CmdID" NOT IN (SELECT "_CmdID_' || subquery_lines || E'" FROM subquery_' || subquery_lines || E')))', ' AND ') FROM (SELECT unnest(objs) AS subquery_lines) AS unnest_subquery INTO where_str; | |
-- RAISE NOTICE '%', where_str; | |
SELECT 'CREATE OR REPLACE VIEW extended_commands_with_results_%1 AS ' || with_str || 'SELECT * FROM commands_%1, ' || from_str || ' ' || where_str INTO query_str; | |
-- RAISE NOTICE '%', query_str; | |
DROP VIEW IF EXISTS extended_commands_with_results; | |
DROP VIEW IF EXISTS extended_commands_with_results_%1; | |
EXECUTE query_str; | |
CREATE OR REPLACE VIEW extended_commands_with_results AS SELECT * FROM extended_commands_with_results_%1; | |
EXCEPTION WHEN OTHERS THEN ROLLBACK; | |
END $$; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment