RosettaCodeData/Task/Matrix-transposition/DuckDB/matrix-transposition-2.duckdb
2025-08-11 18:05:26 -07:00

22 lines
638 B
Text

create or replace table A (i integer, j integer, value float );
insert into A values
(1, 1, 1),
(1, 2, 2),
(1, 3, 3),
(2, 1, 2),
(2, 2, 5),
(2, 3, 7);
# transposition
create or replace table AT as
(select j as i, i as j, value from A);
.print The matrix-like representation of A:
create or replace table AMatrix as (pivot A on j using max(value) order by i);
from AMatrix;
.print Convert the matrix-like representation back to the (i,j,value) representation:
unpivot AMatrix on columns('^[1-9]') into name j VALUE value;
.print The matrix-like representation of "A transpose":
pivot AT on j using max(value) order by i;