1.创建数据库
create database db_cart;
2.使用数据库
use db_cart;
3.创建表
create table t_cart(
province STRING,
month INT,
city STRING,
county STRING,
year INT,
cartype STRING,
productor STRING,
brand STRING,
mold STRING,
owner STRING,
nature STRING,
number INT,
ftype STRING,
outv INT,
power DOUBLE,
fuel STRING,
length INT,
width INT,
height INT,
xlength INT,
xwidth INT,
xheight INT,
count INT,
base INT,
front INT,
norm STRING,
tnumber INT,
total INT,
curb INT,
hcurb INT,
passenger STRING,
zhcurb INT,
business STRING,
dtype STRING,
fmold STRING,
fbusiness STRING,
name STRING,
age INT,
sex STRING)
row format delimited
fields terminated by '\t'
location '/cart/log';
4.加载数据
load data local inpath '/root/cars.txt' into table t_cart;
需求:
1.1、通过统计车辆不同用途的数量分布
第一种情况:
select nature,sum(number)
from t_cart
group by nature
having nature is not null
and nature <> '';
第二种情况:
select '非营运' sum(if(nature='非营运',number,0)),
'营运' sum(if(nature<>'非营运',number,0))
from t_cart
where nature is not null and nature <> '';
1.2、统计山西省 2013 年每个月的汽车销售数量的比例
select a.year,a.month,a.counts/b.sums
from
(select year,month,sum(number) counts
from t_cart
group by year,month
having month is not null
and year is not null)a,
(select year,sum(number) sums
from t_cart
where year=2013
group by year)b;
1.3、统计山西省 2013 年各市、区县的汽车销售的分布
select year,city,county,sum(number)
from t_cart
where year=2013
group by city,county
having city is not null and city <> ''
and county is not null and county <> '';
2、用户数据市场分析:
2.1、统计买车的男女比例
性别:
女 0
男 1
select a.sex,a.counts/b.sums
from
(select sex,sum(number) counts
from t_cart
group by sex
having sex is not null and sex <> '') a,
(select sum(number) sums
from t_cart
where sex is not null and sex <> '')b;
2.2、统计的车的所有权、车辆类型和品牌的分布
brand STRING,品牌
mold STRING,车辆类型
owner STRING,所有权
select brand,mold,owner,sum(number)
from t_cart
group by brand,mold,owner
having brand is not null and brand <> ''
and mold is not null and mold <> ''
and owner is not null and owner <> '';
3、不同车型销售统计分析:
3.1、统计不同品牌的车在每个月的销售量分布(数量)
select month,brand,sum(number)
from t_cart
group by brand,month
having brand is not null and brand <> ''
and month is not null;
3.2、通过不同类型(品牌)车销售情况,来统计发动机型号和燃料种类(数量)
ftype STRING,发动机型号
fuel STRING,燃料种类
select brand,count(ftype),count(fuel)
from t_cart
group by brand,ftype,fuel
having brand is not null and brand <> ''
and ftype is not null and ftype <> ''
and fuel is not null and fuel <> '' ;