计算特定列的表中的重复记录

时间:2013-07-31 23:55:42

标签: sql

我是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
好的,好的,但是,我希望按位置过滤

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

并在关键字'WHERE'附近返回错误的语法。

所以,如果我按NewYork过滤

,我想获得以下结果
orderID      CountOrder       

order11      2         
order22      1
order33      1

我如何解决这个问题?

1 个答案:

答案 0 :(得分:3)

你很亲密,你的订单错了:

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