mysql案例學(xué)習(xí)總結(jié)

案例: 使用mysql 進(jìn)行數(shù)據(jù)分析
老師指路->http://www.reibang.com/u/1f32f227da5f
使用工具:MySQL、Navicat

一、準(zhǔn)備好訂單數(shù)據(jù)和用戶數(shù)據(jù):

a剃斧、打開命令窗口或者 navicat 、workbench 等數(shù)據(jù)庫(kù)軟件
b境输、任意選擇一個(gè)數(shù)據(jù)庫(kù),或者創(chuàng)建一個(gè)新的數(shù)據(jù)庫(kù)并進(jìn)入數(shù)據(jù)庫(kù)

create database data charset utf8;
use data;

c颖系、執(zhí)行以下source 命令嗅剖,注意改成自己的路徑并且不能包含中文

#將.sql文件拖到命令行source后 即可顯示文件路徑
source C:\Users\data\Desktop\Mysql_case\orderinfo.sql
source C:\Users\data\Desktop\Mysql_case\userinfo.sql

注意:后面不需要加分號(hào),上面的命令會(huì)把 **.sql 文件的語(yǔ)句都執(zhí)行一遍

desc orderinfo;--顯示表結(jié)構(gòu)

select *  from orderinfo limit 10; #顯示10條記錄

select  count(*) from orderinfo;#顯示記錄條數(shù)

二嘁扼、題目要求:

1信粮、統(tǒng)計(jì)不同月份的下單人數(shù)
2、統(tǒng)計(jì)用戶三月份的回購(gòu)率和復(fù)購(gòu)率
3偷拔、統(tǒng)計(jì)男女用戶消費(fèi)頻次是否有差異
4蒋院、統(tǒng)計(jì)多次消費(fèi)的用戶亏钩,第一次和最后一次消費(fèi)間隔是多少天
5、統(tǒng)計(jì)不同年齡段欺旧,用戶的消費(fèi)金額是否有差異
6姑丑、統(tǒng)計(jì)消費(fèi)的二八法則,消費(fèi)的top20%用戶辞友,貢獻(xiàn)了多少消費(fèi)額

涉及表:
orderinfo 訂單詳情表
| orderid 訂單id
| userid 用戶id
| isPaid 是否支付
| price 付款價(jià)格
| paidTime 付款時(shí)間

userinfo 用戶信息表
| userid 用戶id
| sex 用戶性別
| birth 用戶出生日期

1栅哀、統(tǒng)計(jì)不同月份的下單人數(shù)

觀察數(shù)據(jù),過濾未支付數(shù)據(jù),去重
不同月份下單人數(shù)

select year(paidTime)as year_ ,
month(paidTime) as month_ ,
count(distinct userid) as cons
from orderinfo 
where isPaid='已支付'
group by year(paidTime),month(paidTime);

2称龙、統(tǒng)計(jì)用戶三月份的回購(gòu)率和復(fù)購(gòu)率

三月份交易量

select count(orderid)as all_order from orderinfo  
where month(paidTime)=3;

三月份下單總?cè)藬?shù)

select 
count(distinct userid) as cons
from orderinfo 
where  month(paidTime)=3;

復(fù)購(gòu)率:當(dāng)月購(gòu)買了多次用戶占當(dāng)月用戶比例
回購(gòu)率:上月購(gòu)買用戶中有多少用戶本月又再次購(gòu)買
(回購(gòu)率:本月購(gòu)買用戶中有多少用戶下個(gè)月又再次購(gòu)買)
復(fù)購(gòu)率:
a留拾、先篩選出3月份消費(fèi)情況

select 
*
from orderinfo
where isPaid='已支付'  and month(paidTime)=3;

b、統(tǒng)計(jì)每個(gè)用戶在3月份消費(fèi)了多少次

select 
userid, count(1) as cons
from orderinfo
where isPaid='已支付'  
and month(paidTime)=3

group by userid;

c鲫尊、對(duì)購(gòu)買次數(shù)進(jìn)行判斷痴柔,統(tǒng)計(jì)出消費(fèi)多次的用戶數(shù)(大于1次)
對(duì)用戶購(gòu)買次數(shù)進(jìn)行判斷,并sum求和疫向,如果購(gòu)買次數(shù)大于1咳蔚,用戶數(shù)加1
復(fù)購(gòu)率:當(dāng)月購(gòu)買了多次用戶占當(dāng)月用戶比例

select 

count(1) as userid_cons,
sum(if (cons>1,1,0)) as fugou_cons,
sum(if (cons>1,1,0))/count(1) as fugou_rate

from(select 
     userid,
     count(1) as cons
    from orderinfo
    where isPaid='已支付'  
    and month(paidTime)=3
    
    group by userid 
    ) a; 

