2017-01-12 23 views
0

如何根據「競爭」和「用戶」列獲得最後一列?以下查詢給出錯誤!如何根據兩列找到一列的數量

SELECT DISTINCT COUNT(*) AS countofcomments 
 
FROM k 
 
GROUP BY competition, user

enter image description here

+0

你的意思是,「如何查找*根據兩列計算一列?」 –

+0

@Alex謝謝你讓我知道!我修好了它。我只有3天時間才能拿到研討會論文,但仍然沒有適當的數據來運行數據分析!我的大腦停止工作 –

+0

我不明白什麼意見應該包含。它是一個數字,一個字符串,還是帶有一系列註釋的表格?你的表結構是什麼? –

回答

0

我不知道的是,一個很好的做法,你想實現的,在考慮到結果集,你可以嘗試這樣的

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 
0
東西

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

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