MySQL新客午夜通話占比和間隔

1.拿出發(fā)標時間在2018年4月至2018年6月成交的新客客戶,區(qū)分M站與APP,計算他們的年齡倒淫,性別碉咆,1期30+逾期金額
2.計算發(fā)標前一個月內(nèi)抖韩,用戶午夜通話次數(shù)的占比
3.拿出題1中的用戶在發(fā)標前1周的平均通話間隔時長
(邏輯:半夜通話較多的用戶,除特殊工種以外疫铜,有可能近期遇到緊急事件茂浮,需要到處籌錢
發(fā)標前一周頻繁通話,是否是頻繁接到催收電話,或者急用錢到處打電話借錢)

~逾期表 yuqi
userid, listingid, creation_date, principal(本金), duedate_30 (30+逾期金額) , risk_category(發(fā)標類型)
~通話歷史表 tonghualishi
userid席揽,calltime(通話時間)顽馋,duration(通話時長秒)
~身份信息的表 shenfenxinxi
字段:userid, idnumber, reg_time(入表時間)

yuqi

shenfenxinxi

tonghualishi

創(chuàng)建表:

#逾期表 yuqi
create table yuqi (userid int,
                   listingid int,
                   creation_date date,
                   principal varchar(45),
                   duedate_30 varchar(45),
                   risk_category varchar(45))

insert into yuqi values (001,1001,'2018-04-01',2000,0,'非APP新客'),
                        (002,1002,'2018-05-01',4000,0,'APP新客')
#身份信息的表  shenfenxinxi
create table shenfenxinxi (userid int,
                           idnumber varchar(45),
                           reg_time date)
insert into shenfenxinxi values (001,'330621940801000','2017-01-01'),
                                (002,'33062119980101001x','2017-01-01')
#通話歷史表  tonghualishi
create table tonghualishi (userid int,
                           calltime datetime,
                           duration time,
                           inserttime datetime)
insert into tonghualishi values (001,'2018-03-26 02:00:30','00:00:30','2017-01-01 00:00:30'),
                                (001,'2018-03-25 06:00:00','00:00:40','2017-01-01 00:00:30'),
                                (001,'2018-03-29 04:00:00','00:01:20','2017-01-01 00:00:30'),
                                (002,'2018-04-10 04:00:00','00:00:20','2017-01-01 00:00:30'),
                                (002,'2018-04-30 12:00:10','00:00:50','2017-01-01 00:00:30')

解答:
1.拿出發(fā)標時間在2018年4月至2018年6月成交的新客客戶,區(qū)分M站與APP幌羞,計算他們的年齡寸谜,性別,1期30+逾期金額

#1,拿出發(fā)標時間在2018年4月至2018年6月的M站與APP的成交的新客客戶属桦,并且區(qū)分M站與APP
#此處不用status like '%成功%'的原因是熊痴,已經(jīng)取得是逾期表中得數(shù)據(jù),說明都已經(jīng)是有借款成功的
create table t0 as
select userid, listingid, creation_date, principal, duedate_30, 
       case when risk_category='非APP新客' then '新客閃電' else '新客APP' end as cj_type
from yuqi
where creation_date>='2018-04-01' and creation_date<'2018-07-01'
and risk_category in ('非APP新客','APP新客')
t0
#2,計算年齡聂宾,性別
create table t1 as
select userid, listingid, creation_date, principal, duedate_30, cj_type,
       case when length(a.idnumber)=18 then ((date_format(creation_date,'%Y')-substr(idnumber,7,4))
            -case when date_format(creation_date,'%m%d')<substr(idnumber,11,4) then 1 else 0 end)
       else ((date_format(creation_date,'%Y')-concat('19',substr(idnumber,7,2)))
            -case when date_format(creation_date,'%m%d')<substr(idnumber,9,4) then 1 else 0 end)
       end as age,
case when length(a.idnumber)=18 and substring(a.idnumber,-2,1) in ('0','2','4','6','8') then 'female'
     when length(a.idnumber)=18 and substring(a.idnumber,-2,1) not in ('0','2','4','6','8') then 'male'
     when length(a.idnumber)=15 and substring(a.idnumber,-1,1) in ('0','2','4','6','8') then 'female'
     when length(a.idnumber)=15 and substring(a.idnumber,-1,1) not in ('0','2','4','6','8') then 'male' 
     else '其他'
     end as gender
from (select a.*, b.idnumber
      from t0 a
      inner join shenfenxinxi b on a.userid=b.userid) a
t1

2.計算發(fā)標前一個月內(nèi)果善,用戶午夜通話次數(shù)的占比

#1,拿出目標用戶
create table t2 as
select a.userid, a.listingid, a.creation_date, a.principal, a.duedate_30, a.cj_type, b.calltime
from t1 a
inner join tonghualishi b 
on a.userid=b.userid
and datediff(a.creation_date,b.calltime)>0
and datediff(a.creation_date,b.calltime)<=30
and a.creation_date>=inserttime
t2
#2,計算總通話次數(shù)
create table t3 as
select userid, listingid, count(calltime) as zth_cs
from t2
group by userid, listingid
t3
#3,計算通話時間段在2:00至5:00的通話次數(shù)
create table t4 as
select userid, listingid, count(calltime) as wyth_cs
from t2
where substr(calltime,12,5) >='02:00' and substr(calltime,12,5) <='05:00'
group by userid,listingid
t4
#4,計算占比
create table t5 as
select a.userid, a.listingid, a.creation_date, a.principal, a.duedate_30, a.cj_type, (coalesce(c.wyth_cs,0)/b.zth_cs) as wy_zb
from t2 a
inner join t3 b 
on a.listingid=b.listingid 
left join t4 c 
on a.listingid=c.listingid
where b.zth_cs>0
t5

