【问题标题】:Nesting window functions in SQL Server在 SQL Server 中嵌套窗口函数
【发布时间】:2013-05-01 12:27:23
【问题描述】:

有时我在摆弄 SQL Server 时会遇到类似于 Can't use a window function on an aggregate。这很令人沮丧,因为我有 SQL Server 2008,而且我知道 2012 提供了更好的窗口函数功能,而且我经常使用它们。但是,有时我会做类似的事情

select   me.patid
        ,COUNT(*) as eligibilityGapsNo
        ,COUNT(*) over(partition by count(*)) 
from memberEligibility as me
group by me.patid

这很好用。我想是因为我已经按 me.patid 分组了,但是有没有人更清楚什么时候可以嵌套这样的窗口函数?

【问题讨论】:

    标签: sql sql-server tsql window-functions


    【解决方案1】:

    您没有在此处嵌套窗口函数。您将聚合函数 count(*) 与窗口函数 count(*) over 嵌套在一起。

    您可以在窗口函数中嵌套聚合函数。而且,我做到了。但是,我发现将其编写为子查询更清楚,因为嵌套聚合函数对我来说“看起来不正确”:

    select patid, eg, count(*) over (partition by egcnt)
    from (select me.patid, count(*) as egcnt
          from memberEligibility me
          group by me.patid
         ) t
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-03
      • 1970-01-01
      • 1970-01-01
      • 2020-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多