查找不定期增加的行
Find rows not increased regularly
我有一个 table 来存储产品的投票和评论者。评论者的价值会随着时间的推移而增加。
CREATE TABLE product
(
product_id character varying(255),
voting integer,
reviewers integer,
created_at timestamp without time zone
);
数据
+------------+-----------+--------+---------------------+
| product_id | reviewers | voting | created_at |
+------------+-----------+--------+---------------------+
| B000GRMH8A | 50 | 5 | 2015-11-19 00:41:30 |
| B000GRMH8A | 38 | 4 | 2015-11-17 00:42:03 |
| B000GRMH8A | 20 | 2 | 2015-11-15 00:41:23 |
| HXXRSEW | 50 | 3 | 2015-11-19 00:41:30 |
| HXXRSEW | 72 | 2 | 2015-11-17 00:42:03 |
| HXXRSEW | 48 | 1 | 2015-11-15 00:41:23 |
+------------+-----------+--------+---------------------+
如何找到包含无效数据的所有行?
假设您的问题如评论中的错误,这可能是您的查询:
SELECT *
FROM (
SELECT *, lag(reviewers) OVER (PARTITION BY product_id
ORDER BY created_at) reviewers_prev
FROM product
) sub
WHERE reviewers < reviewers_prev;
在子查询中使用 window function lag()
,然后过滤审阅者数量减少的行 - 违反您的规则:reviewers value will increase by time
.
我有一个 table 来存储产品的投票和评论者。评论者的价值会随着时间的推移而增加。
CREATE TABLE product
(
product_id character varying(255),
voting integer,
reviewers integer,
created_at timestamp without time zone
);
数据
+------------+-----------+--------+---------------------+
| product_id | reviewers | voting | created_at |
+------------+-----------+--------+---------------------+
| B000GRMH8A | 50 | 5 | 2015-11-19 00:41:30 |
| B000GRMH8A | 38 | 4 | 2015-11-17 00:42:03 |
| B000GRMH8A | 20 | 2 | 2015-11-15 00:41:23 |
| HXXRSEW | 50 | 3 | 2015-11-19 00:41:30 |
| HXXRSEW | 72 | 2 | 2015-11-17 00:42:03 |
| HXXRSEW | 48 | 1 | 2015-11-15 00:41:23 |
+------------+-----------+--------+---------------------+
如何找到包含无效数据的所有行?
假设您的问题如评论中的错误,这可能是您的查询:
SELECT *
FROM (
SELECT *, lag(reviewers) OVER (PARTITION BY product_id
ORDER BY created_at) reviewers_prev
FROM product
) sub
WHERE reviewers < reviewers_prev;
在子查询中使用 window function lag()
,然后过滤审阅者数量减少的行 - 违反您的规则:reviewers value will increase by time
.