【发布时间】:2014-12-11 02:38:56
【问题描述】:
我对 SQL 非常陌生(大约两个月后学习即用型职位),我正在尝试编写一个查询,该查询将为每个客户拉出主要订单中心(即,哪个中心是最受每个客户的青睐)。我只希望每位客户退货一次。
现在,我有一个查询返回最高订购客户和相关中心。它看起来像这样:
with x as (
select
row_number() over (order by cusid asc)
as row
,cusid
,centerid
,count(centerid) numofcenter
from orderdb
where isnumeric (cusid)=1
and estimate=0
group by centered, cusid
)
select row, centered, cusid, numofcenter
from x
where numofcenter=(
select max(numofcenter) from x)
order by cusid
任何帮助或指导将不胜感激。
【问题讨论】:
-
如果您添加创建表定义和一些插入示例数据,您将获得快速答案。
标签: sql sql-server max with-statement