[英]How to count repeating values in a column in PostgreSQL?
Hi I have a table like below, and I want to count the repeating values in the status column.嗨,我有一个如下表,我想计算状态列中的重复值。 I don't want to calculate the overall duplicate values.
我不想计算整体重复值。 For example, I just want to count how many "Offline" appears until the value changes to "Idle".
例如,我只想计算在值变为“空闲”之前出现了多少“离线”。
This is the result I wanted.这是我想要的结果。 Thank you.
谢谢你。
This is often called gaps-and-islands.这通常称为间隙和孤岛。
One way to do it is with two sequences of row numbers.一种方法是使用两个行号序列。
Examine each intermediate result of the query to understand how it works.检查查询的每个中间结果以了解其工作原理。
WITH
CTE_rn
AS
(
SELECT
status
,dt
,ROW_NUMBER() OVER (ORDER BY dt) as rn1
,ROW_NUMBER() OVER (PARTITION BY status ORDER BY dt) as rn2
FROM
T
)
SELECT
status
,COUNT(*) AS cnt
FROM
CTE_rn
GROUP BY
status
,rn1-rn2
ORDER BY
min(dt)
;
Result结果
| status | cnt |
|---------|-----|
| offline | 2 |
| idle | 1 |
| offline | 2 |
| idle | 1 |
WITH
cte1 AS ( SELECT status,
"date",
workstation,
CASE WHEN status = LAG(status) OVER (PARTITION BY workstation ORDER BY "date")
THEN 0
ELSE 1 END changed
FROM test ),
cte2 AS ( SELECT status,
"date",
workstation,
SUM(changed) OVER (PARTITION BY workstation ORDER BY "date") group_num
FROM cte1 )
SELECT status, COUNT(*) "count", workstation, MIN("date") "from", MAX("date") "till"
FROM cte2
GROUP BY group_num, status, workstation;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.