3.拿出題1中的用戶在發(fā)標前1周的平均通話間隔時長

#1,拿出發(fā)標前1周的通話記錄
create table t6 as
select a.userid, a.listingid, a.creation_date, b.calltime, b.duration
from t1 a
inner join tonghualishi b 
on a.userid=b.userid
and datediff(a.creation_date,b.calltime)>0
and datediff(a.creation_date,b.calltime)<=7
and a.creation_date>=b.inserttime
t6
#2,將用戶的通話記錄按照通話時間進行排序
create table t7 as
select a.*, 
 (select count(*) from t6 b where a.listingid=b.listingid and a.calltime>=b.calltime) as r
from t6 a
t7
#3,相鄰的兩個通話,上一個加上通話時間亏吝,然后進行相減岭埠,求出通話間隔;計算平均值
#內(nèi)嵌部分
select a.userid, a.listingid, a.calltime as t1, b.calltime as t2, a.duration
from t7 a
inner join t7 b 
on a.listingid=b.listingid 
and a.r = b.r-1
#求平均之前
select userid, listingid, unix_timestamp(t2)-(unix_timestamp(t1)+duration)
from (select a.userid, a.listingid, a.calltime as t1, b.calltime as t2, a.duration
      from t7 a
      inner join t7 b 
      on a.listingid=b.listingid 
      and a.r = b.r-1) a
#最終組合結(jié)果
create table t8 as
select userid, listingid, avg( unix_timestamp(t2)-(unix_timestamp(t1)+duration) ) as thjg_avg
from (select a.userid, a.listingid, a.calltime as t1, b.calltime as t2, a.duration
      from t7 a
      inner join t7 b 
      on a.listingid=b.listingid 
      and a.r = b.r-1) a
嵌套部分

求平均之前

t8
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市蔚鸥,隨后出現(xiàn)的幾起案子惜论,更是在濱河造成了極大的恐慌,老刑警劉巖止喷,帶你破解...
    沈念sama閱讀 218,858評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件馆类,死亡現(xiàn)場離奇詭異,居然都是意外死亡弹谁,警方通過查閱死者的電腦和手機乾巧,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,372評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來预愤,“玉大人沟于,你說我怎么就攤上這事≈部担” “怎么了旷太?”我有些...
    開封第一講書人閱讀 165,282評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長销睁。 經(jīng)常有香客問我供璧,道長,這世上最難降的妖魔是什么冻记? 我笑而不...
    開封第一講書人閱讀 58,842評論 1 295
  • 正文 為了忘掉前任睡毒,我火速辦了婚禮,結(jié)果婚禮上冗栗,老公的妹妹穿的比我還像新娘演顾。我一直安慰自己供搀,他們只是感情好,可當我...
    茶點故事閱讀 67,857評論 6 392
  • 文/花漫 我一把揭開白布偶房。 她就那樣靜靜地躺著趁曼,像睡著了一般军浆。 火紅的嫁衣襯著肌膚如雪棕洋。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,679評論 1 305
  • 那天乒融,我揣著相機與錄音掰盘,去河邊找鬼。 笑死赞季,一個胖子當著我的面吹牛愧捕,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播申钩,決...
    沈念sama閱讀 40,406評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼次绘,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了撒遣?” 一聲冷哼從身側(cè)響起邮偎,我...
    開封第一講書人閱讀 39,311評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎义黎,沒想到半個月后禾进,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,767評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡廉涕,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,945評論 3 336
  • 正文 我和宋清朗相戀三年泻云,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片狐蜕。...
    茶點故事閱讀 40,090評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡宠纯,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出层释,到底是詐尸還是另有隱情婆瓜,我是刑警寧澤,帶...
    沈念sama閱讀 35,785評論 5 346
  • 正文 年R本政府宣布湃累,位于F島的核電站勃救,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏治力。R本人自食惡果不足惜蒙秒,卻給世界環(huán)境...
    茶點故事閱讀 41,420評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望宵统。 院中可真熱鬧晕讲,春花似錦覆获、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,988評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至勤婚,卻和暖如春摹量,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背馒胆。 一陣腳步聲響...
    開封第一講書人閱讀 33,101評論 1 271
  • 我被黑心中介騙來泰國打工缨称, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人祝迂。 一個月前我還...
    沈念sama閱讀 48,298評論 3 372
  • 正文 我出身青樓睦尽,卻偏偏與公主長得像,于是被迫代替她去往敵國和親型雳。 傳聞我的和親對象是個殘疾皇子当凡,可洞房花燭夜當晚...
    茶點故事閱讀 45,033評論 2 355

推薦閱讀更多精彩內(nèi)容