SQL数据处理:SQL处理excel常用的功能---给新手的建议

无论用哪个工具,能解决问题就是好数据分析师! 不要跟数据分析师谈什么技术实现!


一、关联公式:Vlookup
vlookup是excel几乎最常用的公式,一般用于两个表的关联查询等。所以我先创建一个新表:复制sale表并筛选出地区仅为广州的,命名为sale_guang。
create table sale_guang SELECT * from sale where city="广州";

需求:根据订单明细号关联两表,并且sale_guang只有订单明细号与利润两列

SELECT * from sale a
inner JOIN
(SELECT ordernum,profit from sale_guang) b
on a.`ordernum`=b.`ordernum`;

二、对比两列差异

需求:对比sale的订单明细号与sale_guang订单明细号的差异;

SELECT * from sale a
WHERE a.ordernum not in 
(SELECT b.ordernum from sale_guang b);

三、去除重复值

需求:去除业务员编码的重复值

SELECT * FROM sale
where salesnum not in 
(SELECT salesnum from sale GROUP BY salesman HAVING COUNT(salesnum)>1)

四、缺失值处理

需求:用0填充缺失值或则删除有地区名称缺失值的行。

#用0填充:
update sale set city = 0 where city = NULL
#删除有缺失值的行:
delete from sale where city = NULL;

五、多条件筛选

需求:想知道业务员张爱,在北京区域卖的商品订单金额大于等于6000的信息。

SELECT * from sale 
where salesman = "张爱" 
and city = "北京"
and orderaccount >=6000;

六、 模糊筛选数据

需求:筛选存货名称含有"三星"或则含有"索尼"的信息。

SELECT * from sale 
where inventoryname like "%三星%" or 存货名称 like "%索尼%";

七、分类汇总

需求:北京区域各业务员的利润总额。

SELECT city,sum(`profit`) from sale
WHERE city = "北京"
GROUP BY `city`;

八、条件计算

需求:存货名称含“三星字眼”并且税费高于1000的订单有几个?这些订单的利润总和和平均利润是多少?

#有多少个?
SELECT COUNT(*) from sale 
where inventoryname like "%三星%"
and `tax` > 1000 ;

#这些订单的利润总和和平均利润是多少?
SELECT `ordernum`,SUM(profit),AVG(`profit`) from sale 
where inventoryname like "%三星%"
and `tax` > 1000 
GROUP BY `ordernum`;

九、删除数据间的空格

需求:删除存货名称两边的空格。

SELECT trim(inventoryname) from sale;

十、合并与排序列

需求:计算每个订单号的成本并从高到低排序(成本 = 不含税金额 - 利润)

SELECT city,ordernum,(Nontaxamount - profit) as cost from sale
order by cost DESC;

详细:https://blog.csdn.net/moguxiansheng1106/article/details/44258499

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值