SQL未知行转列问题

SQL未知行转列问题

问题:找出A省的A药品在2019-2020年的销售情况,输出结果为包含的列名有HospitalId、HospitalName、日期1、日期2…

输出结果如下

HospitalIdHospitalName2019-012019-022019-032019-042019-10
1246.00NULL123.00NULLNULL
2NULLNULLNULL123.00NULL

建表

CREATE TABLE `hospital` (
    `HospitalId` varchar(36) NOT NULL,
    `HospitalName` varchar(100) DEFAULT NULL,
    `ProvinceName` varchar(45) DEFAULT NULL,
    `CityName` varchar(45) DEFAULT NULL,
    `Disabled` bit(1) NULL DEFAULT 0,
    PRIMARY KEY (`HospitalId`)
  ) COMMENT='医院表'
select * from hospital;
CREATE TABLE `product` (
  `ProductId` varchar(36) NOT NULL,
  `ProductName` varchar(100) NULL,
  `Price` decimal(10, 2) NOT NULL COMMENT '单价',
  PRIMARY KEY (`ProductId`)
  ) COMMENT='产品表';
CREATE TABLE `sales` (
  `SalesId` varchar(36) NOT NULL,
  `HospitalId` varchar(36) NOT NULL,
  `Period` date NOT NULL COMMENT '日期yyyy/mm/dd',
  `ProductId` varchar(36) NOT NULL,
  `SalesVolume` decimal(10, 2) NOT NULL COMMENT '销售数量',
  PRIMARY KEY (`SalesId`),
  CONSTRAINT `fk_hospitalid` FOREIGN KEY (`HospitalId`) REFERENCES `hospital` (`HospitalId`),
  CONSTRAINT `fk_productid` FOREIGN KEY (`ProductId`) REFERENCES `product` (`ProductId`)
  ) COMMENT='销量表';

插入数据

insert into `hospital` values(1,'一','A','A',1);
insert into `hospital` values(2,'二','A','B',1);
insert into `hospital` values(3,'三','A','C',0);
insert into `hospital` values(4,'四','A','D',1);
insert into `hospital` values(5,'五','B','A',1);
insert into `hospital` values(6,'六','B','B',0);
insert into `hospital` values(7,'七','B','C',1);
insert into `hospital` values(8,'八','B','D',1);
insert into `product` values(1,'A药品',1.23);
insert into `product` values(2,'B药品',2.23);
insert into `product` values(3,'C药品',3.23);
insert into `product` values(4,'D药品',4.23);
insert into `product` values(5,'E药品',10.23);
insert into `sales` values(1,1,'2019/1/2',1,100);
insert into `sales` values(2,1,'2019/3/7',2,100);
insert into `sales` values(3,2,'2019/1/2',2,100);
insert into `sales` values(4,3,'2019/3/2',2,100);
insert into `sales` values(5,1,'2019/3/2',1,100);
insert into `sales` values(6,1,'2019/2/12',2,100);
insert into `sales` values(7,2,'2019/1/2',2,100);
insert into `sales` values(8,3,'2019/1/2',3,100);
insert into `sales` values(9,2,'2019/4/5',1,100);

创建存储过程

delimiter //
create procedure pro_1()
begin
set @sql=null; 
select group_concat(distinct 
  concat('max(case when date_format(Period,"%Y-%m") = 
  ''',date_format(Period,"%Y-%m"),'''
  then SalesId end) as 
  ''',date_format(Period,"%Y-%m"),'''')) 
  into @sql from sales;
set @sql= concat('select 
  hospital.HospitalId,
  hospital.HospitalName,
  ',@sql,'
  from sales 
  left join product 
  on sales.ProductId=product.ProductId 
  left join hospital 
  on sales.HospitalId=hospital.HospitalId 
  where product.ProductName="A药品" 
  and hospital.ProvinceName="A" 
  and year(Period)>=2018 
  and year(Period)<=2019 
  group by hospital.HospitalName,hospital.HospitalName
  having sum(sales.SalesVolume * product.price) < 100000');

prepare tmp from @sql;
execute tmp;
deallocate prepare tmp;
end//

查看存储过程的结果

call pro_1()//

以上题型是找工作过程中遇见的题目,如有侵权,请联系删除

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值