如何在ClickHouse中查找降价商品?
How to find products with dropped price in ClickHouse?
在 ClickHouse 中,我有 table product_prices
产品价格历史记录。此 table 包括以下属性:
CREATE TABLE product_prices
(
ProductId UInt64,
TrackedAt Date,
Price Nullable(Decimal(6, 2))
)
ENGINE MergeTree() ORDER BY (ProductId, TrackedAt)
从一组预定义的 product_ids
中,我需要找到满足以下条件的那些:
这个product_id的最新价格低于倒数第二个。
示例:
| ProductId | Price | TrackedAt |
|:-----------|------------:|:------------:|
| 1 | 20 | 2019-01-16 |
| 1 | 19 | 2019-01-17 |
| 2 | 5 | 2019-01-16 |
| 2 | 7 | 2019-01-17 |
我需要
| ProductId |
|:-----------|
| 1 |
我只能找出一种产品的不同之处:
select (argMax(Price, TrackedAt) - argMin(Price, TrackedAt)) from (select ProductId, Price, TrackedAt from product_prices where ProductId = 1000 order by TrackedAt DESC limit 2)
你知道我怎么做吗?
基本思路是用数组来捕捉每个产品的局部状态。通过使用 returns 升序记录 w.r.t TrackedAt
的子查询,您可以获得每个产品的升序数组。
WITH groupArray(Price) AS ps
SELECT ProductId
FROM
(
SELECT *
FROM product_prices
ORDER BY TrackedAt ASC
)
GROUP BY ProductId
HAVING (length(ps) > 1) AND (ps[-1] < ps[-2])
在 ClickHouse 中,我有 table product_prices
产品价格历史记录。此 table 包括以下属性:
CREATE TABLE product_prices
(
ProductId UInt64,
TrackedAt Date,
Price Nullable(Decimal(6, 2))
)
ENGINE MergeTree() ORDER BY (ProductId, TrackedAt)
从一组预定义的 product_ids
中,我需要找到满足以下条件的那些:
这个product_id的最新价格低于倒数第二个。
示例:
| ProductId | Price | TrackedAt |
|:-----------|------------:|:------------:|
| 1 | 20 | 2019-01-16 |
| 1 | 19 | 2019-01-17 |
| 2 | 5 | 2019-01-16 |
| 2 | 7 | 2019-01-17 |
我需要
| ProductId |
|:-----------|
| 1 |
我只能找出一种产品的不同之处:
select (argMax(Price, TrackedAt) - argMin(Price, TrackedAt)) from (select ProductId, Price, TrackedAt from product_prices where ProductId = 1000 order by TrackedAt DESC limit 2)
你知道我怎么做吗?
基本思路是用数组来捕捉每个产品的局部状态。通过使用 returns 升序记录 w.r.t TrackedAt
的子查询,您可以获得每个产品的升序数组。
WITH groupArray(Price) AS ps
SELECT ProductId
FROM
(
SELECT *
FROM product_prices
ORDER BY TrackedAt ASC
)
GROUP BY ProductId
HAVING (length(ps) > 1) AND (ps[-1] < ps[-2])