반응형
use workdb
go
select a.ntile,
count(distinct accountid) 'pu',
min(sales) 'min_sales',
max(sales) 'max_sales',
avg(sales) 'avg_sales',
sum(sales)'total.sum'
from
(
select server, accountid, sum(price) 'sales', ntile(10) over ( order by sum(price) desc) as ntile
from dbo.purchase a with (nolock)
join dbo.cm_purchase b with (nolock) on a.clientproductid=b.clientproductid
where
cast(time as date) between '2019-09-01' and ' 2019-09-30' --- 기간
group by
server, accountid
) as a
group by
a.ntile
order by 1
---- 10단위별 pu 및 매출 분포(중앙값)
select aa.ntile, aa.median_sales
from
(
select a.ntile
,percentile_disc(0.5) within group (order by a.sales desc ) over (partition by a.ntile) 'median_sales'
from
(
select server, accountid, sum(price) 'sales', ntile(10) over ( order by sum(price) desc) as ntile
from dbo.purchase a with (nolock)
join dbo.cm_purchase b with (nolock) on a.clientproductid=b.clientproductid
where
cast(time as date) between '2019-09-01' and ' 2019-09-30' -- 기간
group by
server, accountid
) as a
) as aa
group by
aa.ntile, aa.median_sales
반응형
'sql' 카테고리의 다른 글
[SQL] 잔존율 retention 집계 하기 D+7, M+1 (0) | 2022.09.26 |
---|---|
[sql] DB 컬럼 정보 조회 방법, sp 내용 확인하기 (0) | 2022.09.26 |
[sql] 카테고리별 매출 계산 (ABC 분석, 팬차트, 히스토그램) (0) | 2022.09.26 |
[sql] STRING_SPLIT 함수, 구분자로 자르기, 행열 변환 (0) | 2022.09.26 |
[SQL] like 연산자 (0) | 2021.04.14 |
댓글