sql - 跨移动窗口计算字符串值

标签 sql postgresql window-functions

我正在尝试计算移动窗口中字符串值的出现次数。具体来说,我想计算前 3 行中每个字符串值的出现次数——不包括该行

我的数据看起来像这样:

id |  color
---+---------
 0 |  'blue'
 : |      :
 6 | 'green'
 7 |  'blue'
 8 | 'green'
 9 |   'red'
10 |   'red'

我正试图得到这样的东西:

id | n_red | n_blue | n_green 
---+-------+--------+---------
 : |     : |      : |       :
 9 |     0 |      1 |       2
10 |     1 |      1 |       1

其中数据是 3 行中每个“红色”、“蓝色”和“绿色”的出现次数(例如,在 id 10 之前的 3 行中有 1 个红色, 1 个蓝色和 1 个绿色)。

我认为我应该能够使用窗口函数来完成此操作,但还没有完全解决。

select
    sum(red) over(order by id rows between 3 preceding and 1 preceding) end as n_red,
    sum(blue) over(order by id rows between 3 preceding and 1 preceding) end as n_blue,
    sum(green) over(order by id rows between 3 preceding and 1 preceding) end as n_green
from (select id,
        case when color = 'red' then 1 else 0 end as red,
        case when color = 'blue' then 1 else 0 end as blue,
        case when color = 'green' then 1 else 0 end as green
    from color_table) as dummy_color_table

这似乎可行,但不是很简洁。有没有人有编写这些类型的查询的经验,谁可以改进它?

最佳答案

您可以利用 Postgres 中的 :::

select sum((color = 'red')::int) over (order by id rows between 3 preceding and 1 preceding) end as n_red,
       sum((color = 'blue')::int) over (order by id rows between 3 preceding and 1 preceding) end as n_blue,
       sum((color = 'green')::int) over (order by id rows between 3 preceding and 1 preceding) end as n_green
from color_table;

关于sql - 跨移动窗口计算字符串值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28662123/

相关文章:

sql - 使用同一表上的选择更新行

sql - SQL查询中查找空记录的查询问题

arrays - Postgres JSONB : where clause for arrays of arrays

Mysql-如何在案例数达到峰值时获取最大日期

mysql - index_together 的顺序在 Django 模型中是否重要?

sql - 哪个查询效率更高?内部联接与子查询?总计与有

java - 挑战 token 错误

sql - 使用CASE和GROUP BY进行数据透视的动态替代方案

SQL - LAG 相当于使用 OVER 子句检索以前的值并存储在当前行中?

c# - 无效的对象名称 'dbo.CategoryIdArray'