select temp1.年月,temp1.车间名称,temp1.化学品名称,temp1.本月用量,sum(temp2.本月用量) from a temp1, a temp2 where temp1.车间名称=temp2.车间名称 and temp1.化学品名称=temp2.化学品名称 and DATEPART(YEAR,temp1.年月)=DATEPART(YEAR,temp2.年月) and DATEPART(month,temp1.年月)>=DATEPART(month,temp2.年月) group by temp1.车间名称,temp1.化学品名称,temp1.年月,temp1.本月用量经过测试完全达到你要的要求。
这就不能这么说了,如果这样的话,就是本月以前的所有的数据都要算在里面 把这个DATEPART(YEAR,temp1.年月)=DATEPART(YEAR,temp2.年月) and DATEPART(month,temp1.年月)>=DATEPART(month,temp2.年月)改成这样: (DATEPART(YEAR,temp1.年月)=DATEPART(YEAR,temp2.年月) and DATEPART(month,temp1.年月)>=DATEPART(month,temp2.年月) or DATEPART(YEAR,temp1.年月)>DATEPART(YEAR,temp2.年月))
SELECT temp1.*, SUM(temp2.monthconsume) FROM WSChemistConsume temp1, WSChemistConsume temp2 WHERE temp1.equipment=temp2.equipment AND temp1.chemicalname=temp2.chemicalname AND DATEPART(YEAR,temp1.yearmonth)=DATEPART(YEAR,temp2.yearmonth) AND DATEPART(month,temp1.yearmonth)>=DATEPART(month,temp2.yearmonth) GROUP BY temp1.equipment,temp1.chemicalname,temp1.yearmonth其中monthconsume在数据库中是文本,我不止到怎么转换成int后再用sum()
SELECT temp1.*, SUM(convert(float,temp2.monthconsume)) FROM WSChemistConsume temp1, WSChemistConsume temp2 WHERE temp1.equipment=temp2.equipment AND temp1.chemicalname=temp2.chemicalname AND DATEPART(YEAR,temp1.yearmonth)=DATEPART(YEAR,temp2.yearmonth) AND DATEPART(month,temp1.yearmonth)>=DATEPART(month,temp2.yearmonth) GROUP BY temp1.equipment,temp1.chemicalname,temp1.yearmonth,temp1.monthconsume注意temp1.monthconsume这个必须要。convert是个系统函数,转换类型的。
谢谢各位大虾的帮忙,我现在把最后完整的答案总结如下: 再次感谢夕夕公主,Veeve等等各位大虾!!!问题零:比如数据库中一个表包括下列字段: 年月 产品名称 本月库存 而最后要显示的报表包括下列字段: 年月 产品名称 本月库存 上月库存 上月库存就是上个月的库存,需要从另外一条纪录中提取,怎样实现?答案:select FIRST.* , (select monthstock from WSChemistConsume where FIRST.equipment=equipment and FIRST.chemicalname=chemicalname and FIRST.yearmonth=DATEADD(month,1,yearmonth))as '上月库存' from WSChemistConsume as FIRST 问题一:有一个表包括下列字段: 年月 车间名称 化学品名称 本月用量 比如说 2002.10 HDPE车间 抗氧剂 3.66吨 其中年月、车间名称、化学品名称三个字段是主关键字,决定本月用量。 现在要给用户显示下列一个报表,内容包括: 年月 车间名称 化学品名称 本月用量 本年本月止累计用量 其中‘本年本月止累计用量’=当年1月份到当月的累计的用量, 根据上一条纪录就是:从2002.1到2002.10‘本月用量’累加起来的值。答案:select t1.*, (select SUM(convert(float,monthconsume)) from WSChemistConsume where equipment=t1.equipment and chemicalname=t1.chemicalname and year(yearmonth)=year(t1.yearmonth) and month(yearmonth)<=month(t1.yearmonth)) as '本年本月止累计用量' from WSChemistConsume as t1感谢网络,然我觉得这个世界好温暖,在这儿人间的真情表现的多么。。 还有看到好多人都写着“高分提问”,其实我觉得不是因为你给得分高人家就回答,而是应该感谢在这儿,在这个小天地里,形成的美好的这种气氛!原这种互帮互助的,互相学习的气氛永远保持下去,也愿在我们的生活中也充满着这种温暖。
select * from 本月库存,上月库存 where 本月库存的ID=上月库存的ID
你别告诉我两个表一点关系都没,那是神仙也那写了。呵呵。
本月库存的ID,上月库存的ID分别代表这两个表之间相等的字段名称。
select a.年月,a.产品名称,a.本月库存,上月库存=#temp.本月库存 from #temp,a
where a.年月=DATEADD(month,1,#temp.年月)
select temp1.年月,temp1.产品名称,temp1.本月库存,上月库存=temp2.本月库存
from a temp1, a temp2
where temp1.产品名称=temp2.产品名称 and temp1.年月=DATEADD(month,1,temp2.年月)
有一个表包括下列字段:
年月 车间名称 化学品名称 本月用量
比如说 2002.10 HDPE车间 抗氧剂 3.66吨
其中年月、车间名称、化学品名称三个字段是主关键字,决定本月用量。
现在要给用户显示下列一个报表,内容包括:
年月 车间名称 化学品名称 本月用量 本年本月止累计用量
其中‘本年本月止累计用量’=当年1月份到当月的累计的用量,
根据上一条纪录就是:从2002.1到2002.10‘本月用量’累加起来的值。是不是可以通过datatable 增加一列,然后对每个纪录执行一遍SQL语句计算到‘本年本月止累计用量’,并把值插入这列,我觉得这样做好烦。
我想通过SQL自身连接语句实现,怎样实现?请教各位大虾!!谢谢啦!最近被这些报表弄得烦透了!!
from a temp1, a temp2
where temp1.车间名称=temp2.车间名称 and temp1.化学品名称=temp2.化学品名称 and DATEPART(YEAR,temp1.年月)=DATEPART(YEAR,temp2.年月) and DATEPART(month,temp1.年月)>=DATEPART(month,temp2.年月)
group by temp1.车间名称,temp1.化学品名称,temp1.年月,temp1.本月用量经过测试完全达到你要的要求。
把这个DATEPART(YEAR,temp1.年月)=DATEPART(YEAR,temp2.年月) and DATEPART(month,temp1.年月)>=DATEPART(month,temp2.年月)改成这样:
(DATEPART(YEAR,temp1.年月)=DATEPART(YEAR,temp2.年月) and DATEPART(month,temp1.年月)>=DATEPART(month,temp2.年月) or DATEPART(YEAR,temp1.年月)>DATEPART(YEAR,temp2.年月))
AS
SELECT temp1.*, SUM(temp2.monthconsume)
FROM WSChemistConsume temp1, WSChemistConsume temp2
WHERE temp1.equipment=temp2.equipment AND temp1.chemicalname=temp2.chemicalname AND DATEPART(YEAR,temp1.yearmonth)=DATEPART(YEAR,temp2.yearmonth) AND DATEPART(month,temp1.yearmonth)>=DATEPART(month,temp2.yearmonth)
GROUP BY temp1.equipment,temp1.chemicalname,temp1.yearmonth其中monthconsume在数据库中是文本,我不止到怎么转换成int后再用sum()
FROM WSChemistConsume temp1, WSChemistConsume temp2
WHERE temp1.equipment=temp2.equipment AND temp1.chemicalname=temp2.chemicalname AND DATEPART(YEAR,temp1.yearmonth)=DATEPART(YEAR,temp2.yearmonth) AND DATEPART(month,temp1.yearmonth)>=DATEPART(month,temp2.yearmonth)
GROUP BY temp1.equipment,temp1.chemicalname,temp1.yearmonth,temp1.monthconsume注意temp1.monthconsume这个必须要。convert是个系统函数,转换类型的。
columnname=SUM(convert(float,temp2.monthconsume))
再次感谢夕夕公主,Veeve等等各位大虾!!!问题零:比如数据库中一个表包括下列字段:
年月 产品名称 本月库存
而最后要显示的报表包括下列字段:
年月 产品名称 本月库存 上月库存
上月库存就是上个月的库存,需要从另外一条纪录中提取,怎样实现?答案:select FIRST.* ,
(select monthstock from WSChemistConsume
where FIRST.equipment=equipment and FIRST.chemicalname=chemicalname
and FIRST.yearmonth=DATEADD(month,1,yearmonth))as '上月库存'
from WSChemistConsume as FIRST 问题一:有一个表包括下列字段:
年月 车间名称 化学品名称 本月用量
比如说 2002.10 HDPE车间 抗氧剂 3.66吨
其中年月、车间名称、化学品名称三个字段是主关键字,决定本月用量。
现在要给用户显示下列一个报表,内容包括:
年月 车间名称 化学品名称 本月用量 本年本月止累计用量
其中‘本年本月止累计用量’=当年1月份到当月的累计的用量,
根据上一条纪录就是:从2002.1到2002.10‘本月用量’累加起来的值。答案:select t1.*,
(select SUM(convert(float,monthconsume)) from WSChemistConsume
where
equipment=t1.equipment
and chemicalname=t1.chemicalname
and year(yearmonth)=year(t1.yearmonth)
and month(yearmonth)<=month(t1.yearmonth)) as '本年本月止累计用量'
from WSChemistConsume as t1感谢网络,然我觉得这个世界好温暖,在这儿人间的真情表现的多么。。
还有看到好多人都写着“高分提问”,其实我觉得不是因为你给得分高人家就回答,而是应该感谢在这儿,在这个小天地里,形成的美好的这种气氛!原这种互帮互助的,互相学习的气氛永远保持下去,也愿在我们的生活中也充满着这种温暖。