当前位置: 数据库>sqlserver
sql中获取分组排序后数据方法实例
来源: 互联网 发布时间:2014-08-29
本文导语: 例子,在sql语句中获取分组排序后的数据。 代码示例: select distinct b.OrgID,b.CompanyID,b.AreaID,b.CustChannelID,b.CustID,b.SaleTotalQty, GETDATE() from KDS400TelSurveyCustAll_Temp as tmp1 cross apply ( select top 10 percent * from KDS400TelSurveyCustAll_Temp tmp2...
例子,在sql语句中获取分组排序后的数据。
代码示例:
select distinct
b.OrgID,b.CompanyID,b.AreaID,b.CustChannelID,b.CustID,b.SaleTotalQty,
GETDATE() from KDS400TelSurveyCustAll_Temp as tmp1 cross apply
(
select top 10 percent * from KDS400TelSurveyCustAll_Temp tmp2
where tmp1.OrgID=tmp2.OrgID and tmp1.CompanyID=tmp2.CompanyID
and tmp1.AreaID=tmp2.AreaID and tmp1.CustChannelID=tmp2.CustChannelID
and tmp2.CustChannelID in (1027,1028)
order by tmp2.OrgID,tmp2.CompanyID,tmp2.AreaID,tmp2.AreaType,tmp2.CustChannelID
) as b
b.OrgID,b.CompanyID,b.AreaID,b.CustChannelID,b.CustID,b.SaleTotalQty,
GETDATE() from KDS400TelSurveyCustAll_Temp as tmp1 cross apply
(
select top 10 percent * from KDS400TelSurveyCustAll_Temp tmp2
where tmp1.OrgID=tmp2.OrgID and tmp1.CompanyID=tmp2.CompanyID
and tmp1.AreaID=tmp2.AreaID and tmp1.CustChannelID=tmp2.CustChannelID
and tmp2.CustChannelID in (1027,1028)
order by tmp2.OrgID,tmp2.CompanyID,tmp2.AreaID,tmp2.AreaType,tmp2.CustChannelID
) as b
解释:
获取取每个经销商在每个县级城市中两类客户。
取各类客户的前3个月累计销量排名在前10%的客户进行调查。
您可能感兴趣的文章:
- sql语句获取分组排序后数据
- SQL Server分组排序提取数据的代码举例
- sql server分组排序之row_number()over函数的使用