![](/img/trans.png)
[英]SQL Server Count Distinct records with a specific condition in window functions
[英]How to do a COUNT(DISTINCT) using window functions with a frame in SQL Server
捎带这个可爱的问题: Partition Function COUNT() OVER possible using DISTINCT
我希望计算不同值的移动计数。 类似于以下内容:
Count(distinct machine_id) over(partition by model order by _timestamp rows between 6 preceding and current row)
显然,SQL Server 不支持该语法。 不幸的是,我不太了解(没有内化会更准确)dense_rank 绕行是如何工作的:
dense_rank() over (partition by model order by machine_id)
+ dense_rank() over (partition by model order by machine_id)
- 1
因此我无法对其进行调整以满足我对移动 window 的需求。 如果我按 machine_id 订购,是否也可以按 _timestamp 订购并使用 _timestamp rows between
的行?
dense_rank()
给出当前记录的密集排名。 当您首先使用ASC
排序顺序运行它时,您会从第一个元素中获得当前记录的密集排名(唯一值排名)。 当您使用DESC
命令运行时,您会从最后一条记录中获得当前记录的密集排名。 然后你删除 1 因为当前记录的密集排名被计算了两次。 这给出了整个分区中的总唯一值(并为每一行重复)。
因为, dense_rank
不支持frames
,你不能直接使用这个解决方案。 您需要通过其他方式生成frame
。 一种方法是通过正确的unique id
比较来JOIN
同一个表。 然后,您可以在组合版本上使用dense_rank
。
请查看以下解决方案建议。 假设您的表中有一个唯一的记录键 ( record_id
)。 如果您没有唯一键,请在第一个 CTE 之前添加另一个 CTE 并为每条记录生成一个唯一键(使用new_id()
function 或使用concat()
组合多个列,中间带有分隔符以解释NULLs
)
; WITH cte AS (
SELECT
record_id
, record_id_6_record_earlier = LEAD(machine_id, 6, NULL) OVER (PARTITION BY model ORDER BY _timestamp)
, .... other columns
FROM mainTable
)
, cte2 AS (
SELECT
c.*
, DistinctCntWithin6PriorRec = dense_rank() OVER (PARTITION BY c.model, c.record_id ORDER BY t._timestamp)
+ dense_rank() OVER (PARTITION BY c.model, c.record_id ORDER BY t._timestamp DESC)
- 1
, RN = ROW_NUMBER() OVER (PARTITION BY c.record_id ORDER BY t._timestamp )
FROM cte c
LEFT JOIN mainTable t ON t.record_id BETWEEN c.record_id_6_record_earlier and c.record_id
)
SELECT *
FROM cte2
WHERE RN = 1
此解决方案有 2 个限制:
如果帧的记录少于 6 条,则LAG()
function 将为NULL
,因此此解决方案将不起作用。 这可以通过不同的方式处理:我能想到的一种快速方法是生成 6 个 LEAD 列(1 个之前的记录,2 个之前的记录等),然后将BETWEEN
子句更改为类似这样的东西BETWEEN COALESCE(c.record_id_6_record_earlier, c.record_id_5_record_earlier, ...., c.record_id_1_record_earlier, c.record_id) and c.record_id
COUNT()
不计算NULL
。 但是DENSE_RANK
可以。 如果它适用于您的数据,您也需要考虑这一点
只需使用outer apply
:
select t.*, t2.num_machines
from t outer apply
(select count(distinct t2.machine_id) as num_machines
from (select top (6) t2.*
from t t2
where t2.model = t.model and
t2.timestamp <= t.timestamp
order by t2.timestamp desc
) t2
) t2;
如果每个 model 有很多行,您还可以使用lag()
的(繁琐)技巧:
select t.*, v.num_machines
from (select t.*,
lag(machine_id, 1) over (partition by model order by timestamp) as machine_id_1,
lag(machine_id, 2) over (partition by model order by timestamp) as machine_id_2,
lag(machine_id, 3) over (partition by model order by timestamp) as machine_id_3,
lag(machine_id, 4) over (partition by model order by timestamp) as machine_id_4,
lag(machine_id, 5) over (partition by model order by timestamp) as machine_id_5
from t
) t cross apply
(select count(distinct v.machine_id) as num_machines
from (values (t.machine_id),
(t.machine_id_1),
(t.machine_id_2),
(t.machine_id_3),
(t.machine_id_4),
(t.machine_id_5)
) v(machine_id)
) v;
在许多情况下,这可能在 SQL 服务器中具有最佳性能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.