你会怎么做:
Tim ...
Tim ...
Henry ...
Henry ...
Henry ...
我有一个表格,在第一列上有这些名称,我有兴趣添加一个新的列X,应该有:
0.5
0.5
0.333
0.333
0.333
如果要执行新列的总和,则计算第一列中不同条目的数量。谢谢!
答案 0 :(得分:2)
您可以使用以下内容:
select name,
1.0 / count(name) over(partition by name) as X
from yourtable
答案 1 :(得分: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]