因需要实现如下统计报表:
报表一: (三月份)
|产品名称 |本月| 本季|本年累计|
----------------------------
|氧气 |100 | 500| 500 |
----------------------------
|蒸汽 |300 | 400| 400 |
----------------------------
报表二:(三月份)
| | 厂 A | 厂 B |
-----------------------------------------------
|产品名称 |本月| 本季|本年累计| 本月|本季|本年累计 |
-----------------------------------------------
|氧气 |100 | 300| 300 | |200| 200 |
-----------------------------------------------
|蒸汽 |300 | 400| 400 | | | |
--------------------------------------------———
表的数据结构如下
name time num fact
------------------
氧气 2004-01 100 厂A
氧气 2004-02 100 厂A
氧气 2004-03 100 厂A
氧气 2004-02 200 厂B
蒸汽 2004-02 100 厂A
蒸汽 2004-03 300 厂Aname: 产品名称
time: 时间
num : 消耗量
fact : 厂名称
表一:是用来生成所有厂某种能源的消耗量统计
表二:是用来生成各个厂的所有能源的消耗量统计问两个个问题: 1。报表一的那种统计本季,本年的数据一般怎么实现?是不是先用存储过程
做好还是有别的什么方法?
2。报表二的把厂并排横向的方法如何做?
直接用sql统计的话,统计本季好像部怎么好写把?
你有什么好的统计方法?
再直接弄到报表上打印...
/////////
直接用sql统计的话,统计本季好像部怎么好写把?
你有什么好的统计方法?
/////
没什么不好写啊.把你的表贴出来看看
------------------
氧气 2004-01 100 厂A
氧气 2004-02 100 厂A
氧气 2004-03 100 厂A
氧气 2004-02 200 厂B
蒸汽 2004-02 100 厂A
蒸汽 2004-03 300 厂A表名allot
select name,
(select count(num) from t as t2 where t1.name=t2.name and time>datediff(m,-1,getdate()),
(select count(num) from t as t2 where t1.name=t2.name and time>datediff(m,-3,getdate()),
(select count(num) from t as t2 where t1.name=t2.name and time>datediff(y,-1,getdate())
from t as t1
group by name
//for sql server2 与1 类似,在条件中加上 and fact = '厂A'
To : liuqifeiyu(liuqi)
没什么难的,你就说说怎么做的啊
如果是8月的话那么本季就应该统计的是7~8月
本年就应该统计1~8月
(select count(num) from t as t2 where t1.name=t2.name and year(time)=year(getdate()) and month(time)=month(getdate())),
(select count(num) from t as t2 where t1.name=t2.name and year(time)=year(getdate()) and (month(time)+1) / 3 =(month(getdate())+1) /3),
(select count(num) from t as t2 where t1.name=t2.name and year(time)=year(getdate()) ),
from t as t1
group by name
//for sql serveryear(time)=year(getdate()) and (month(time)+1) / 3 =(month(getdate())+1) /3
就是本季的
year(time)=year(getdate()) and (month(time)-1) / 3 =(month(getdate())-1) /3
就是本季的
统计季的方法好像不对把
如果是8月,应该就是统计7~9 月的总计把
(month(time)-1) / 3 =(month(getdate())-1) /3 这个条件可不可以统计两个月和三个月了
应为一季可能有三个月吧!请教了?