34,593
社区成员
发帖
与我相关
我的任务
分享
/*---------------------------------------
瘦狼阿亮 | 钱不是问题,问题是没钱!
我乐于享受学习的过程
---------------------------------------*/
--建立测试环境表
declare @tmp table
(listID varchar(10),baseDate datetime,baseAmount smallint)
/*表字段说明
listID--编号
baseDate--日期
baseAmount--数量
*/
--插入测试数据
insert into @tmp
select '001','2009-01-01',30 union all
select '001','2009-01-15',-2 union all
select '001','2009-01-18',-5 union all
select '001','2009-02-01',15 union all
select '001','2009-02-02',-10 union all
select '001','2009-02-15',-2
/*执行结果:
(所影响的行数为 6 行)
*/
--检查测试数据
select * from @tmp
/*执行结果:
listID baseDate baseAmount
---------- ------------------------------------------------------ ----------
001 2009-01-01 00:00:00.000 30
001 2009-01-15 00:00:00.000 -2
001 2009-01-18 00:00:00.000 -5
001 2009-02-01 00:00:00.000 15
001 2009-02-02 00:00:00.000 -10
001 2009-02-15 00:00:00.000 -2
(所影响的行数为 6 行)
*/
--查询结果与数据测试比对
select basedate,adddate=datediff(day,basedate,'2009-03-01'),baseAmount from @tmp
/*执行结果:
basedate adddate baseAmount
------------------------------------------------------ ----------- ----------
2009-01-01 00:00:00.000 59 30
2009-01-15 00:00:00.000 45 -2
2009-01-18 00:00:00.000 42 -5
2009-02-01 00:00:00.000 28 15
2009-02-02 00:00:00.000 27 -10
2009-02-15 00:00:00.000 14 -2
(所影响的行数为 6 行)
*/
--执行查询
select
listID,
smallin30_Amount=sum(case when datediff(dd,baseDate,'2009-03-01')<30 then baseAmount else 0 end),
smallin89_Amount=sum(case when datediff(dd,baseDate,'2009-03-01') between 30 and 89 then baseAmount else 0 end),
bigin90_Amount=sum(case when datediff(dd,baseDate,'2009-03-01')>=90 then baseAmount else 0 end)
from @tmp
group by listID
/*执行结果:
listID smallin30_Amount smallin89_Amount bigin90_Amount
---------- ---------------- ---------------- --------------
001 3 23 0
(所影响的行数为 1 行)
*/
if object_id('[tab]') is not null drop table [tb]
go
create table [tab]([编号] varchar(3),[日期] datetime,[数量] int)
insert [tab]
select '001','2009-1-1',30 union all
select '001','2009-1-15',-2 union all
select '001','2009-1-18',-5 union all
select '001','2009-2-1',15 union all
select '001','2009-2-2',-10 union all
select '001','2009-2-15',-2
--select * from tab
select [编号], sum(case when datediff(dd,[日期],'2009-03-01')<30 then [数量] else 0 end) as '30',
sum(case when datediff(dd,[日期],'2009-03-01') between 30 and 89 then [数量] else 0 end) as '3089',
sum(case when datediff(dd,[日期],'2009-03-01')>=90 then [数量] else 0 end) as '90'
into #in
from tab
where [数量]>0
group by [编号]
select [编号], sum(case when datediff(dd,[日期],'2009-03-01')<30 then [数量] else 0 end) as '30',
sum(case when datediff(dd,[日期],'2009-03-01') between 30 and 89 then [数量] else 0 end) as '3089',
sum(case when datediff(dd,[日期],'2009-03-01')>=90 then [数量] else 0 end) as '90'
into #out
from tab
where [数量]<0
group by [编号]
--select * from #in
--select * from #out
select a.[编号], case when a.[3089]+a.[90]+b.[30]+b.[3089]+b.[90]>=0 then a.[30] else a.[30]+a.[3089]+a.[90]+b.[30]+b.[3089]+b.[90] end as '库龄小于30天',
case when a.[90]+b.[30]+b.[3089]+b.[90]>=0 then a.[3089] else a.[3089]+a.[90]+b.[30]+b.[3089]+b.[90] end as '库龄30-89天',
case when a.[90]+b.[30]+b.[3089]+b.[90]>0 then a.[90]+b.[30]+b.[3089]+b.[90] else 0 end as '库龄90天以上'
from #in a inner join #out b on a.[编号] = b.[编号]
---测试数据---
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([编号] varchar(3),[日期] datetime,[数量] int)
insert [tb]
select '001','2009-1-1',30 union all
select '001','2009-1-15',-2 union all
select '001','2009-1-18',-5 union all
select '001','2009-2-1',15 union all
select '001','2009-2-2',-10 union all
select '001','2009-2-15',-2
---查询---
select
a.编号,
a.日期,
a.入库数量,
b.出库总量
into #
from
(select 编号,日期,数量 as 入库数量 from tb where 数量>0) a
left join
(select 编号,sum(数量) as 出库总量 from tb where 数量<0 group by 编号) b
on
a.编号=b.编号
select
编号,
[库龄小于30天]=sum(case when datediff(day,日期,'2009-3-1')<30 then 库存 else 0 end),
[库龄30-89天]=sum(case when datediff(day,日期,'2009-3-1') between 30 and 89 then 库存 else 0 end),
[库龄90天以上]=sum(case when datediff(day,日期,'2009-3-1')>=90 then 库存 else 0 end)
from
(
select
编号,
日期,
库存=case when 库存<0 then 0 else case when 入库数量<库存 then 入库数量 else 库存 end end
from
(
select *,(select sum(入库数量) from # where 日期<=t.日期)+出库总量 as 库存 from # t
) t) tt
group by
编号
drop table #
---结果---
编号 库龄小于30天 库龄30-89天 库龄90天以上
---- ----------- ----------- -----------
001 15 11 0
(所影响的行数为 1 行)
declare @t1 table( 编号 varchar(10), 日期 date , 数量 int)
insert into @t1 values('001', '2009-1-1', 30 )
insert into @t1 values('001', '2009-1-15', -2 )
insert into @t1 values('001', '2009-1-18', -5 )
insert into @t1 values('001', '2009-2-1', 15 )
insert into @t1 values('001', '2009-2-2', -10 )
insert into @t1 values('001', '2009-2-15', -2 )
select b.编号,
case when b.[库龄90天以上]+b.[库龄3089天以上负]+b.库龄3089天以上正 + b.库龄小于30天负>=0 then b.库龄小于30天正
else b.库龄小于30天正 + b.[库龄90天以上]+b.[库龄3089天以上负]+b.库龄3089天以上正 + b.库龄小于30天负
end as '库龄小于30天',
case when b.[库龄90天以上] + b.库龄3089天以上负 + b.库龄小于30天负 >=0 then b.库龄3089天以上正
when b.[库龄90天以上] + b.库龄3089天以上负 + b.库龄小于30天负 + b.库龄3089天以上正 >=0 then
b.[库龄90天以上] + b.库龄3089天以上负 + b.库龄小于30天负 + b.库龄3089天以上正
else 0
end as '库龄3089天',
b.[库龄90天以上]
from(
select
a.编号,
(select isnull(SUM(数量),0) from @t1 where 编号=a.编号 and
DATEDIFF(day,日期,'2009-3-1') >89) as '库龄90天以上',
(select isnull(SUM(数量),0) from @t1 where 编号=a.编号 and
DATEDIFF(day,日期,'2009-3-1') between 30 and 89 and 数量>=0) as '库龄3089天以上正',
(select isnull(SUM(数量),0) from @t1 where 编号=a.编号 and
DATEDIFF(day,日期,'2009-3-1') between 30 and 89 and 数量<0) as '库龄3089天以上负',
(select isnull(SUM(数量),0) from @t1 where 编号=a.编号 and
DATEDIFF(day,日期,'2009-3-1') <30 and 数量>=0) as '库龄小于30天正',
(select isnull(SUM(数量),0) from @t1 where 编号=a.编号 and
DATEDIFF(day,日期,'2009-3-1') <30 and 数量<0) as '库龄小于30天负'
from @t1 a group by a.编号)b