【问题标题】:use default value in partition clause of a window function if null如果为空,则在窗口函数的分区子句中使用默认值
【发布时间】:2017-10-05 12:53:10
【问题描述】:

我在查询中使用窗口函数根据行组合中的值对行求和。现在如果 1 行包含 null 那么我必须将其视为错误我该怎么办?我曾尝试在分区中添加coalesce(atg.flag,false),但它不起作用。

【问题讨论】:

  • coalesce(atg.flag,false) 应该可以工作。引用分区 - 你说的是窗口函数吗?..
  • 是的,我正在使用 windows 功能。
  • Edit您的问题并添加您正在使用的查询。 Formatted textno screen shots Edit 您的问题 - 请不要在 cmets 中提供邮政编码或其他信息。

标签: sql postgresql window-functions isnull


【解决方案1】:

coalesce 是一种方式,这里是一个例子:

t=# with dset(i,bool) as (values(1,true),(2,false),(3,null))
select i, bool::text, count(1) over (partition by coalesce(bool,false))
from dset;
 i | bool  | count
---+-------+-------
 2 | false |     2
 3 |       |     2
 1 | true  |     1
(3 rows)

你可以看到 count =2 表示 null 和 false,=1 表示 true

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-25
    • 2018-09-12
    • 2017-12-25
    • 2016-01-02
    • 2013-07-25
    • 2012-12-01
    • 2015-02-13
    • 2019-03-09
    相关资源
    最近更新 更多