2013-07-31 222 views
1

我是SQL Server的新手,我需要獲得多少次重複列值的值,並按重複次數對它進行分組,位置。在一個特定列的表中計數重複的記錄

我的意思是:

Table ACTIVITY 
id  orderID  Location  date 

x  order11  NewYork  xxxx  
x  order22  Miami  xxxx  
x  order11  LA   xxxx 
x  order33  NewYork  xxxx 
x  order11  NewYork  xxxx 
x  order22  Miami  xxxx 
x  order22  NewYork  xxxx 
x  order44  Miami  xxxx 

我有這樣的:

Select [orderID], count(1) as CountOrder from [MobileService].[ACTIVITY] 
Group by [orderID] 
Order BY CountOrder Desc 

,並返回我:

orderID  CountOrder  

order11  3 
order22  3 
order33  1 
order44  1 

OK,好,但是,我想通過位置

過濾
Select [NewsItemTitle], count(1) as xx from [MobileServiceExtra].[ACTIVITY] 
Group by [NewsItemTitle] 
Order BY xx Desc 
WHERE [Location] = 'NewYork' 

並返回關鍵字'WHERE'附近的錯誤語法。

所以,我想,如果我通過紐約

orderID  CountOrder  

order11  2   
order22  1 
order33  1 

過濾我怎樣才能解決這個問題下面的結果呢?

回答

3

你接近,你剛剛的順序錯了:

Select [NewsItemTitle], count(1) as xx from [MobileServiceExtra].[ACTIVITY] 
WHERE [Location] = 'NewYork' 
Group by [NewsItemTitle] 
Order BY xx Desc 
+0

Thnks!非常接近,傑傑奧。 – Makito

相關問題