복잡 한 SQL 구현 사고
도시 등급 (city rank) 이 3 보다 작고 GMV 가 6000 보다 크 거나 도시 등급 이 3 보다 크 며 GMV 가 5000 보다 크 면 고소 비 (gq) 로 정의 된다.
도시 등급 (city rank) 이 3 보다 작고 광고 수입 이 360 보다 크 거나 도시 등급 이 3 보다 크 며 광고 수입 이 300 보다 크 면 고소 득 (pq) 으로 정의 된다.
flow_rank: 0 저 유량 1 중 유량 2 고 유량
합작 업 체 가 여러 도 시 를 뛰 어 넘 고 cityrank 최소 값 은 cityrank, 총 수입 은 도시 에서 합 쳐 지고 광고 총 수입 은 합작 업 체 에 따라 받는다.
2. 조회 요청
3. 실현
select distinct
tc.partner_id as partnerId,
tc.contract_id as contractId,
tc.contract_num as contractNum,
tc.bd_id as bdId,
tc.org_id as orgId,
if(tc.org_scale='NULL','0',tc.org_scale) as orgScale
from table tc
join (
select
tc.partner_id,
case
when min(city_rank)<=3 and avg(t.gmv)>=6000 then 1
when min(city_rank)>3 and avg(t.gmv)>=5000 then 1
else 0
end as gq,
case
when min(city_rank)<=3 and avg(t.gross_profit+t.advertisement_gross_profit)>=360 then 1
when min(city_rank)>3 and avg(t.gross_profit+t.advertisement_gross_profit)>=300 then 1
else 0
end as pq,
max(tc.flow_rank) as fq,
sum(t.gmv) as gmv
from (
select partner_id,poi_id,min(city_rank) as city_rank,
max(tc.flow_rank) as flow_rank,
sum(is_old) as is_old
from table tc
where tc.partner_id>=#{start} and tc.partner_id<=#{end}
group by partner_id,poi_id
) tc
join (
select poi_id,
sum(gmv) as gmv,
sum(gross_profit) as gross_profit,
avg(advertisement_gross_profit) as advertisement_gross_profit
from table tc
where tc.partner_id>=#{start} and tc.partner_id<=#{end}
group by poi_id
) t on tc.poi_id=t.poi_id
group by tc.partner_id
having max(t.gmv)<10000 and sum(t.gmv)<30000 and sum(tc.is_old)>0 and
case
when sum(gmv)>0 then sum(gross_profit)/sum(gmv)>0.02
else 1=1
end
) t2 on t.partner_id=tc.partner_id
where
]]>
((t2.gq=1 and t2.pq=0 and fq!=2) or (t2.gq=0 and t2.pq=1 and fq!=2) or (t2.gq=0 and t2.pq=0 and fq=1) or (t2.gq=0 and t2.pq=0 and fq=0 and t2.gmv>2000))
4. 관건
4.1 t2
a) tc 서브 조회 데이터 계산, 첫 합작 여부
b) t 자 조회 로 총이익, 총수입 계산
c) 합작 업 체 의 소비 액, 유 동량, 수입 유형 을 계산 하고 요구 1 을 만족 시 키 는 합작 업 체 를 조회 한다.
d) having 서브 조회 여과, case 자구 제한 총 이율
4.2 요구 2
필터 위치
5. 총화
취 합 컴 퓨 팅, 여과, 업무 기능 은 SQL 을 사용 하여 이 루어 집 니 다.
만약 코드 로 유사 한 기능 을 실현 한다 면 복잡 한 정 도 는 상상 할 수 있 고 모든 집합 은 큰 덩어리 코드 에 대응 할 것 이다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Redash를 사용할 때 몰랐던 SQL을 쓰는 법을 배웠습니다.최근 redash에서 sql을 쓸 기회가 많고, 이런 쓰는 방법이 있었는지와 sql에 대해 공부를 다시하고 있기 때문에 배운 것을 여기에 씁니다. Redash란? 월별로 데이터를 표시하고 싶습니다 주별로 데이터를 표...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.