可参考昨天的贴子
http://topic.csdn.net/u/20080617/18/e6adc396-17bb-4ccd-b0cc-4dbe6b010fc9.html?1773065316现在有6个表
1.大分类表 (分类ID, 分类名称)
2.小分类表 (分类ID, 分类名称, 所属大类ID)
3.物资基础表 (物资ID, 物资名称, 所属大类ID, 所属小类ID)
4.物资采购表 (采购ID, 物资ID, 数量, 价格, 采购时间)
5.物资出货表 (出货ID, 物资ID, 数量, 价格, 出货时间, 领用单位ID)
6.领用单位表 (单位ID, 单位名称)要求两种统计结果
1.要求统计出某年度 各单位每月领用的物资总金额,这个后期用来计算每月各单位的成本消耗
===================================================================
xxxx年各单位消耗物资统计
单位名称 一月 二月 三月 四月 五月 六月 七月 八月 九月 十月 十一月 十二月 合计
aaaa 0 0 0 0 0 0 0 0 0 0 0 0 0
bbbb 0 0 0 0 0 0 0 0 0 0 0 0 0
cccc 0 0 0 0 0 0 0 0 0 0 0 0 0
............
====================================================================
有两个条件 1.指定年份 or 2.指定某个分类
2.要求统计出某年度 各分类每月进出物资总金额===================================================================
xxxx年分类采购统计
大类名成 小类名称 一月 二月 三月 四月 五月 六月 七月 八月 九月 十月 十一月 十二月 合计
大类A 0 0 0 0 0 0 0 0 0 0 0 0 0
小类A 0 0 0 0 0 0 0 0 0 0 0 0 0
小类B 0 0 0 0 0 0 0 0 0 0 0 0 0
大类B 0 0 0 0 0 0 0 0 0 0 0 0 0
小类C 0 0 0 0 0 0 0 0 0 0 0 0 0
小类D 0 0 0 0 0 0 0 0 0 0 0 0 0
......................................................................... xxxx年分类出货统计
大类名成 小类名称 一月 二月 三月 四月 五月 六月 七月 八月 九月 十月 十一月 十二月 合计
大类A 0 0 0 0 0 0 0 0 0 0 0 0 0
小类A 0 0 0 0 0 0 0 0 0 0 0 0 0
小类B 0 0 0 0 0 0 0 0 0 0 0 0 0
大类B 0 0 0 0 0 0 0 0 0 0 0 0 0
小类C 0 0 0 0 0 0 0 0 0 0 0 0 0
小类D 0 0 0 0 0 0 0 0 0 0 0 0 0
.........................................................................
====================================================================
对于第2个统计,实际上两个表可以做成一个,对每个月再次划分成采购和出货,但我想可能比较麻烦呵呵,各位再帮我解决一下,我知道是个苦力活,呵呵,就当练练手吧,非常感谢
http://topic.csdn.net/u/20080617/18/e6adc396-17bb-4ccd-b0cc-4dbe6b010fc9.html?1773065316现在有6个表
1.大分类表 (分类ID, 分类名称)
2.小分类表 (分类ID, 分类名称, 所属大类ID)
3.物资基础表 (物资ID, 物资名称, 所属大类ID, 所属小类ID)
4.物资采购表 (采购ID, 物资ID, 数量, 价格, 采购时间)
5.物资出货表 (出货ID, 物资ID, 数量, 价格, 出货时间, 领用单位ID)
6.领用单位表 (单位ID, 单位名称)要求两种统计结果
1.要求统计出某年度 各单位每月领用的物资总金额,这个后期用来计算每月各单位的成本消耗
===================================================================
xxxx年各单位消耗物资统计
单位名称 一月 二月 三月 四月 五月 六月 七月 八月 九月 十月 十一月 十二月 合计
aaaa 0 0 0 0 0 0 0 0 0 0 0 0 0
bbbb 0 0 0 0 0 0 0 0 0 0 0 0 0
cccc 0 0 0 0 0 0 0 0 0 0 0 0 0
............
====================================================================
有两个条件 1.指定年份 or 2.指定某个分类
2.要求统计出某年度 各分类每月进出物资总金额===================================================================
xxxx年分类采购统计
大类名成 小类名称 一月 二月 三月 四月 五月 六月 七月 八月 九月 十月 十一月 十二月 合计
大类A 0 0 0 0 0 0 0 0 0 0 0 0 0
小类A 0 0 0 0 0 0 0 0 0 0 0 0 0
小类B 0 0 0 0 0 0 0 0 0 0 0 0 0
大类B 0 0 0 0 0 0 0 0 0 0 0 0 0
小类C 0 0 0 0 0 0 0 0 0 0 0 0 0
小类D 0 0 0 0 0 0 0 0 0 0 0 0 0
......................................................................... xxxx年分类出货统计
大类名成 小类名称 一月 二月 三月 四月 五月 六月 七月 八月 九月 十月 十一月 十二月 合计
大类A 0 0 0 0 0 0 0 0 0 0 0 0 0
小类A 0 0 0 0 0 0 0 0 0 0 0 0 0
小类B 0 0 0 0 0 0 0 0 0 0 0 0 0
大类B 0 0 0 0 0 0 0 0 0 0 0 0 0
小类C 0 0 0 0 0 0 0 0 0 0 0 0 0
小类D 0 0 0 0 0 0 0 0 0 0 0 0 0
.........................................................................
====================================================================
对于第2个统计,实际上两个表可以做成一个,对每个月再次划分成采购和出货,但我想可能比较麻烦呵呵,各位再帮我解决一下,我知道是个苦力活,呵呵,就当练练手吧,非常感谢
insert into tb select 1,'2008-01-01',10
insert into tb select 2,'2008-01-01',10
insert into tb select 3,'2008-02-01',20
insert into tb select 4,'2008-02-01',20
insert into tb select 5,'2008-03-01',30
insert into tb select 6,'2008-03-01',30
insert into tb select 7,'2008-04-01',40
insert into tb select 8,'2008-04-01',40
insert into tb select 9,'2008-05-01',50
insert into tb select 10,'2008-05-01',50
insert into tb select 11,'2008-06-01',60
insert into tb select 12,'2008-06-01',60
insert into tb select 13,'2008-07-01',70
insert into tb select 14,'2008-07-01',70
insert into tb select 15,'2008-08-01',80
insert into tb select 16,'2008-08-01',80
insert into tb select 17,'2008-09-01',90
insert into tb select 18,'2008-09-01',90
insert into tb select 19,'2008-10-01',100
insert into tb select 20,'2008-10-01',100
insert into tb select 21,'2008-11-01',110
insert into tb select 22,'2008-11-01',110
insert into tb select 23,'2008-12-01',120
insert into tb select 24,'2008-12-01',120--静态查询
select
sum(case when datepart(yy,date)='2008' and datepart(mm,date)='01' then sale else 0 end) as '1月',
sum(case when datepart(yy,date)='2008' and datepart(mm,date)='02' then sale else 0 end) as '2月',
sum(case when datepart(yy,date)='2008' and datepart(mm,date)='03' then sale else 0 end) as '3月',
sum(case when datepart(yy,date)='2008' and datepart(mm,date)='04' then sale else 0 end) as '4月',sum(case when datepart(yy,date)='2008' and datepart(mm,date)='05' then sale else 0 end) as '5月',
sum(case when datepart(yy,date)='2008' and datepart(mm,date)='06' then sale else 0 end) as '6月',
sum(case when datepart(yy,date)='2008' and datepart(mm,date)='07' then sale else 0 end) as '7月',
sum(case when datepart(yy,date)='2008' and datepart(mm,date)='08' then sale else 0 end) as '8月',sum(case when datepart(yy,date)='2008' and datepart(mm,date)='09' then sale else 0 end) as '9月',
sum(case when datepart(yy,date)='2008' and datepart(mm,date)='10' then sale else 0 end) as '10月',
sum(case when datepart(yy,date)='2008' and datepart(mm,date)='11' then sale else 0 end) as '11月',
sum(case when datepart(yy,date)='2008' and datepart(mm,date)='12' then sale else 0 end) as '12月'
from tb
--插入2009年数据
insert into tb select 25,'2009-01-01',10
--动态查询
declare @sql varchar(max)
select @sql=isnull(@sql+',','')+'sum(case when datepart(yy,date)='''+ltrim([year])+''' and datepart(mm,date)='''+ltrim([month])+''' then sale else 0 end) as ['+ltrim([year])+'年'+ltrim([month])+'月]'
from (select distinct datepart(yy,date) as [year],datepart(mm,date) as [month] from tb)a
exec('select '+@sql+' from tb')12月的统计
===================================================================
xxxx年各单位消耗物资统计
单位名称 一月 二月 三月 四月 五月 六月 七月 八月 九月 十月 十一月 十二月 合计
aaaa 0 0 0 0 0 0 0 0 0 0 0 0 0
bbbb 0 0 0 0 0 0 0 0 0 0 0 0 0
cccc 0 0 0 0 0 0 0 0 0 0 0 0 0 declare @sql varchar(8000)
select @sql=isnull(@sql+',','')+'sum(case when datepart(yy,出货时间)='''+ltrim([month])+''' then 价格 else 0 end) as ['+ltrim([month])+'月]'
from (select distinct datepart(mm,出货时间) as [month] from 物资出货表)a
exec('select b.单位名称,'+@sql+' ,sum(价格) as ''合计'' from 物资出货表 a left join 领用单位表 b on a.领用单位ID=b.单位ID group by b.单位名称')写第一个吧,太多了
sum(case when datepart(yy,出货时间)='2008' and datepart(mm,出货时间)='01' then 总价 else 0 end) as '1月',
sum(case when datepart(yy,出货时间)='2008' and datepart(mm,出货时间)='02' then 总价 else 0 end) as '2月',
sum(case when datepart(yy,出货时间)='2008' and datepart(mm,出货时间)='03' then 总价 else 0 end) as '3月',
sum(case when datepart(yy,出货时间)='2008' and datepart(mm,出货时间)='04' then 总价 else 0 end) as '4月',sum(case when datepart(yy,出货时间)='2008' and datepart(mm,出货时间)='05' then 总价 else 0 end) as '5月',
sum(case when datepart(yy,出货时间)='2008' and datepart(mm,出货时间)='06' then 总价 else 0 end) as '6月',
sum(case when datepart(yy,出货时间)='2008' and datepart(mm,出货时间)='07' then 总价 else 0 end) as '7月',
sum(case when datepart(yy,出货时间)='2008' and datepart(mm,出货时间)='08' then 总价 else 0 end) as '8月',sum(case when datepart(yy,出货时间)='2008' and datepart(mm,出货时间)='09' then 总价 else 0 end) as '9月',
sum(case when datepart(yy,出货时间)='2008' and datepart(mm,出货时间)='10' then 总价 else 0 end) as '10月',
sum(case when datepart(yy,出货时间)='2008' and datepart(mm,出货时间)='11' then 总价 else 0 end) as '11月',
sum(case when datepart(yy,出货时间)='2008' and datepart(mm,出货时间)='12' then 总价 else 0 end) as '12月',sum(case when datepart(yy,出货时间)='2008' then 总价 else 0 end) as '合计'From 物资出货表 a left join 领用单位表 b on a.领用单位ID=b.单位ID group by b.单位名称
sum(case when datepart(mm,出货时间)='01' then 总价 else 0 end) as '1月',
sum(case when datepart(mm,出货时间)='02' then 总价 else 0 end) as '2月',
sum(case when datepart(mm,出货时间)='03' then 总价 else 0 end) as '3月',
sum(case when datepart(mm,出货时间)='04' then 总价 else 0 end) as '4月',sum(case when datepart(mm,出货时间)='05' then 总价 else 0 end) as '5月',
sum(case when datepart(mm,出货时间)='06' then 总价 else 0 end) as '6月',
sum(case when datepart(mm,出货时间)='07' then 总价 else 0 end) as '7月',
sum(case when datepart(mm,出货时间)='08' then 总价 else 0 end) as '8月',sum(case when datepart(mm,出货时间)='09' then 总价 else 0 end) as '9月',
sum(case when datepart(mm,出货时间)='10' then 总价 else 0 end) as '10月',
sum(case when datepart(mm,出货时间)='11' then 总价 else 0 end) as '11月',
sum(case when datepart(mm,出货时间)='12' then 总价 else 0 end) as '12月',isnull(sum(总价),0) as '合计'From 物资出货表 a right join 领用单位表 b on datepart(yy,出货时间)='2008' and a.领用单位ID=b.单位ID group by b.单位名称
sum(case when month(采购时间)=1 then 数量*价格 else 0 end) as '1月',
sum(case when month(采购时间)=2 then 数量*价格 else 0 end) as '2月',
sum(case when month(采购时间)=3 then 数量*价格 else 0 end) as '3月',
sum(case when month(采购时间)=4 then 数量*价格 else 0 end) as '4月',sum(case when month(采购时间)=5 then 数量*价格 else 0 end) as '5月',
sum(case when month(采购时间)=6 then 数量*价格 else 0 end) as '6月',
sum(case when month(采购时间)=7 then 数量*价格 else 0 end) as '7月',
sum(case when month(采购时间)=8 then 数量*价格 else 0 end) as '8月',sum(case when month(采购时间)=9 then 数量*价格 else 0 end) as '9月',
sum(case when month(采购时间)=10 then 数量*价格 else 0 end) as '10月',
sum(case when month(采购时间)=11 then 数量*价格 else 0 end) as '11月',
sum(case when month(采购时间)=12 then 数量*价格 else 0 end) as '12月',isnull(sum(数量*价格),0) as '合计'From 物资采购表 a,物资基础表 b,大分类表 c,小分类表 d
where year(a.采购时间)=2008 and a.物资ID=b.物资ID and b.所属大类ID=c.分类ID and b.所属小类ID=d.分类ID
group by d.分类名称,c.分类名称 with rollup having d.分类名称 is not null
order by d.分类名称,c.分类名称
---第三个表改改表名和字段名就可以了吧
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[物资出货表](
[出货ID] [int] primary key,
[物资ID] [int] NULL,
[数量] [int] ,
[价格] money NULL,
[出货时间] datetime null,
[领用单位] [int],
) ON [PRIMARY]CREATE TABLE [dbo].[领用单位表]
(
[单位ID] [int] primary key,
[单位名] [nvarchar](100)
)
select datepart(month,getdate())
insert into 物资出货表 select 3,1000,200,11.5,'2007-2-5',2000 union select 4,1001,300,60,'2007-2-1',2001
insert into 领用单位表 select 2000,'清代有华' union select 2001 ,'万科'select * from 物资出货表
declare @year int
set @year=2007
select 单位名 ,sum(case when datepart(month,出货时间)=1 then 数量 else 0 end) '1月',
sum(case when datepart(month,出货时间)=2 then 数量 else 0 end) '2月' ,sum(case when datepart(month,出货时间)=3 then 数量 else 0 end) '3月',
sum(case when datepart(month,出货时间)=4 then 数量 else 0 end) '4月',
sum(case when datepart(month,出货时间)=5 then 数量 else 0 end) '5月',
sum(case when datepart(month,出货时间)=6 then 数量 else 0 end) '6月',
sum(case when datepart(month,出货时间)=7 then 数量 else 0 end) '7月',
sum(case when datepart(month,出货时间)=8then 数量 else 0 end) '8月',
sum(case when datepart(month,出货时间)=9 then 数量 else 0 end) '9月',
sum(case when datepart(month,出货时间)=10 then 数量 else 0 end) '10月',
sum(case when datepart(month,出货时间)=11 then 数量 else 0 end) '11月',
sum(case when datepart(month,出货时间)=12 then 数量 else 0 end) '12月',小计=(select sum(a.数量) from 物资出货表 a where a.领用单位=领用单位表.单位ID and datepart(year,a.出货时间)=@year )
from 物资出货表,领用单位表 where 领用单位表.单位ID=物资出货表.领用单位 group by 单位名,单位ID
1.第一条记录大类名称是 null
2.没有直接对大类做出统计是吗