1 SQL 문법
SELECT m, N
에서 (
SELECT ROW_NUMBER ()를 통해 (분할 하여 m의 순서 에 의해 N 개의 DESC ) RN, - m - 패킷 각각 역순 N 각 숫자에 요구 패킷
m, N
에서 표
WHERE ...
) W
WHERE w.rn <= 10 , 10 미만의 번호
순서 에 의해 m, N- DESC
고객 데이터를 얻기 위해 한 달 전에 2, 십가지 경우
원래 데이터
케이스 SQL
select
StatDate,
OrderCount,
AmountTotal,
CustomerUnitPrice
from
(
select
row_number () over (partition by t.StatDate order by t.AmountTotal desc) rn,
*
from
(
select
CONVERT(varchar(7), AuditTime, 120) StatDate,
FBM_USER_ID CustomerId,
Count(1) OrderCount,
sum(isnull(FBM_BLL_BOOK_TOTAL,0)) AmountTotal,
case when sum(isnull(FBM_BLL_BOOK_TOTAL,0)) >0 then Round((sum(isnull(FBM_BLL_BOOK_TOTAL,0)) / Count(1) + 0.0), 4) else 0 end as CustomerUnitPrice
from
F_CUST_BOOK_MSTR b
where auditStatus=2 and AuditTime is not null
and AuditTime>='2017-06-01'
group by CONVERT(varchar(7), AuditTime, 120),FBM_USER_ID
) t
) tt
where rn <=10
order by StatDate desc
查询结果