Skip to content

Instantly share code, notes, and snippets.

@iyunbo
Last active September 19, 2019 20:18
Show Gist options
  • Save iyunbo/e24bac05a4f8639b6d91291bdd099272 to your computer and use it in GitHub Desktop.
Save iyunbo/e24bac05a4f8639b6d91291bdd099272 to your computer and use it in GitHub Desktop.
Clickhouse many to many example
DROP TABLE schedule;
CREATE TABLE schedule
(
aggregateId UInt16,
scheduleId UInt16,
cashflow Float32,
perimeter Array(String)
)
ENGINE = TinyLog;
insert into schedule(aggregateId, scheduleId, cashflow, perimeter) values
(1, 60, 100, array('PERIMETER 1', 'PERIMETER 2'))
(1, 70, 150, array('PERIMETER 1', 'PERIMETER 2'))
(2, 80, 120, array('PERIMETER 4'))
(2, 90, 200, array('PERIMETER 4'))
(3, 100, 1000, array('PERIMETER 3', 'PERIMETER 2'));
select * from schedule where has(perimeter, 'PERIMETER 2');
select * from schedule where hasAll(perimeter, ['PERIMETER 2','PERIMETER 1']);
select * from schedule where hasAny(perimeter, ['PERIMETER 1','PERIMETER 3']);
select sum(cashflow) from schedule where has(perimeter, 'PERIMETER 2');
drop table schedule2;
CREATE TABLE schedule2
(
aggregateId UInt16,
scheduleId UInt16,
cashflow Float32,
perimeter Tuple(String, String, String)
)
ENGINE = TinyLog;
insert into schedule2(aggregateId, scheduleId, cashflow, perimeter) values
(1, 60, 100, tuple('PERIMETER 1', 'PERIMETER 2', 'PERIMETER 3'))
(1, 70, 150, tuple('PERIMETER 2', 'PERIMETER 3', 'PERIMETER 4'))
(2, 80, 120, tuple('PERIMETER 4', 'PERIMETER 2', 'PERIMETER 1'))
(2, 90, 200, tuple('PERIMETER 4', 'PERIMETER 5', 'PERIMETER 1'))
(3, 100, 1000, tuple('PERIMETER 5', 'PERIMETER 2', 'PERIMETER 3'));
select multiply(cashflow, cashflow) from schedule2;
with (select sum(cashflow) from schedule2) as total
select divide(cashflow, total) as ratio from schedule2;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment