SELECT (@i: = @ i + 1) generated sequence number

Reprinted from https://blog.csdn.net/qq_27922171/article/details/86477544

 

The serial number is automatically generated in the same category: https:? //Bbs.csdn.net/topics/392035580 locationNum = 3 & fps = 1

 

 

- total loan amount, and when the parts are used
SELECT
    hou.time AS 'period'
    , isu.accum_loan_contract_amt AS 'total loan amount'
    , round (isu.issue_cost_time / isu.issue_finish_cnt) AS 'process used when lending'
from dim_hour HOU
left join (
    select
        isu.loan_contract_amt
        ,isu.issue_finish_cnt
        ,coalesce(isu.issue_cost_time,0) as issue_cost_time
        ,case
            when @time is null then @accum_loan_contract_amt:=isu.loan_contract_amt
            else @accum_loan_contract_amt:=@accum_loan_contract_amt + isu.loan_contract_amt
  end as accum_loan_contract_amt
        ,@time:=isu.time as time
    from (select @time:=null, @accum_loan_contract_amt:=null)s,
  (
        select
            hou.time
            ,sum(case when date(isu.loan_issue_time) = curdate() then isu.loan_contract_amt else 0 end) as loan_contract_amt
            ,count(case when date(coalesce(isu.loan_issue_time,isu.loan_issue_fail_time)) = curdate() then isu.split_order_id else null end) as issue_finish_cnt
            ,sum(case
                 when date(coalesce(isu.loan_issue_time,isu.loan_issue_fail_time)) = curdate()
                     then timestampdiff(second,isu.loan_issue_submit_time, coalesce(isu.loan_issue_time,isu.loan_issue_fail_time))
                 else null end) as issue_cost_time
        from dim_hour hou
        left join (
            SELECT * from pl_performance_loan_apply_fact where date(coalesce(loan_issue_time,loan_issue_fail_time)) = curdate()
        )isu on DATE_FORMAT(isu.loan_issue_time, '%H:%i:%s') >= hou.`time` and DATE_FORMAT(isu.loan_issue_time, '%H:%i:%s') < hou.due_time
        join (select time from dim_hour where start_time <= DATE_FORMAT(now() , '%H:%i:%s') and DATE_FORMAT(now(), '%H:%i:%s') < end_time) dim on hou.time <= dim.time
        group by hou.time
        order by hou.time
    )isu
)isu on isu.time = hou.time
;
 
 
- working hours, not classified to generate a serial number
SELECT  tot.collect_user_name AS `催收员`, sum(tot.collect_diff)/3600 AS `工作时间`
FROM
(
SELECT tot.collect_user_name
, CASE WHEN TIMESTAMPDIFF(SECOND, par.`collect_time`, tot.`collect_time`) > 1800 THEN NULL ELSE TIMESTAMPDIFF(SECOND, par.`collect_time`, tot.`collect_time`) END AS collect_diff
FROM
(
SELECT tot.*,  @collect_series:= @collect_series + 1 AS collect_series  
FROM ( 
    SELECT collect_user_name, collect_time FROM pl_collect_call_info cat
   
    ORDER BY collect_user_name, collect_time
) tot, ( SELECT @collect_series:=-1 )s
) tot
LEFT JOIN (
SELECT tot.*,  @collect_series_2:= @collect_series_2 + 1 AS collect_series_2    
FROM ( 
    SELECT collect_user_name, collect_time FROM pl_collect_call_info cat
    ORDER BY collect_user_name, collect_time
) tot, ( SELECT  @collect_series_2:=0 )s
) par ON tot.collect_series = par.collect_series_2 AND par.`collect_user_name` = tot.`collect_user_name`
WHERE par.collect_user_name IS NOT NULL
) tot
GROUP BY tot.collect_user_name
;
 
- Working Time, Method 2, the classification generated sequence number
SELECT collect_user_name, collect_time,NO
FROM
(
SELECT t.*,
       CASE WHEN @partition_by = t.collect_user_name THEN @rownum := @rownum + 1
            ELSE @rownum :=1
       END AS NO,
       @partition_by := t.collect_user_name AS partition_by
FROM ( 
    SELECT collect_user_name, collect_time FROM pl_collect_call_info cat
    WHERE  date(cat.collect_time) = curdate()
                     
    #ORDER BY collect_user_name, collect_time
) t
INNER JOIN (SELECT @rownum :=0) r
        ON 1=1
INNER JOIN (SELECT @partition_by :='') p
        ON 1=1
ORDER BY t.collect_user_name, t.collect_time
)tb
;
 
- enrollment distribution (time period), classified by time
The SELECT
    sls.time AS `period`
    , the when sls.time Case> `sub-enrollment AS hou.time the then null the else End` sls.cust_cnt
    , the when sls.time Case> hou.time the then null the else sls.accum_cust_cnt End the cumulative number of people registered as ``
from (
    the SELECT
        number dtl.cust_cnt #as `segment registered`
        , Case
            the when the then @accum_cust_cnt @time IS null: = dtl.cust_cnt
            the else @accum_cust_cnt: = @ accum_cust_cnt + dtl.cust_cnt End
            AS accum_cust_cnt # `total registered number of`
        , @ time: = time # dtl.time AS `time`
    from (the SELECT @time: = null, @accum_cust_cnt: = null) S, (
        the SELECT
            hou.time
            ,count(distinct usb.user_id) as cust_cnt
        from dim_hour hou
        left join (
            select usb.user_id,usb.regist_time from pl_customer_basic_info usb
            where date(usb.regist_time) = curdate() 
        )usb on DATE_FORMAT(usb.regist_time, '%H:%i:%s') >= hou.`time` and DATE_FORMAT(usb.regist_time, '%H:%i:%s') < hou.due_time
        group by hou.time
        order by hou.time
    )dtl
)sls
left join (
    select * from dim_hour
    where start_time <= DATE_FORMAT(now(), '%H:%i:%s')
        and DATE_FORMAT(now(), '%H:%i:%s') < end_time
) hou on 1=1
;
- enrollment distribution (time period), according to the company, vest, time classification
SELECT cpy.company
    ,sls.merchant_no
    ,sls.time as period
    ,case when sls.time > hou.time then null else sls.cust_cnt          end as rgs_cnt
    ,case when sls.time > hou.time then null else sls.accum_cust_cnt    end as accum_rgs_cnt
from (
    select dtl.time
        ,dtl.cust_cnt #as `分段注册人数`
        ,CASE
WHEN @merchant_no IS NULL THEN @accum_cust_cnt:=dtl.cust_cnt
WHEN @merchant_no != merchant_no2 THEN @accum_cust_cnt:=dtl.cust_cnt
ELSE @accum_cust_cnt:=@accum_cust_cnt + dtl.cust_cnt END
AS accum_cust_cnt #`累计注册人数`
        ,@merchant_no:=dtl.merchant_no2 as merchant_no #`时间段`
    from (select @merchant_no:=null, @accum_cust_cnt:=null)s, (
        select
            hou.time,usb.merchant_no,usb.merchant_no2
            ,count(distinct usb.user_id) as cust_cnt
        from dim_hour hou
        left join (
            select usb.user_id,usb.regist_time ,usb.merchant_no,usb.merchant_no as merchant_no2
   from (select * from pl_customer_basic_info usb where date(usb.regist_time) = curdate()) usb
            left join dim_company_app cpy on usb.merchant_no=cpy.merchant_no
        )usb on DATE_FORMAT(usb.regist_time, '%H:%i:%s') >= hou.`time` and DATE_FORMAT(usb.regist_time, '%H:%i:%s') < hou.due_time
        group by hou.time,usb.merchant_no,merchant_no2
        order by usb.merchant_no,hou.time
    )dtl
)sls
left join (
    select * from dim_hour
    where start_time <= DATE_FORMAT(now(), '%H:%i:%s')
        and DATE_FORMAT(now(), '%H:%i:%s') < end_time
) hou on 1=1
left join dim_company_app cpy on sls.merchant_no=cpy.merchant_no
where sls.merchant_no is not null
order by merchant_no,period
;
 
- cumulative loan amount, classified by time
SELECT
    hou.time AS 'period'
    , isu.accum_loan_contract_amt AS 'total loan amount'
    , round (isu.issue_cost_time / isu.issue_finish_cnt) 'process used when lending' AS
    , round (isu.issue_cost_time_v2 / isu.issue_finish_cnt) AS 'lending when using flow V2 '
