-- Create a table with grouped measurements
CREATE TABLE group_measurements (
tenant_id uuid,
group_id int,
x numeric,
y numeric,
PRIMARY KEY (tenant_id, group_id, x)
);
-- Insert grouped data
INSERT INTO group_measurements (tenant_id, group_id, x, y) VALUES
('11111111-1111-1111-1111-111111111111', 1, 1, 1),
('11111111-1111-1111-1111-111111111111', 1, 2, 4),
('11111111-1111-1111-1111-111111111111', 1, 3, 9),
('11111111-1111-1111-1111-111111111111', 2, 1, 2),
('11111111-1111-1111-1111-111111111111', 2, 2, 3),
('11111111-1111-1111-1111-111111111111', 2, 3, 5);
-- Calculate Xi correlation
SELECT xicor(x, y) FROM measurements;
-- Calculate correlation by group
SELECT
group_id,
xicor(x, y) as correlation
FROM group_measurements
GROUP BY group_id;