RN
вы вообще неправильно используете AggregatingMergeTree
И у вас нету hash_id в селекте MV
create table contacts (client_id UInt32, hash_id UInt32, dts Date, dtm Date)
Engine MergeTree() PARTITION BY toYYYYMM(dts)
ORDER BY (client_id, hash_id);
CREATE TABLE stg_device (
client_id UInt32,
hash_id UInt32,
dts SimpleAggregateFunction(min, Date),
dtm SimpleAggregateFunction(max, Date)
) ENGINE = AggregatingMergeTree()
PARTITION BY toYYYYMM(dts)
ORDER BY (client_id, hash_id)
SETTINGS index_granularity = 8192;
CREATE MATERIALIZED VIEW stg_device_handler
TO stg_device
(
client_id UInt32,
hash_id UInt32,
dts Date,
dtm Date
)
AS
SELECT
client_id,
min(dts) as dts,
max(dtm) as dtm
FROM contacts
GROUP BY client_id,
hash_id
;
insert into contacts values (1,1,'2011-05-05', '2020-07-06');
insert into contacts values (1,1,'2011-05-06', '2020-07-01');
optimize table stg_device final
select * from stg_device;