--
You received this message because you are subscribed to the Google Groups "firebird-support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebird-suppo...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/firebird-support/cb813429-c126-4e27-9d05-7943ccf9eb51n%40googlegroups.com.
CREATE GLOBAL TEMPORARY TABLE GTT_SOURCE (
ID INTEGER NOT NULL,
SEX CHAR(1) NOT NULL,
BIRTHYEAR INTEGER NOT NULL,
Y INTEGER NOT NULL,
SAMPLE FLOAT
) ON COMMIT PRESERVE ROWS;
--Create pivot table
execute block
as
declare variable y integer;
declare variable stmt varchar(4096);
begin
stmt = 'CREATE GLOBAL TEMPORARY TABLE GTT_PIVOT ('||
'ID INTEGER NOT NULL,'||
'SEX CHAR(1) NOT NULL,'||
'BIRTHYEAR INTEGER NOT NULL';
for select distinct
gtt_source.y
from gtt_source
order by 1
into
:y
do
stmt = stmt || ',y' || cast( y as varchar(4))|| ' float default 0';
stmt = stmt||') ON COMMIT PRESERVE ROWS;';
execute statement stmt;
end
--Pivot table created by code above
CREATE GLOBAL TEMPORARY TABLE GTT_PIVOT (
ID INTEGER NOT NULL,
SEX CHAR(1) NOT NULL,
BIRTHYEAR INTEGER NOT NULL,
y1961 float default 0,
y1962 float default 0,
y1963 float default 0,
y1964 float default 0,
...
y2018 float default 0,
y2019 float default 0,
y2020 float default 0,
y2021 float default 0
) ON COMMIT PRESERVE ROWS;
--Code to populate pivot table with source data
execute block
as
declare variable Y integer;
declare variable ID integer;
declare variable SEX char(1);
declare variable BIRTHyear integer;
declare variable sample FLOAT;
declare variable stmt varchar(4096);
begin
stmt = '';
for select
gtt_source.y,
gtt_source.sex,
gtt_source.birthyear,
gtt_source.sample
from gtt_source
into
:y,
:id,
:sex,
:birthyear ,
:sample
do begin
if (sample is not null) then begin
stmt = 'update or insert into gtt_pivot'
|| ' (id, sex, birthyear, y' || cast( y as varchar(4)) || ')'
|| ' values (' || cast( id as varchar(4))
|| ', ' || '''' || :sex || ''''
|| ', ' || cast( birthyear as varchar(4))
|| ', ' || cast( sample as varchar(10)) || ')'
|| ' matching (id, sex, birthyear);';
execute statement stmt;
end
end
end