如下:
表A
店铺 调出数
A 10
B 8
C 20 表B
店铺 销售数量 价格 日期
A 3 100 2011-01-01
A 4 200 2011-02-02
A 5 250 2011-03-03
B 10 120 2011-05-05
C 5 130 2011-06-06现在的想要的结果是:查出店铺的销售总和,条件是假如店铺的销售数量总和大于调出数则按调出数来计算并且价格按日期的先后获取价格,如果小于则按销售量计算,结果大概如下店铺 销售总和
A 3*100 + 4*200 + 3*250 价格为250的只取两个
B 8*250
C 5*130求大虾们帮帮忙!
表A
店铺 调出数
A 10
B 8
C 20 表B
店铺 销售数量 价格 日期
A 3 100 2011-01-01
A 4 200 2011-02-02
A 5 250 2011-03-03
B 10 120 2011-05-05
C 5 130 2011-06-06现在的想要的结果是:查出店铺的销售总和,条件是假如店铺的销售数量总和大于调出数则按调出数来计算并且价格按日期的先后获取价格,如果小于则按销售量计算,结果大概如下店铺 销售总和
A 3*100 + 4*200 + 3*250 价格为250的只取两个
B 8*250
C 5*130求大虾们帮帮忙!
set nocount on
declare @表A table (店铺 varchar(1),调出数 int)
insert into @表A
select 'A',10 union all
select 'B',8 union all
select 'C',20declare @表B table (店铺 varchar(1),销售数量 int,价格 int,日期 datetime)
insert into @表B
select 'A',3,100,'2011-01-01' union all
select 'A',4,200,'2011-02-02' union all
select 'A',5,250,'2011-03-03' union all
select 'B',10,120,'2011-05-05' union all
select 'C',5,130,'2011-06-06'select rn=identity(int),a.调出数,b.* into #t
from @表A a left join @表B b on a.店铺=b.店铺 order by b.日期
select 店铺,
销售总和=sum(case when 调出数>=[sum] then 销售数量*价格 when 调出数<[sum]
and [count]=1 then 调出数*价格 when 调出数<[sum] and [count]>1
then (销售数量-([sum]-调出数))*价格 end)
from (select *,[sum]=(select sum(销售数量) from #t where rn<=a.rn and 店铺=a.店铺),
[count]=(select count(销售数量) from #t where rn<=a.rn and 店铺=a.店铺) from #t a
)aa group by 店铺drop table #t
/*
店铺 销售总和
---- -----------
A 1850
B 960
C 650
*/
正确的结果是,不过搞完之后才发现原来时间是有可能存在相同的,汗select cusid,sum(price*total) as 销售总和 from
(
select * from
(
select
b.cusid,
b.price,
(case when (select sum(num) from b as tb
where tb.cusid=b.cusid and tb.createdate<=b.createdate)<=a.export then b.num --when (select sum(num) from b as tb
-- where tb.cusid=b.cusid and tb.createdate<=b.createdate)>a.export
-- --and (select sum(num) from b as tb where tb.cusid=b.cusid and tb.createdate<b.createdate)>a.export
--then a.export else a.export-(select isnull(sum(num),0) from b as tb
where tb.cusid=b.cusid and tb.createdate<b.createdate)
end
) as total,
createdate
from a,b
where a.cusid=b.cusid order by createdate
) d where total>0
) as c group by cusid
insert into 表A select 'A',10
insert into 表A select 'B',8
insert into 表A select 'C',20
create table 表B(店铺 varchar(10),销售数量 int,价格 int,日期 datetime)
insert into 表B select 'A',3,100,'2011-01-01'
insert into 表B select 'A',4,200,'2011-02-02'
insert into 表B select 'A',5,250,'2011-03-03'
insert into 表B select 'B',10,120,'2011-05-05'
insert into 表B select 'C',5,130,'2011-06-06'
go
;with c1 as(
select row_number()over(partition by 店铺 order by 日期)rn,* from 表B
),c2 as(
select a.rn,a.店铺,(case when a.销售数量<b.调出数 then a.销售数量 else b.调出数 end)*a.价格 销售总和,a.日期,
b.调出数-a.销售数量 as 剩余量
from c1 a inner join 表a b on a.店铺=b.店铺 and a.rn=1
union all
select a.rn,a.店铺,(case when a.销售数量<b.剩余量 then a.销售数量 else b.剩余量 end)*a.价格 销售总和,a.日期,
b.剩余量-a.销售数量 as 剩余量
from c1 a inner join c2 b on a.店铺=b.店铺 and a.rn=b.rn+1
)
select 店铺,sum(销售总和)as 销售总和 from c2 group by 店铺
/*
店铺 销售总和
---------- -----------
A 1850
B 960
C 650(3 行受影响)*/
go
drop table 表A,表B
select 'A', 10 union all
select 'B', 8 union all
select 'C', 20
create table B(店铺 varchar(10),销售数量 int,价格 int,日期 datetime)
insert into B select 'A',3,100,'2011-01-01'
insert into B select 'A',4,200,'2011-02-02'
insert into B select 'A',5,250,'2011-03-03'
insert into B select 'A',5,1000,'2011-04-04'
insert into B select 'B',10,120,'2011-05-05'
insert into B select 'C',5,130,'2011-06-06'
select 店铺,sum(价格*对应数量) as 销售总和 from
(
select b.店铺,b.价格,
对应数量=case when (select isnull(sum(销售数量),0) from b as tb
where tb.店铺=b.店铺 and tb.日期<=b.日期)<=a.调出数
then b.销售数量 when (select sum(销售数量) from b as tb
where tb.店铺=b.店铺 and tb.日期<=b.日期)>a.调出数 and
(select sum(销售数量) from b as tb where tb.店铺=b.店铺 and tb.日期<b.日期)>a.调出数
then 0 else a.调出数-(select isnull(sum(销售数量),0) from b as tb
where tb.店铺=b.店铺 and tb.日期<b.日期)
end
from a,b
where a.店铺=b.店铺
) as c group by 店铺drop table a,b
/*
店铺 销售总和
---------- -----------
A 1850
B 960
C 650
*/