回購(gòu)率:本月購(gòu)買用戶中有多少用戶下個(gè)月又再次購(gòu)買
計(jì)算所有月份回購(gòu)率
舉例:三月份的回購(gòu)率=3月用戶中4月又再次購(gòu)買的人數(shù)/3月用戶總數(shù)
a、統(tǒng)計(jì)每年每月的用戶消費(fèi)情況

#分組:每個(gè)月 有哪些用戶ID
select 
    userid,
    date_format(paidTime,'%Y-%m-01') as month_dt,
    count(1)  as cons

from orderinfo
where isPaid='已支付'
group by userid,date_format(paidTime,'%Y-%m-01');

b搔驼、相鄰月份 進(jìn)行關(guān)聯(lián)谈火,能關(guān)聯(lián)上的用戶為回購(gòu)
自己與自己關(guān)聯(lián),用戶對(duì)應(yīng)舌涨,月份對(duì)應(yīng)
此處使用left join 糯耍,保留用戶總數(shù);
inner join 會(huì)刪掉關(guān)聯(lián)不上的用戶囊嘉,三月用戶總數(shù)會(huì)減少

select 
*
from(select 
    userid,
    date_format(paidTime,'%Y-%m-01') as month_dt,
    count(1)  as cons

from orderinfo
where isPaid='已支付'
group by userid,date_format(paidTime,'%Y-%m-01')
) a

left join (select 
    userid,
    date_format(paidTime,'%Y-%m-01') as month_dt,
    count(1)  as cons

from orderinfo
where isPaid='已支付'
group by userid,date_format(paidTime,'%Y-%m-01')


) b on a.userid=b.userid
and date_sub(b.month_dt,interval 1 month)=a.month_dt;

c温技、用統(tǒng)計(jì)出四月份有關(guān)聯(lián)的 用戶數(shù)據(jù)量 除以 三月份用戶數(shù)據(jù)量
count(a.userid)>count(b.userid) 4月份有null情況

select 
    a.month_dt,
    count(a.userid),
    count(b.userid),
    count(b.userid)/ count(a.userid) as '回購(gòu)率'

from(select 
    userid,
    date_format(paidTime,'%Y-%m-01') as month_dt,
    count(1)  as cons

from orderinfo
where isPaid='已支付'
group by userid,date_format(paidTime,'%Y-%m-01')

) a

left join (select 
    userid,
    date_format(paidTime,'%Y-%m-01') as month_dt,
    count(1)  as cons

from orderinfo
where isPaid='已支付'
group by userid,date_format(paidTime,'%Y-%m-01')


) b on a.userid=b.userid
and date_sub(b.month_dt,interval 1 month)=a.month_dt
group  by a.month_dt;

3、統(tǒng)計(jì)男女用戶消費(fèi)頻次是否有差異

獲取用戶 性別 消費(fèi)次數(shù) 男生用戶消費(fèi)總次數(shù)/男生人數(shù)
a哗伯、統(tǒng)計(jì)每個(gè)用戶消費(fèi)次數(shù)荒揣、帶性別,篩選出性別不為空的用戶

select   

a.userid,
sex,
count(1) as cons

from orderinfo a
inner join (select *  from userinfo where sex!='') b
on a.userid=b.userid
group  by a.userid,sex ;

b、對(duì)性別做一個(gè)消費(fèi)次數(shù)平均計(jì)算

select 
    sex,
    avg(cons) as avg_cons
 
from(select   

a.userid,
sex,
count(1) as cons

from orderinfo a
inner join (select *  from userinfo where sex!='') b
on a.userid=b.userid
group  by a.userid,sex 
) c
group  by sex;

4焊刹、統(tǒng)計(jì)多次消費(fèi)的用戶,第一次和最后一次消費(fèi)間隔是多少天

a恳蹲、取出多次消費(fèi)用戶

select 
 userid
from orderinfo
 where isPaid='已支付'
 group by userid
 
 having count(1)>1;

b虐块、取出第一次與第二次購(gòu)物時(shí)間

計(jì)算時(shí)間差

select 
 
 userid,
 
 min(paidTime),
 max(paidTime),
 datediff(max(paidTime),min(paidTime)) as jiange

from orderinfo
where isPaid='已支付'
group by userid
 
having count(1)>1;

5、統(tǒng)計(jì)不同年齡段嘉蕾,用戶的消費(fèi)金額是否有差異

a贺奠、計(jì)算每個(gè)用戶年齡,并對(duì)用戶年齡進(jìn)行分層
0-10:1 11-20:2 21-30:3 31-40 41-50 10歲為一層
ceil 向上取整

select 
userid,
birth,
now(),
ceil(timestampdiff(year,birth,now())/10) as age

from userinfo  
where  birth> '1900-01-01';

b错忱、關(guān)聯(lián)訂單信息儡率,獲取不同年齡段的消費(fèi)頻次和消費(fèi)數(shù)據(jù)

