表 ITEM
字段
item dct(类型)mcu(库位)num(数量)shijian(时间)danwei(单位)
1 ib d01 100 07-9-8 ga
1 ib d01 50 07-8-8 ga
3 ib d01 70 07-9-9 ga
1 ib h01 82 07-5-6 ea
1 ib d01 22 07-5-9 ea
2 ib d01 2 06-5-3 ea
3 ib h01 41 06-5-3 ea
结果:
根据item和mcu分组,按照时间段(距今天1个月、半年、1年、1年以上四个时间段)罗列出以上信息,其中num是求和。就是对于item为1、mcu为d01下四个时间段的信息;item为1、mcu为h01下四个时间段的信息....
显示:
item=1
d01
一月内:
一月-半年内:
半年-1年内:
〉1年:
h01
一月内:
一月-半年内:
半年-1年内:
〉1年:
item=2
h01
一月内:
一月-半年内:
半年-1年内:
〉1年:
d01
一月内:
一月-半年内:
半年-1年内:
〉1年:
字段
item dct(类型)mcu(库位)num(数量)shijian(时间)danwei(单位)
1 ib d01 100 07-9-8 ga
1 ib d01 50 07-8-8 ga
3 ib d01 70 07-9-9 ga
1 ib h01 82 07-5-6 ea
1 ib d01 22 07-5-9 ea
2 ib d01 2 06-5-3 ea
3 ib h01 41 06-5-3 ea
结果:
根据item和mcu分组,按照时间段(距今天1个月、半年、1年、1年以上四个时间段)罗列出以上信息,其中num是求和。就是对于item为1、mcu为d01下四个时间段的信息;item为1、mcu为h01下四个时间段的信息....
显示:
item=1
d01
一月内:
一月-半年内:
半年-1年内:
〉1年:
h01
一月内:
一月-半年内:
半年-1年内:
〉1年:
item=2
h01
一月内:
一月-半年内:
半年-1年内:
〉1年:
d01
一月内:
一月-半年内:
半年-1年内:
〉1年:
解决方案 »
- 会用游标的帮忙看看这个存储过程有问题吗?????
- SQL语句问题~散分~~
- 数据库压缩后怎么空间空不出来
- 如何减少逻辑磁盘读取次数
- 急问:对数据库的操作量也不大,但是数据库近来从4G增加到 17G,一般是什么原因?
- 【急】日期区间比较计算问题
- 如何在一条sql语句中把a,b表中满足关联条件的某列设为定值?
- 关于数据流写入数据库(ms-sql)和从数据库读取的问题(ms-sql)???
- 关于sql数据库的问题
- 创建链接服务器提示“没有从远程服务器IP映射到本地用户null的远程用户SA,错误:15466”
- order by,要求先字母(字母先小写),后数字的升序排
- 小问题一个...哪位进来告诉我一下这个语句是什么意思?
select distinct item,mcu,
isnull(t1.距今天1个月,0) 距今天1个月,
isnull(t2.距今天半年,0) 距今天半年 ,
isnull(t3.距今天1年,0) 距今天1年 ,
isnull(t4.距今天1年以上,0) 距今天1年以上
from t
left join (select item,mcu,sum(num) 距今天1个月 from item where datediff(day,shijian,getdate()) <= 30 group by item,mcu) t1 on t.item = t1.item and t.mcu = t1.mcu
left join (select item,mcu,sum(num) 距今天半年 from item where datediff(day,shijian,getdate()) <= 180 group by item,mcu) t2 on t.item = t2.item and t.mcu = t2.mcu
left join (select item,mcu,sum(num) 距今天1年 from item where datediff(day,shijian,getdate()) <= 365 group by item,mcu) t3 on t.item = t3.item and t.mcu = t3.mcu
left join (select item,mcu,sum(num) 距今天1年以上 from item where datediff(day,shijian,getdate()) > 365 group by item,mcu) t4 on t.item = t4.item and t.mcu = t4.mcu
create table ITEM
(item int,dct nvarchar(10) null,
mcu nvarchar(10) null,
num int,shijian datetime,danwei nvarchar(10))
insert into item select 1,'ib','d01',100,'2007-9-8','ga'
union all select 1,'ib','d01',50,'2007-8-8','ga'
union all select 3,'ib','d01',70,'2007-9-8','ga'
union all select 1,'ib','d01',82,'2007-9-9','ea'
union all select 1,'ib','h01',22,'2007-5-8','ea'
union all select 2,'ib','d01',2,'2006-5-3','ea'
union all select 3,'ib','h01',41,'2006-5-3','ea'
--测试环境
item,
mcu,
(Select Sum(num) From ITEM Where item=A.item And mcu=A.mcu And Datediff(dd,shijian,getdate()) Between 0 And 31) As '一月内',
(Select Sum(num) From ITEM Where item=A.item And mcu=A.mcu And Datediff(dd,shijian,getdate()) Between 31 And 183) As '一月-半年内',
(Select Sum(num) From ITEM Where item=A.item And mcu=A.mcu And Datediff(dd,shijian,getdate()) Between 183 And 366) As '半年-一年内',
(Select Sum(num) From ITEM Where item=A.item And mcu=A.mcu And Datediff(dd,shijian,getdate())>365) As '大于一年'
From ITEM As A
create table ITEM
(item int,dct nvarchar(10) null,
mcu nvarchar(10) null,
num int,shijian datetime,danwei nvarchar(10))
insert into item select 1,'ib','d01',100,'2007-9-8','ga'
union all select 1,'ib','d01',50,'2007-8-8','ga'
union all select 3,'ib','d01',70,'2007-9-8','ga'
union all select 1,'ib','d01',82,'2007-9-9','ea'
union all select 1,'ib','h01',22,'2007-5-8','ea'
union all select 2,'ib','d01',2,'2006-5-3','ea'
union all select 3,'ib','h01',41,'2006-5-3','ea'
select item,mcu,
sum(case when datediff(day,shijian, getdate()) between 0 and 30 then num else 0 end) as [30天內],
sum(case when datediff(day,shijian, getdate()) between 31 and 183 then num else 0 end) as [半年內],
sum(case when datediff(day,shijian, getdate()) between 184 and 365 then num else 0 end) as [一年內],
sum(case when datediff(day,shijian, getdate()) >365 then num else 0 end) as [一年以上]
from ITEM
group by item,mcu/*
item mcu 30天內 半年內 一年內 一年以上
----------- ---------- ----------- ----------- ----------- -----------
1 d01 182 50 0 0
2 d01 0 0 0 2
3 d01 70 0 0 0
1 h01 0 22 0 0
3 h01 0 0 0 41
*/
drop table ITEM
returns datetime
as
begin
declare @datetime datetime
set @datetime = dateadd(dd,right(@date,3)-1,cast(rtrim(1900+left(@date,3))+'0101' as datetime))
return @datetime
end
go*/
declare @now datetime
set @now=getdate()
declare @date1 datetime,@date2 datetime,@date3 datetime,@date4 datetime,@date5 datetime
set @date1=dateadd(dd,-60,@now)
set @date2=dateadd(dd,-120,@now)
set @date3=dateadd(dd,-180,@now)
set @date4=dateadd(dd,-360,@now)
set @date5=dateadd(dd,-720,@now)
--select @date1,@date2,@date3,@date4,@date5
declare @jdate1 int,@jdate2 int,@jdate3 int,@jdate4 int,@jdate5 int
set @jdate1=(year(@date1)-1900)*1000+datediff(dd,cast(cast(year(@date1) as nvarchar)+'0101' as datetime),@date1)+1
set @jdate2=(year(@date2)-1900)*1000+datediff(dd,cast(cast(year(@date2) as nvarchar)+'0101' as datetime),@date2)+1
set @jdate3=(year(@date3)-1900)*1000+datediff(dd,cast(cast(year(@date3) as nvarchar)+'0101' as datetime),@date3)+1
set @jdate4=(year(@date4)-1900)*1000+datediff(dd,cast(cast(year(@date4) as nvarchar)+'0101' as datetime),@date4)+1
set @jdate5=(year(@date5)-1900)*1000+datediff(dd,cast(cast(year(@date5) as nvarchar)+'0101' as datetime),@date5)+1
--select @jdate1,@jdate2,@jdate3,@jdate4,@jdate5select t1.ilitm,t1.ilmcu,t1.p,sum(t1.ilsqor) as qty
from
(Select
ilitm,ilmcu,
case
when iltrdj<@jdate5 then 6
when iltrdj<@jdate4 then 5
when iltrdj<@jdate3 then 4
when iltrdj<@jdate2 then 3
when iltrdj<@jdate1 then 2
else 1
end as p,
ilsqor
From proddta.f4111 As A
where ildct in ('ia','ov')
and ilitm between 1 and 10) as t1
group by t1.ilitm,t1.ilmcu,t1.p
最终结果
谢谢老龟、playwarcraft、SoftwKLC(自由的飞鸟(卢成))