Sql server 2008 如何根据两列查找一列的计数

Sql server 2008 如何根据两列查找一列的计数,sql-server-2008,count,Sql Server 2008,Count,如何根据“竞争”和“用户”列获取最后一列?下面的查询给出了错误 选择不同的计数(*)作为注释的计数 来自k 按竞争分组,用户我不知道你想要达到的是一个好的实践,考虑到结果集,你可以尝试这样的方法 CREATE TABLE #Example( [competition] [nvarchar](50) NULL, [user] [nvarchar](50) NULL, [comments] nvarchar(50)

如何根据“竞争”和“用户”列获取最后一列?下面的查询给出了错误

选择不同的计数(*)作为注释的计数
来自k

按竞争分组,用户
我不知道你想要达到的是一个好的实践,考虑到结果集,你可以尝试这样的方法

CREATE TABLE #Example(
       [competition]      [nvarchar](50)   NULL,       
       [user]   [nvarchar](50)   NULL,
       [comments]   nvarchar(50) NOT NULL,
)
GO

INSERT   #Example  ([competition],[user],  [comments])   VALUES ('INDIA','CHENNAI','ssss')
INSERT   #Example  ([competition],[user],  [comments])   VALUES ('INDIA','KOCHI','ssss')
INSERT   #Example  ([competition],[user],  [comments])   VALUES ('INDIA','BANGLORE','ssss')
INSERT   #Example  ([competition],[user],  [comments])   VALUES ('INDIA','HYDERABAD','ssss')
INSERT   #Example  ([competition],[user],  [comments])   VALUES ('US','MAIAMI','ssss')
INSERT   #Example  ([competition],[user],  [comments])   VALUES ('US','SANFRANC','ssss')
INSERT   #Example  ([competition],[user],  [comments])   VALUES ('US','MOUNT','ssss')
INSERT   #Example  ([competition],[user],  [comments])   VALUES ('US','LOSANGELS','ssss')
INSERT   #Example  ([competition],[user],  [comments])   VALUES ('UK','MANCHESTER','ssss')
INSERT   #Example  ([competition],[user],  [comments])   VALUES ('UK','CHELSEA','ssss')

SELECT * from (
SELECT * FROM #Example 
) tab1 join 

(
SELECT [competition] , count([comments] )count FROM #Example
group by [competition]
) tab2 on tab1.[competition]= tab2.[competition]


DROP table #Example

可以使用窗口功能执行此操作:

SELECT *, COUNT(*) OVER(PARTITION BY competition, user) as [Count of comments]
FROM k

你是说,“如何根据两列计算一列的计数?”@Alex谢谢你让我知道!我修好了。我只有3天的时间完成研讨会论文,但仍然没有合适的数据来运行数据分析!我的大脑停止了工作,我不明白评论应该包含什么。它是数字、字符串还是带有注释数组的表?你的表结构是什么?我们不需要评论的内容。例如,在这里我们看到一个用户哈尼有2条评论!实际上,我想计算一个用户Hani的行数,创建一个名为“评论计数”的新列,并在其中插入2个。我恐怕不明白你想达到什么目的。