数据分析师笔试题 | 真题实战③
The following article is from 求知鸟 Author 求知鸟
讨论
1、我们把用户对商品的评分用稀疏向量表示,保存在数据库表t里面:t的字段有:uid,goods_id,star;uid是用户id;goods_id是商品id;star是用户对该商品的评分,值为1-5。现在我们想要计算向量两两之间的内积,内积在这里的语义为:对于不同的用户,如果他们都对同样的一批商品打了分,那么对于这里面的每个人的分数乘起来,并对这些乘积求和。比如g0:2*2+2*4+2*5+4*2+4*4+4*5+5*2+5*4+5*5=121
建表语句:
create table B(uid VARCHAR(12),good_id VARCHAR(12),star int(10));
INSERT INTO B (uid,good_id,star) VALUES('u0','g0',2),('u0','g1',3),('u1','g0',4),('u1','g1',3),('u2','g0',5),('u2','g1',1);
表结构如下:
select t.good_id,sum(xiangchen)
from (
select t1.uid,t1.good_id,t1.star*t2.star as xiangchen --3*6
from (
select *
from B)t1
inner join (
select * from B)t2
on t1.good_id=t2.good_id
)t
group by t.good_id;
2、员工表,宿舍表,部门表,统计出宿舍楼各部门人数表
设员工表为employee,字段为id,employee_name,belong_dormitory_id,belong_department_id;
宿舍表为dormitory,字段为id,dormitory_number;
部门表为department,字段为id,department_name
select t3.dormitory_name,t2.department_number,count(distinct t1.employee_name)
from (
select *
from employee
)t1
inner join (
select *
from department
)t2
on t1.belong_department_id=t2.id
inner join (
select *
from dormitory
)t3
on t3.id=t1.belong_dormitory_id
group by t3.dormitory_name,t2.department_number
create table order_record (id int, user_id int,paid_time datetime,amount bigint);
insert into order_record(id,user_id,paid_time,amount) values(1,1028898,'2018-01-04 22:32:07.0',9600);
insert into order_record(id,user_id,paid_time,amount) values(2,1030621,'2018-02-24 23:04:58.0',5400);
Select date(paid_time),amount
From order_record
Where month(paid_time)='07'
Group by date(paid_time)
Order by count(distinct user_id) desc
Limit 3
Select t2.id,t2.amount
From (
Select user_id,max(paid_time) as max_date
From order_record
Where datediff(now(),paid_time)=1
Group by user_id
)t1
Inner join (
Select user_id,amount,id,paid_time
From order_record
Where datediff(now(),paid_time)=1
)t2
On t1.user_id=t2.user_id
And t1.max_date=t2.paid_time
后台回复「进群」,加入读者交流群~
点击「积分」,了解积分规则~