我现在的数据:
斩刀号 底模 出货日期 数量
C-750 S-28 2003-10-10 100
C-750 S-28 2003-10-25 200需要的结果:
斩刀号 底模 10/10数量 10/25 总数量
C-750 S-28 100 200 300请教各位大虾这个怎么实现!
斩刀号 底模 出货日期 数量
C-750 S-28 2003-10-10 100
C-750 S-28 2003-10-25 200需要的结果:
斩刀号 底模 10/10数量 10/25 总数量
C-750 S-28 100 200 300请教各位大虾这个怎么实现!
from table t,
(select 斩刀号 底模 数量 from table where 出货日期=2003-10-10 ) a,
(select 斩刀号 底模 数量 from table where 出货日期=2003-10-25 ) b
where t.斩刀号=a.斩刀号 and t.斩刀号=b.斩刀号
and t.底模=a.底模 and t.底模 =b.底模
SELECT [斩刀号], [底模], Sum([数量]) AS [总数量]
FROM 表名
GROUP BY [斩刀号], [底模]
PIVOT Format([日],"Short Date");
(select 数量 form yourtable where 出货日期 = yourdatetime1)
(select 数量 form yourtable where 出货日期 = yourdatetime2)
(select sum(数量) form yourtable group by 斩刀号 底模) from yourtable;
字段:cCutNo,cXuanTou,cDimo,fSQty
里面只有两条记录:斩刀号 楦头 底模 出货日期 数量
C-750 A01 S-28 2003-10-10 100
C-750 A01 S-28 2003-10-25 200
set @sql = ''
select @sql = @sql + ',sum(case CONVERT(varchar(10),出货日期,111) when '''+出货日期+''' then 数量 else 0 end) ['+出货日期+']'
from (select distinct CONVERT(varchar(10),出货日期,111) 出货日期 from 有一表) temexec('select 斩刀号,底模'+@sql+',sum(数量) 总数量 from 有一表 group by 斩刀号,底模')
go
如果是oracle再和我说
set @sql = ''
select @sql = @sql + ',sum(case CONVERT(char(5),出货日期,101) when '''+出货日期+''' then 数量 else 0 end) ['+出货日期+']'
from (select distinct CONVERT(char(5),出货日期,101) 出货日期 from 有一表) temexec('select 斩刀号,底模'+@sql+',sum(数量) 总数量 from 有一表 group by 斩刀号,底模')
go