0

你会怎么做:

Tim   ...
Tim   ...
Henry  ...
Henry  ...
Henry ...

我有一个表在第一列包含这些名称,我有兴趣添加一个新列 X,它应该有:

0.5
0.5
0.333
0.333
0.333

如果要计算新列的总和,则计算第一列中不同条目的数量。谢谢!

4

2 回答 2

2

您可以使用以下内容:

select name,
  1.0 / count(name) over(partition by name) as X
from yourtable

请参阅带有演示的 SQL Fiddle

于 2013-02-13T10:45:19.257 回答
1

请试试:

select 
 [Name], 1.00/count(*) 
from YourTable 
group by [Name]

样本:

declare @testTable table (Name varchar(10))

insert into @testTable
select 'A' UNION ALL
select 'A' UNION ALL
select 'A' UNION ALL
select 'B' UNION ALL
select 'B'

SELECT 
    [Name], 1.00/count(*) 
FROM @testTable 
GROUP BY [Name]
于 2013-02-13T10:44:33.360 回答