복잡 한 SQL 구현 사고

1. 정의
도시 등급 (city rank) 이 3 보다 작고 GMV 가 6000 보다 크 거나 도시 등급 이 3 보다 크 며 GMV 가 5000 보다 크 면 고소 비 (gq) 로 정의 된다.
도시 등급 (city rank) 이 3 보다 작고 광고 수입 이 360 보다 크 거나 도시 등급 이 3 보다 크 며 광고 수입 이 300 보다 크 면 고소 득 (pq) 으로 정의 된다.
flow_rank: 0 저 유량 1 중 유량 2 고 유량
합작 업 체 가 여러 도 시 를 뛰 어 넘 고 cityrank 최소 값 은 cityrank, 총 수입 은 도시 에서 합 쳐 지고 광고 총 수입 은 합작 업 체 에 따라 받는다.
2. 조회 요청
  • GMV 도 시 는 최고 10000 보다 적 고 총 화 는 30000 보다 적 으 며 비 첫 합작, 예 를 들 어 GMV 의 총이익 이 0.02
  • 보다 크다.
  • 고소 비 액, 저 소득, 비고 유량;저 소비 액, 고소 득, 비고 유량;저 소비 액, 저 소득, 중 유량;저 소비 액, 저 소득, 저 유량, 그리고 합작 업 체 gmv 가 2000
  • 을 초과 합 니 다.
    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 을 사용 하여 이 루어 집 니 다.
    만약 코드 로 유사 한 기능 을 실현 한다 면 복잡 한 정 도 는 상상 할 수 있 고 모든 집합 은 큰 덩어리 코드 에 대응 할 것 이다.

    좋은 웹페이지 즐겨찾기