案例背景:公司数据储存在N张表中,N是大数,现在需要求每个类别某类数据topN用户的数据
代码结果:创建存储过程将数据从多个相同表中查询求topN并存入,
本文主要内容
知识点
分组查询topN问题 https://editor.csdn.net/md/?articleId=126635132
创建存储过程
案例
存储过程知识点介绍
mysql存储过程文档:
https://www.runoob.com/w3cnote/mysql-stored-procedure.html
案例
建立数据集
#创建表
create table orders1 (
orderid int(11) not null primary key ,
Itemid varchar(30) not null ,
category varchar(10) not null ,
orderdate datetime not null ,
sales float not null );#创建表
create table orders2 like orders1;
create table orders3 like orders1;
create table orders4 like orders1;
create table orders5 like orders1;
#插入数据
insert into orders1 (orderid, Itemid, category, orderdate, sales)
values ('1','k1','A','2020-1-2','459.5'),
('2','k2','A','2020-2-2','345.4'),
('3','k1','A','2020-1-7','47'),
('4','k3','C','2020-1-21','678'),
('5','k4','B','2020-5-2','345'),
('6','k5','C','2020-3-12','654'),
('7','k4','B','2020-4-25','464'),
('8','k2','A','2020-5-28','632'),
('9','k3','C','2020-7-20','98'),
('10','k5','C','2020-4-28','455.6'),
('11','k1','A','2020-3-27','459.7'),
('12','k2','A','2020-2-23','776.6'),
('13','k4','B','2020-2-12','759'),
('14','k5','C','2020-8-2','999'),
('15','k2','A','2020-9-21','599'),
('16','k3','C','2020-10-21','433'),
('17','k4','B','2020-10-28','232'),
('18','k5','C','2020-10-3','124'),
('19','k3','C','2020-6-6','321.4'),
('20','k4','B','2020-9-11','788.6');
insert into orders2 (orderid, Itemid, category, orderdate, sales)
values ('21','k1','A','2020-1-3','954.5'),
('22','k2','A','2020-2-4','435.4'),
('23','k1','A','2020-1-8','74'),
('24','k3','C','2020-7-5','78'),
('25','k4','B','2020-5-26','35'),
('26','k5','C','2020-3-10','54'),
('27','k4','B','2020-4-29','44'),
('28','k2','A','2020-5-24','32'),
('29','k3','C','2020-7-7','98'),
('30','k5','C','2020-4-6','45.6'),
('31','k1','A','2020-3-23','49.7'),
('32','k2','A','2020-2-1','76.6'),
('33','k4','B','2020-2-18','759'),
('34','k5','C','2020-8-29','99'),
('35','k2','A','2020-9-29','59'),
('36','k3','C','2020-10-25','43'),
('37','k4','B','2020-10-28','22'),
('38','k5','C','2020-10-3','14'),
('39','k3','C','2020-6-10','31.4'),
('40','k4','B','2020-9-11','78.6');
insert into orders3 (orderid, Itemid, category, orderdate, sales)
values ('41','k1','A','2020-1-3','954.5'),
('42','k2','A','2020-2-4','435.4'),
('43','k1','A','2020-1-8','674'),
('44','k3','C','2020-7-5','478'),
('45','k4','B','2020-5-26','635'),
('46','k5','C','2020-3-10','654'),
('47','k4','B','2020-4-29','644'),
('48','k2','A','2020-5-24','632'),
('49','k3','C','2020-7-7','98'),
('50','k5','C','2020-4-6','645.6'),
('51','k1','A','2020-3-23','49.7'),
('52','k2','A','2020-2-1','676.6'),
('53','k4','B','2020-2-18','759'),
('54','k5','C','2020-8-29','99'),
('55','k2','A','2020-9-29','459'),
('56','k3','C','2020-10-25','443'),
('57','k4','B','2020-10-28','522'),
('58','k5','C','2020-10-3','614'),
('59','k3','C','2020-6-10','31.4'),
('60','k4','B','2020-9-11','78.6');
insert into orders4 (orderid, Itemid, category, orderdate, sales)
values ('61','k1','A','2020-1-3','954.5'),
('62','k2','A','2020-2-4','4435.4'),
('63','k1','A','2020-1-8','874'),
('64','k3','C','2020-7-5','778'),
('65','k4','B','2020-5-26','535'),
('66','k5','C','2020-3-10','754'),
('67','k4','B','2020-4-29','644'),
('68','k2','A','2020-5-24','932'),
('69','k3','C','2020-7-7','898'),
('70','k5','C','2020-4-6','645.6'),
('71','k1','A','2020-3-23','449.7'),
('72','k2','A','2020-2-1','376.6'),
('73','k4','B','2020-2-18','759'),
('74','k5','C','2020-8-29','399'),
('75','k2','A','2020-9-29','159'),
('76','k3','C','2020-10-25','343'),
('77','k4','B','2020-10-28','222'),
('78','k5','C','2020-10-3','114'),
('79','k3','C','2020-6-10','231.4'),
('80','k4','B','2020-9-11','178.6');
insert into orders5 (orderid, Itemid, category, orderdate, sales)
values ('81','k1','A','2020-1-3','1954.5'),
('82','k2','A','2020-2-4','1435.4'),
('83','k1','A','2020-1-8','174'),
('84','k3','C','2020-7-5','178'),
('85','k4','B','2020-5-26','35'),
('86','k5','C','2020-3-10','154'),
('87','k4','B','2020-4-29','144'),
('88','k2','A','2020-5-24','132'),
('89','k3','C','2020-7-7','198'),
('90','k5','C','2020-4-6','145.6'),
('91','k1','A','2020-3-23','149.7'),
('92','k2','A','2020-2-1','176.6'),
('93','k4','B','2020-2-18','1759'),
('94','k5','C','2020-8-29','199'),
('95','k2','A','2020-9-29','159'),
('96','k3','C','2020-10-25','143'),
('97','k4','B','2020-10-28','122'),
('98','k5','C','2020-10-3','114'),
('99','k3','C','2020-6-10','131.4'),
('100','k4','B','2020-9-11','178.6');
创建存储过程
创建一个表data来储存查询结果数据集
create table data (category varchar(10) not null ,orderid int(11) not null primary key ,
Itemid varchar(30) not null ,
sales float not null
);
#如果你需要调试存储过程一定要加这句代码
drop procedure if exists insert_odata;
存储过程主体
delimiter ;;
create procedure insert_odata (IN i int)
begin
DECLARE i int;
set i=1;
while i<=5 do
set @sqlStr:=CONCAT('insert into data (select category, orderid,Itemid,sales from(
select category,orderid,Itemid,sales,
row_number()over(partition by category order by category,sales desc ) as ranking
from orders',i,') t1 where ranking <=2);');
prepare stmt from @sqlStr;
execute stmt;
deallocate prepare stmt;
set i=i+1;
end while
end
;;
delimiter ;
调用存储过程,并查看数据结果
call insert_odata()
select * from data;
最后求所有数据每个category,sales的前两名
select category,orderid,Itemid,sales from
(select category,orderid,Itemid,sales,
row_number()over(partition by category,Itemid order by category,sales desc ) as ranking
from data) t1 where ranking <=2
);
我在第一次写的时候,因为不知道在MySQL中使用循环需要将其放入存储过程,我的思路是将表名作为变量创建存储过程,然后再通过循环调用存储过程,导致第一个方案用了两个存储过程。
create table data (category varchar(10) not null ,orderid int(11) not null primary key ,
Itemid varchar(30) not null ,
sales float not null
);
#第一个存储过程
#用表作为变量
drop procedure if exists insert_odata;
delimiter ;;
create procedure insert_odata (IN temp varchar(30))
begin
set @tempStr:=CONCAT('insert into data (select category,orderid,Itemid,sales from(
select category,orderid,Itemid,sales,
row_number()over(partition by category order by category,sales desc ) as ranking
from ',temp,') t1 where ranking <=2);');
prepare stmt from @tempStr;
execute stmt;
deallocate prepare stmt;
end
;;
delimiter ;
#第二个 存储过程
drop procedure if exists run;
delimiter $$
create procedure run()
begin
DECLARE i int;
DECLARE Tname varchar(25);
set i=1;
while i<=5 do
set Tname =concat('orders',i);
call insert_odata(Tname);
set i=i+1;
end while;
end
$$
delimiter ;
call run();
select * from data;