from dim_hour HOU
left join (
    select
        isu.loan_contract_amt
        ,isu.issue_finish_cnt
        ,coalesce(isu.issue_cost_time,0) as issue_cost_time
        ,case
            when @time is null then @accum_loan_contract_amt:=isu.loan_contract_amt
            else @accum_loan_contract_amt:=@accum_loan_contract_amt + isu.loan_contract_amt end
             as accum_loan_contract_amt
        ,coalesce(isu.issue_cost_time_v2,0) as issue_cost_time_v2
        ,@time:=isu.time as time
    from (select @time:=null, @accum_loan_contract_amt:=null)s, (
        select
            hou.time
            ,sum(case when date(isu.loan_issue_time) = curdate() then isu.loan_contract_amt else 0 end) as loan_contract_amt
            ,count(case when date(isu.loan_issue_submit_time) = curdate() then isu.split_order_id else null end) as issue_finish_cnt
            ,sum(case
                 when date(coalesce(isu.loan_issue_time,isu.loan_issue_fail_time)) = curdate()
                     then timestampdiff(second,isu.loan_issue_submit_time, coalesce(isu.loan_issue_time,isu.loan_issue_fail_time))
                 else null end) as issue_cost_time
             ,sum(case
                 when date(coalesce(isu.loan_issue_time,isu.loan_issue_fail_time)) = curdate()
                     then timestampdiff(second,isu.loan_issue_submit_time, coalesce(isu.loan_issue_time,isu.loan_issue_fail_time))
             when date(isu.loan_issue_submit_time)= curdate() and isu.loan_issue_time is null and isu.loan_issue_fail_time is null
                 then timestampdiff(second,isu.loan_issue_submit_time, now())
                 else null end) as issue_cost_time_v2
        from dim_hour hou
        left join (
            SELECT * from pl_performance_loan_apply_fact where date(coalesce(loan_issue_time,loan_issue_fail_time)) = curdate()
        )isu on DATE_FORMAT(isu.loan_issue_time, '%H:%i:%s') >= hou.`time` and DATE_FORMAT(isu.loan_issue_time, '%H:%i:%s') < hou.due_time
        join (select time from dim_hour where start_time <= DATE_FORMAT(now() , '%H:%i:%s') and DATE_FORMAT(now(), '%H:%i:%s') < end_time) dim on hou.time <= dim.time
        group by hou.time
        order by hou.time
    )isu
)isu on isu.time = hou.time
;
 
- cumulative loan amount, according to the company, the time classification
select isu.company
    ,hou.time
 ,isu.loan_contract_amt
 ,isu.accum_loan_contract_amt
    ,isu.issue_cost_time
    ,isu.issue_cost_time_v2
    ,isu.issue_finish_cnt
from dim_hour hou
left join (
    select
        isu.loan_contract_amt
        ,isu.issue_finish_cnt
        ,coalesce(isu.issue_cost_time,0) as issue_cost_time
      
  ,case
      WHEN @company IS NULL THEN @accum_loan_contract_amt:=isu.loan_contract_amt
            WHEN @company != company2 THEN @accum_loan_contract_amt:=isu.loan_contract_amt
            ELSE @accum_loan_contract_amt:=@accum_loan_contract_amt + isu.loan_contract_amt END
            AS accum_loan_contract_amt 
        ,coalesce(isu.issue_cost_time_v2,0) as issue_cost_time_v2
  ,@company:=isu.company2 as company
    ,isu.time  
    from (select @company:=null,@accum_loan_contract_amt:=null)s, (
        select cpy.company,cpy.company as company2
            ,hou.time
            ,sum(case when date(isu.loan_issue_time) = curdate() then isu.loan_contract_amt else 0 end) as loan_contract_amt
            ,count(case when date(isu.loan_issue_submit_time) = curdate() then isu.split_order_id else null end) as issue_finish_cnt
            ,sum(case
                 when date(coalesce(isu.loan_issue_time,isu.loan_issue_fail_time)) = curdate()
                     then timestampdiff(second,isu.loan_issue_submit_time, coalesce(isu.loan_issue_time,isu.loan_issue_fail_time))
                 else null end) as issue_cost_time
             ,sum(case
                 when date(coalesce(isu.loan_issue_time,isu.loan_issue_fail_time)) = curdate()
                     then timestampdiff(second,isu.loan_issue_submit_time, coalesce(isu.loan_issue_time,isu.loan_issue_fail_time))
             when date(isu.loan_issue_submit_time)= curdate() and isu.loan_issue_time is null and isu.loan_issue_fail_time is null
                 then timestampdiff(second,isu.loan_issue_submit_time, now())
                 else null end) as issue_cost_time_v2
        from dim_hour hou
        left join (
            SELECT * from pl_performance_loan_apply_fact where date(coalesce(loan_issue_time,loan_issue_fail_time)) = curdate()
        )isu on DATE_FORMAT(isu.loan_issue_time, '%H:%i:%s') >= hou.`time` and DATE_FORMAT(isu.loan_issue_time, '%H:%i:%s') < hou.due_time
        join (select time from dim_hour where start_time <= DATE_FORMAT(now() , '%H:%i:%s') and DATE_FORMAT(now(), '%H:%i:%s') < end_time) dim on hou.time <= dim.time
  join pl_customer_basic_info usb on isu.user_id=usb.user_id
  join dim_company_app cpy on usb.merchant_no=cpy.merchant_no
        group by cpy.company,hou.time
        order by cpy.company,hou.time
    )isu
)isu on isu.time = hou.time
;

Guess you like

Origin www.cnblogs.com/dlp-527/p/11302552.html
I
"I"
I: