電商交易項目案例
--字段含義--
Sdate定義了日期的分類,將天天分別賦予所屬的月份、星期、季度等屬性,
字段分別爲日期、年月、年、月、日、周幾、第幾周、季度、旬、半月;
Stock定義了訂單表頭,字段分別爲訂單號、交易位置、交易日期;
StockDetail文件定義了訂單明細,該表和Stock以交易號進行關聯,
字段分別爲訂單號、行號、貨品、數量、價格、金額;算法
--建立sdate表--
CREATE TABLE sdate(
dateID string, --日期
theyearmonth string,--年月
theyear string,--年
themonth string,--月
thedate string,--日
theweek string,--周幾
theweeks string,--第幾周
thequot string,--季度
thetenday string,--旬
thehalfmonth string --半月
)
ROW FORMAT DELIMITED FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n' ;函數
--建立stock表--
CREATE TABLE stock(
ordernumber string,--訂單號
locationid string,--交易位置
dateID string --日期
)
ROW FORMAT DELIMITED FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n' ;qt
--建立stockdetail表--
CREATE TABLE stockdetail(
ordernumber string,--訂單號
rownum int,--行號
itemid string, --貨品
qty int, --數量
price int, --價格
amount int --總金額
) ROW FORMAT DELIMITED FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n' ;string
建立表,導入數據
load data local inpath '/home/lan/sdate.txt'
overwrite into table sdate;it
load data local inpath '/home/lan/stock.txt'
overwrite into table stock;io
load data local inpath '/home/lan/stockdetail.txt'
overwrite into table stockdetail;table
一、計算全部訂單每一年的總金額
算法分析:
要計算全部訂單每一年的總金額,首先須要獲取全部訂單的訂單號、訂單日期和訂單金信息,
而後把這些信息和日期表進行關聯,
獲取年份信息,最後根據這四個列按年份歸組統計獲取全部訂單每一年的總金額。
select a.theyear,sum(c.amount) from sdate a,stock b,stockdetail c where a.dateID=b.dateID and b.ordernumber=c.ordernumber group by a.theyear;電商
二、計算全部訂單每一年最大金額訂單的銷售額
算法分析:
該算法分爲兩步:
1)按照日期和訂單號進行分組計算,
獲取全部訂單天天的銷售數據;date
select a.dateid, a.ordernumber,sum(b.amount) as sumofamount
from stock a,stockdetail b
where a.ordernumber=b.ordernumber
group by a.dateid,a.ordernumber;select
2)把第一步獲取的數據和日期表進行關聯獲取的年份信息,
而後按照年份進行分組,使用Max函數,獲取全部訂單每一年最大金額訂單的銷售額。
select c.theyear,max(d.sumofamount) from sdate c,
(select a.dateid, a.ordernumber,sum(b.amount) as sumofamount
from stock a,stockdetail b
where a.ordernumber=b.ordernumber
group by a.dateid,a.ordernumber)d
where c.dateid=d.dateid
group by c.theyear order by c.theyear;
三、統計全部訂單中季度銷售額前10位
select c.theyear,c.thequot,sum(b.amount) as sumofamount
from stock a,stockdetail b,sdate c
where a.ordernumber=b.ordernumber and a.dateid=c.dateid
group by c.theyear,c.thequot
order by sumofamount desc limit 10;
四、列出銷售金額在100000以上的單據(訂單號)
select a.ordernumber,sum(b.amount) as sumofamount
from stock a,stockdetail b
where a.ordernumber=b.ordernumber
group by a.ordernumber
having sumofamount>100000;
五、全部訂單中每一年最暢銷貨品
第一步:
統計出每一年每種貨品的銷售總金額
stock a,stockdetail b,sdate c
===================================
select c.theyear,b.itemid,sum(b.amount) as sumofamount
from stock a,stockdetail b,sdate c
where a.ordernumber=b.ordernumber and a.dateid=c.dateid
group by c.theyear,b.itemid;
第二步: 在第一步的數據上,統計出每一年最大的銷售總金額 將第一步的數據集起別名爲d; select d.theyear,max(sumofamount) as maxofamount from (select c.theyear,b.itemid,sum(b.amount) as sumofamount from stock a,stockdetail b,sdate c where a.ordernumber=b.ordernumber and a.dateid=c.dateid group by c.theyear,b.itemid) d group by d.theyear; 第三步:全部訂單中每一年最暢銷貨品 e:每一年每種貨品的銷售總金額 f:每一年最大的銷售總金額 select distinct e.theyear,e.itemid,f.maxofamount from (select c.theyear,b.itemid, sum(b.amount) as sumofamount from stock a,stockdetail b,sdate c where a.ordernumber=b.ordernumber and a.dateid=c.dateid group by c.theyear,b.itemid) e, (select d.theyear,max(d.sumofamount) as maxofamount from (select c.theyear,b.itemid,sum(b.amount) as sumofamount from stock a,stockdetail b,sdate c where a.ordernumber=b.ordernumber and a.dateid=c.dateid group by c.theyear,b.itemid) d group by d.theyear) f where e.theyear=f.theyear and e.sumofamount=f.maxofamount order by e.theyear;