Skip to content

Instantly share code, notes, and snippets.

@ruchej
Created September 1, 2022 11:00
Show Gist options
  • Save ruchej/79a701b7b74da55636f40ce64ef4944e to your computer and use it in GitHub Desktop.
Save ruchej/79a701b7b74da55636f40ce64ef4944e to your computer and use it in GitHub Desktop.
CREATE OR REPLACE PROCEDURE CREATE_TABLE_TRANSFORMED_DATA()
LANGUAGE 'plpgsql'
AS $$
CREATE TABLE IF NOT EXISTS public.transformed_data
(
value_timestamp timestamp without time zone NOT NULL,
CONSTRAINT transformed_data_pkey PRIMARY KEY (value_timestamp)
);
DECLARE names ARRAY;
NAMES := (SELECT DISTINCT VALUE_NAME FROM DATA_FROM_KAFKA);
BEGIN
FOREACH NAME in ARRAY NAMES
LOOP
EXECUTE 'ALTER TABLE public.transformed_data ADD COLUMN %I double precision;'
USING format(%I, NAME);
END LOOP;
END;
$$;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment