27,579
社区成员
发帖
与我相关
我的任务
分享
select * from
(
select c.drug_code,
c.drugname,
c.name,
case when d.number<=c.t then c.order_max
else c.y end 'amount'
from
(select a.drug_code,a.drugname,a.name,b.order_max,
a.amount/b.order_max 't',
a.amount%b.order_max 'y'
from atf_ypxx a
inner join atf_cas b on a.drug_code=b.drug_code) c
cross join master.dbo.spt_values d
where d.type='P' and d.number between 1 and c.t+1 )t
where t .amount<>0
create table atf_ypxx
(drug_code int,drugname varchar(10),name varchar(10),amount int)
insert into atf_ypxx
select 1,'aaa','张三',36 union all
select 2,'bbb','张三',36 union all
select 3,'ccc','李四',36 union all
select 4,'eee','王五',19
create table atf_cas
(drug_code int,order_max int)
insert into atf_cas
select 1,10 union all
select 2,10 union all
select 3,10 union all
select 4,3
select c.drug_code,
c.drugname,
c.name,
case when d.number<=c.t then c.order_max
else c.y end 'amount'
from
(select a.drug_code,a.drugname,a.name,b.order_max,
a.amount/b.order_max 't',
a.amount%b.order_max 'y'
from atf_ypxx a
inner join atf_cas b on a.drug_code=b.drug_code) c
cross join master.dbo.spt_values d
where d.type='P' and d.number between 1 and c.t+1
/*
drug_code drugname name amount
----------- ---------- ---------- -----------
1 aaa 张三 10
1 aaa 张三 10
1 aaa 张三 10
1 aaa 张三 6
2 bbb 张三 10
2 bbb 张三 10
2 bbb 张三 10
2 bbb 张三 6
3 ccc 李四 10
3 ccc 李四 10
3 ccc 李四 10
3 ccc 李四 6
4 eee 王五 3
4 eee 王五 3
4 eee 王五 3
4 eee 王五 3
4 eee 王五 3
4 eee 王五 3
4 eee 王五 1
(19 行受影响)
*/
use tempdb
if object_id('atf_ypxx') is not null
drop table atf_ypxx
if object_id('atf_cas') is not null
drop table atf_cas
create table atf_ypxx (drug_code int not null primary key,
drugName varchar(20),
name varchar(20),
amount int)
create table atf_cas(drug_code int,order_max int)
insert into atf_ypxx
select 1,'aaa','张三',36 union all
select 2,'bbb','张三',36 union all
select 3,'ccc','李四',36
insert into atf_cas
select 1,10 union all
select 2,10 union all
select 3, 10
;with sel as(
select a.drug_code,a.drugName,a.name,b.order_max amount,floor(a.amount/b.order_max) as num
from atf_ypxx a
join atf_cas b on a.drug_code=b.drug_code
where a.amount>b.order_max
union all
select a.drug_code,a.drugName,a.name, b.order_max,a.num-1 as num from sel a
join atf_cas b on a.drug_code=b.drug_code
where num>1
)
select drug_code,drugName,name,amount from sel
union all
select a.drug_code,a.drugName,a.name,a.amount%b.order_max
from atf_ypxx a
join atf_cas b on a.drug_code=b.drug_code
where a.amount%b.order_max>0
order by drug_code,amount desc
with atf_ypxx (drug_code,drugname,name,amount) as
(select 1,'aaa','张三',36 union all
select 2,'bbb','张三',36 union all
select 3,'ccc','李四',36 ),
atf_cas(drug_code,order_max) as
(select 1,10 union all
select 2,10 union all
select 3,10 ),
cte1 as
(select t1.drug_code,t1.drugname,t1.name,t2.order_max,t1.amount from atf_ypxx as t1 join atf_cas as t2 on
t1.drug_code=t2.drug_code ),
cte as
(select drug_code,drugname,name,order_max,amount from cte1
union all
select a.drug_code,a.drugname,a.name,a.order_max+b.order_max as order_max,a.amount
from cte1 as a join cte as b on a.drug_code=b.drug_code
where b.order_max< a.amount
)
select c1.drug_code,c1.drugname,c1.name,
case when c1.order_max =(select MAX(order_max) from cte as c3 where c1.drug_code=c3.drug_code)
then c2.amount+c2.order_max-c1.order_max
else c2.order_max
end as amount
from cte as c1 join cte1 as c2 on c1.drug_code=c2.drug_code
order by c1.drug_code,amount desc
with atf_ypxx (drug_code,drugname,name,amount) as
(select 1,'aaa','张三',36 union all
select 2,'bbb','张三',36 union all
select 3,'ccc','李四',36 ),
atf_cas(drug_code,order_max) as
(select 1,10 union all
select 2,10 union all
select 3,10 ),
cte1 as
(select t1.drug_code,t1.drugname,t1.name,t2.order_max,t1.amount from atf_ypxx as t1 join atf_cas as t2 on
t1.drug_code=t2.drug_code ),
cte as
(select drug_code,drugname,name,order_max,amount from cte1
union all
select a.drug_code,a.drugname,a.name,a.order_max+b.order_max as order_max,a.amount
from cte1 as a join cte as b on a.drug_code=b.drug_code
where b.order_max< a.amount
)
select drug_code,drugname,name,
case when order_max =(select MAX(order_max) from cte
as c2 where c2.drug_code=c1.drug_code )
then c1.amount%(select min(order_max) from cte
as c2 where c2.drug_code=c1.drug_code )
else (select min(order_max) from cte
as c2 where c2.drug_code=c1.drug_code)
end as amount
from cte as c1
order by drug_code,amount desc
drug_code drugname name amount
----------- -------- ---- -----------
1 aaa 张三 10
1 aaa 张三 10
1 aaa 张三 10
1 aaa 张三 6
2 bbb 张三 10
2 bbb 张三 10
2 bbb 张三 10
2 bbb 张三 6
3 ccc 李四 10
3 ccc 李四 10
3 ccc 李四 10
3 ccc 李四 6
(12 行受影响)