select 
  a.userid,
  age,
  count(1) as cons ,
  sum(price) as prices
from orderinfo a

inner join (
select 
userid,
birth,
now(),
ceil(timestampdiff(year,birth,now())/10) as age

from userinfo  
where  birth> '1900-01-01'

) b
on a.userid=b.userid
group by b.userid,b.age;

c挂据、對(duì)不同年齡層 進(jìn)行聚合 最終得到不同年齡層消費(fèi)情況

select
age,
avg(cons),
avg(prices)

from (
select 
  a.userid,
  age,
  count(1) as cons ,
  sum(price) as prices

from orderinfo a

inner join (
select 
userid,
birth,
now(),
ceil(timestampdiff(year,birth,now())/10) as age

from userinfo  
where  birth> '1900-01-01'

) b

on a.userid=b.userid

group by b.userid,b.age
) c
 
group  by age ;

6、統(tǒng)計(jì)消費(fèi)的二八法則儿普,消費(fèi)的top20%用戶崎逃,貢獻(xiàn)了多少消費(fèi)額

用戶消費(fèi)占比,20%用戶 消費(fèi)貢獻(xiàn)占80%

a眉孩、統(tǒng)計(jì)每個(gè)用戶消費(fèi)金額个绍,并進(jìn)行降序排序

select 
userid,
sum(price) as total_prices

from orderinfo a

where  isPaid='已支付'
group  by userid 
order by total_prices desc ;

b、統(tǒng)計(jì)一共有多少用戶浪汪,以及用戶總消費(fèi)金額

select

count(1) as cons,
sum(total_prices) as all_prices

from ( select 
userid,
sum(price) as total_prices

from orderinfo a

where  isPaid='已支付'
group  by userid  ) b;

c巴柿、取出消費(fèi)前20%用戶,進(jìn)行金額統(tǒng)計(jì)
8萬(wàn)用戶去20%-17000名

select 

count(1) as cons,
sum(total_prices) as all_prices

from (
select 
userid,
sum(price) as total_prices

from orderinfo a

where  isPaid='已支付'
group  by userid  

order by total_prices desc
limit 17000) b;

select (2.7/3.1);

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末死遭,一起剝皮案震驚了整個(gè)濱河市广恢,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌呀潭,老刑警劉巖钉迷,帶你破解...
    沈念sama閱讀 219,270評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異蜗侈,居然都是意外死亡篷牌,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,489評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門踏幻,熙熙樓的掌柜王于貴愁眉苦臉地迎上來枷颊,“玉大人,你說我怎么就攤上這事该面∝裁纾” “怎么了?”我有些...
    開封第一講書人閱讀 165,630評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵隔缀,是天一觀的道長(zhǎng)题造。 經(jīng)常有香客問我,道長(zhǎng)猾瘸,這世上最難降的妖魔是什么界赔? 我笑而不...
    開封第一講書人閱讀 58,906評(píng)論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮牵触,結(jié)果婚禮上淮悼,老公的妹妹穿的比我還像新娘。我一直安慰自己揽思,他們只是感情好袜腥,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,928評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著钉汗,像睡著了一般羹令。 火紅的嫁衣襯著肌膚如雪鲤屡。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,718評(píng)論 1 305
  • 那天福侈,我揣著相機(jī)與錄音酒来,去河邊找鬼。 笑死癌刽,一個(gè)胖子當(dāng)著我的面吹牛役首,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播显拜,決...
    沈念sama閱讀 40,442評(píng)論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼衡奥,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了远荠?” 一聲冷哼從身側(cè)響起矮固,我...
    開封第一講書人閱讀 39,345評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎譬淳,沒想到半個(gè)月后档址,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,802評(píng)論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡邻梆,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,984評(píng)論 3 337
  • 正文 我和宋清朗相戀三年守伸,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片浦妄。...
    茶點(diǎn)故事閱讀 40,117評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡尼摹,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出剂娄,到底是詐尸還是另有隱情蠢涝,我是刑警寧澤,帶...
    沈念sama閱讀 35,810評(píng)論 5 346
  • 正文 年R本政府宣布阅懦,位于F島的核電站和二,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏耳胎。R本人自食惡果不足惜惯吕,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,462評(píng)論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望怕午。 院中可真熱鬧混埠,春花似錦、人聲如沸诗轻。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,011評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)扳炬。三九已至吏颖,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間恨樟,已是汗流浹背半醉。 一陣腳步聲響...
    開封第一講書人閱讀 33,139評(píng)論 1 272
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留劝术,地道東北人缩多。 一個(gè)月前我還...
    沈念sama閱讀 48,377評(píng)論 3 373
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像养晋,于是被迫代替她去往敵國(guó)和親衬吆。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,060評(píng)論 2 355