使用MySQL進(jìn)行銷售數(shù)據(jù)分析(練習(xí))

數(shù)據(jù)說(shuō)明

由兩張表組成,其中orderinfo表記錄了3月-5月的訂單情況,字段包括orderid变抽、userid、ispaid(支付狀態(tài))氮块、price和paidtime(支付時(shí)間)绍载,共539411行。

userinfo記錄了所有用戶情況滔蝉,字段包括userid击儡、sex和birth,共101535行蝠引。

分析問題

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)了多少額度

導(dǎo)入處理

  1. 在數(shù)據(jù)庫(kù)中建表
create table orderinfo(
orderid    int   primary key not null ,
userid      int,
isPaid      varchar(10),
price        float,
paidTime   varchar(30));

create table userinfo(
    userid   int primary key,
    sex     varchar(10),
    birth    date);

2.從cvs導(dǎo)入數(shù)據(jù)到mysql

load data local  infile 'C:/Users/47207/Desktop/order_info_utf.csv' into table orderinfo  fields terminated by ',';
load data local  infile 'C:/Users/47207/Desktop/user_info_utf.csv' into table userinfo  fields terminated by ',';

注:這里fields terminated by ','是指文件內(nèi)容按‘逗號(hào)為分割填入sql表中

  1. 觀察數(shù)據(jù)茧痒,將數(shù)據(jù)格式轉(zhuǎn)化為統(tǒng)一的sql日期格式


    image.png

1、先把時(shí)間格式標(biāo)準(zhǔn)化為 1993-02-27
update orderinfo set paidtime=replace(paidtime,'/','-') where paidtime is not null;
2融蹂、然后更新字符串為日期格式旺订,然后才能使用日期函數(shù)進(jìn)行操作,
update orderinfo set paidtime=str_to_date(paidtime,'%Y-%m-%d %H:%i') where paidtime is not null;
預(yù)處理完成如下:

image.png

問題解決

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

select DATE_FORMAT(paidtime,'%Y-%M') as month, count(distinct userid)
from orderinfo
where ispaid='已支付'
group by month;
image.png

2.統(tǒng)計(jì)用戶三月份的回購(gòu)率復(fù)購(gòu)率
這兩個(gè)指標(biāo)邏輯不同:
(1).回購(gòu)率是三月購(gòu)買的userid四月依舊購(gòu)買超燃。

select count(distinct userid)
from orderinfo 
where month(paidtime)=4 and ispaid='已支付' and userid in (
select userid
from orderinfo
where ispaid='已支付' and month(paidtime)=3
group by userid);

上述語(yǔ)句得出了4月依舊購(gòu)買的人數(shù)区拳,除以3月購(gòu)買人數(shù)即可
image.png

方法二:

select count(b.april_user)/count(a.march_user)
from (select userid as march_user
from orderinfo
where isPaid='已支付' and month(paidTime)=3
group by userid) a left join (select userid as april_user
from orderinfo
where isPaid='已支付' and month(paidTime)=4
group by userid) b on a.march_user=b.april_user;
image.png

每個(gè)月的復(fù)購(gòu)率可以如下計(jì)算:

 select t1.m,count(t1.m),count(t2.m),count(t2.m)/count(t1.m) from 
    (select userid,date_format(paidtime,"%Y-%m-01") m from orderinfo where isPaid = "已支付" group by userid,date_format(paidtime,"%Y-%m-01")) t1
    left join 
    (select userid,date_format(paidtime,"%Y-%m-01") m from orderinfo where isPaid = "已支付" group by userid,date_format(paidtime,"%Y-%m-01")) t2
    on t1.userid = t2.userid and t2.m=date_add(t1.m,interval 1 month) group by t1.m;
image.png

(2).復(fù)購(gòu)率是三月所有消費(fèi)者中,購(gòu)買2次及以上消費(fèi)者的比例意乓。

select count(purchase_num), SUM(IF(purchase_num>1,1,NULL)), SUM(IF(purchase_num>1,1,NULL))/count(purchase_num) as rate
FROM (select userid, count(*) as purchase_num 
from orderinfo
where ispaid='已支付' and month(paidtime)=3
group by userid) a;
image.png

3.統(tǒng)計(jì)男女用戶的消費(fèi)頻次是否有差異(平均消費(fèi)次數(shù))

select u.sex, count(o.userid)/count(distinct u.userid)
from orderinfo o, userinfo u
where o.userid=u.userid and o.isPaid='已支付' and u.sex <> ''
group by u.sex;
image.png

4.統(tǒng)計(jì)多次消費(fèi)的用戶樱调,第一次和最后一次消費(fèi)間隔是多少约素?

select userid, min(paidtime), max(paidtime), DATEDIFF(max(paidtime),min(paidtime))
from orderinfo
where ispaid='已支付'
group by userid
having count(*)>1
order by userid asc;
image.png

5.統(tǒng)計(jì)不同年齡段,用戶的消費(fèi)金額是否有差異笆凌?
思路:去除有問題日期數(shù)據(jù)后圣猎,將年齡分段(ceil函數(shù)向上取整)。再求出每個(gè)用戶的年齡段乞而、訂單數(shù)送悔、總金額。最后求出不同年齡段的訂單平均金額爪模。

select a.age_level, sum(a.sm)/sum(a.ct)
from 
(select u.userid, u.age_level, count(u.userid) as ct, sum(price) as sm
from orderinfo o join
(select userid, CEIL((year(now())-year(birth))/10) as age_level
from userinfo
where birth > '1901-00-00') u on o.userid=u.userid
where o.isPaid='已支付'
group by u.userid, u.age_level) a
group by a.age_level
order by a.age_level asc;
image.png

6.統(tǒng)計(jì)消費(fèi)的二八法則欠啤,消費(fèi)的top20%用戶,貢獻(xiàn)了多少額度
思路:先求出總消費(fèi)金額和用戶數(shù)屋灌。

select count(*), sum(a.sum_p)
from(
select userid, sum(price) as sum_p
from orderinfo
where isPaid='已支付'
group by userid
order by sum(price) desc) a;
image.png

前百分之20%的用戶為17130

select count(*), sum(a.sum_p)
from(
select userid, sum(price) as sum_p
from orderinfo
where isPaid='已支付'
group by userid
order by sum(price) desc
limit 17130) a;
image.png

貢獻(xiàn)了額度的272201811.46/318501846.54=85%洁段,基本符合二八定律。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末共郭,一起剝皮案震驚了整個(gè)濱河市祠丝,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌除嘹,老刑警劉巖写半,帶你破解...
    沈念sama閱讀 212,080評(píng)論 6 493
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異憾赁,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)散吵,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,422評(píng)論 3 385
  • 文/潘曉璐 我一進(jìn)店門龙考,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人矾睦,你說(shuō)我怎么就攤上這事晦款。” “怎么了枚冗?”我有些...
    開封第一講書人閱讀 157,630評(píng)論 0 348
  • 文/不壞的土叔 我叫張陵缓溅,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我赁温,道長(zhǎng)坛怪,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 56,554評(píng)論 1 284
  • 正文 為了忘掉前任股囊,我火速辦了婚禮袜匿,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘稚疹。我一直安慰自己居灯,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,662評(píng)論 6 386
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著怪嫌,像睡著了一般义锥。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上岩灭,一...
    開封第一講書人閱讀 49,856評(píng)論 1 290
  • 那天拌倍,我揣著相機(jī)與錄音,去河邊找鬼川背。 笑死贰拿,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的熄云。 我是一名探鬼主播膨更,決...
    沈念sama閱讀 39,014評(píng)論 3 408
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼缴允!你這毒婦竟也來(lái)了荚守?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,752評(píng)論 0 268
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤练般,失蹤者是張志新(化名)和其女友劉穎矗漾,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體薄料,經(jīng)...
    沈念sama閱讀 44,212評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡敞贡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,541評(píng)論 2 327
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了摄职。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片誊役。...
    茶點(diǎn)故事閱讀 38,687評(píng)論 1 341
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖谷市,靈堂內(nèi)的尸體忽然破棺而出蛔垢,到底是詐尸還是另有隱情,我是刑警寧澤迫悠,帶...
    沈念sama閱讀 34,347評(píng)論 4 331
  • 正文 年R本政府宣布鹏漆,位于F島的核電站,受9級(jí)特大地震影響创泄,放射性物質(zhì)發(fā)生泄漏艺玲。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,973評(píng)論 3 315
  • 文/蒙蒙 一鞠抑、第九天 我趴在偏房一處隱蔽的房頂上張望板驳。 院中可真熱鬧,春花似錦碍拆、人聲如沸若治。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,777評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)端幼。三九已至礼烈,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間婆跑,已是汗流浹背此熬。 一陣腳步聲響...
    開封第一講書人閱讀 32,006評(píng)論 1 266
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留滑进,地道東北人犀忱。 一個(gè)月前我還...
    沈念sama閱讀 46,406評(píng)論 2 360
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像扶关,于是被迫代替她去往敵國(guó)和親阴汇。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,576評(píng)論 2 349

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

  • 一节槐、創(chuàng)建表搀庶,并將數(shù)據(jù)導(dǎo)入userinfo和orderinfo表中 1.導(dǎo)入數(shù)據(jù): 第一步:建表 create ta...
    那些花兒_0514閱讀 1,052評(píng)論 0 1
  • 數(shù)據(jù)來(lái)源于某網(wǎng)站的銷售統(tǒng)計(jì),主要分為兩部分:1铜异、網(wǎng)絡(luò)訂單數(shù)據(jù)哥倔;2、用戶信息揍庄。數(shù)據(jù)來(lái)源:鏈接:https://pan...
    雅_(tái)2f4f閱讀 535評(píng)論 0 6
  • 數(shù)據(jù)來(lái)源于某網(wǎng)站的銷售統(tǒng)計(jì)咆蒿,主要分為兩部分:1、網(wǎng)絡(luò)訂單數(shù)據(jù)蚂子;2沃测、用戶信息。數(shù)據(jù)來(lái)源:鏈接:https://pan...
    LucasOoo閱讀 960評(píng)論 4 3
  • 數(shù)據(jù)來(lái)源于CDNOW網(wǎng)站的用戶購(gòu)買記錄缆镣,包括用戶ID芽突,購(gòu)買日期试浙,購(gòu)買產(chǎn)品數(shù)董瞻,購(gòu)買金額等四項(xiàng)基本數(shù)據(jù),以下通過Pyt...
    LucasOoo閱讀 4,212評(píng)論 5 13
  • 在data下面新建兩張order和user表田巴,將原數(shù)據(jù)導(dǎo)入mysql钠糊,csv的格式以逗號(hào)分隔。load data ...
    Jarvan_c806閱讀 740評(píng)論 0 0