我有一个从表中返回组(簇)的函数。
create function dbo.ftAllClusters() returns table as return
select distinct Cluster from Company
现在我需要为返回的每一行添加RowNumber。
答案 0 :(得分:1)
SELECT Cluster, ROW_NUMBER() OVER(ORDER BY Cluster) AS RowNo
FROM
(
SELECT DISTINCT Cluster
FROM Company
) x
或者...
SELECT Cluster, ROW_NUMBER() OVER (ORDER BY Cluster) AS RowNo
FROM Company
GROUP BY Cluster
答案 1 :(得分:0)
在SQL中有一个名为ROW_NUMBER()
的函数修改强> 根据评论失去不同功能的顺序 您可以尝试SubSelet
create function dbo.ftAllClusters() returns table as return
Select
Cluster
,(ROW_NUMBER() OVER (order by Cluster)) as RN
from (
Select
distinct
,Cluster
from Company ) as Comp
或者您可以尝试使用Group BY而不是Distinc(我甚至认为这有点快)
Select
Cluster
,(ROW_NUMBER() OVER (order by Cluster)) as RN
from Company
group by Cluster
答案 2 :(得分:0)
这是我制定的解决方案,有更好的方法吗?
ALTER function dbo.ftAllClusters() returns table as return
With CTE(comno) as
(select distinct Cluster from company)
select id=Row_number() over(order by comno),comno from cte