我是 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
我该如何解决这个问题?