日期:2014-05-18  浏览次数:20572 次

急!关于数据库和水晶报表问题两个,大侠请进!!!!!!!!
第一个问题:我有一个表(TabProduction)两列
strDatTime(生产日期,nvarchar) intFinalProductionS1(日产量,int型)
20070906 54
20070907 53
20070908 57
20071006 45
20071007 58

要求按月份累加起来并放入水晶报表中,帮忙指点一下。
反映在水晶报表中是:

  年度月份 月产量
  200709 XXX
  200710 XXX
  . .
  . .
  . .
该用什么方法,小弟新手,请多指教!!!!

第二个问题:

怎样从两个视图中取对应的内容进行比较计算,具体例子为:
我有N个按钮(带灯按钮,按下去灯会亮,再按灯就灭了,此为两次事件,记录在数据库中)数据库中记录每次事件的(时间),(按钮号),(状态).状态列为灯是亮还是灭,亮为1,灭为2.
  时间(datetime) 按钮号(nvarchar) 状态(int)
  2007-8-16 16:48:27 1 1
  2007-8-16 16:48:28 2 1
  2007-8-16 16:58:32 2 0
  2007-8-16 16:59:31 1 0
  2007-8-16 16:48:28 3 1
  2007-8-16 16:51:28 3 0
以此类推......
我考虑用两个视图将状态为1和状态为0的分开.
怎样才能选取某个按钮灯亮和灯灭的时间差,并记录在另一个集合中,供查询调用,并输出到水晶报表中,就像下边一样

  开始时间 结束时间 按钮号 时间长度
  2007-8-16 16:48:27 2007-8-16 16:59:31 1 XXX
  2007-8-16 16:48:28 2007-8-16 16:58:32 2 XXX  







------解决方案--------------------
使用分组求和,如下
[code=S
--问题一
declare @t table(tine nchar(10),cid int )
insert into @t select '20070906',1
insert into @t select '20070908',2
insert into @t select '20070909',3
insert into @t select '20071007',4
insert into @t select '20071007',5

select left(tine,6) as '月份',sum(cid) as '合计' from @t group by left(tine,6)
/*
月份 合计
------ -----------
200709 6
200710 9

(2 行受影响)
*/QL]
[/code]
------解决方案--------------------
使用分组求和,如下 
SQL code

--问题一 
declare @t table(tine nchar(10),cid int ) 
insert into @t select  '20070906 ',1 
insert into @t select  '20070908 ',2 
insert into @t select  '20070909 ',3 
insert into @t select  '20071007 ',4 
insert into @t select  '20071007 ',5 

select left(tine,6) as  '月份 ',sum(cid) as  '合计 ' from @t group by left(tine,6) 
/* 
月份     合计 
------ ----------- 
200709 6 
200710 9 

(2 行受影响) 
*/

------解决方案--------------------
SQL code

--问题二
declare @t table(tine datetime,cid int ,flag int)
insert into @t select '2007-8-16 16:48:27',1,1
insert into @t select '2007-8-16 16:48:28',2,1
insert into @t select '2007-8-16 16:58:32',2,0
insert into @t select '2007-8-16 16:59:31',1,0
insert into @t select '2007-8-16 16:48:28',3,1
insert into @t select '2007-8-16 16:51:28',3,0
         

select min(tine) as '开始时间',max(tine) as '结束时间',
cid as '按钮号',max(tine)-min(tine) as '时间长度'
 from @t group by cid
--你要的效果
/*
开始时间                    结束时间                    按钮号         时间长度
----------------------- ----------------------- ----------- -----------------------
2007-08-16 16:48:27.000 2007-08-16 16:59:31.000 1           1900-01-01 00:11:04.000
2007-08-16 16:48:28.000 2007-08-16 16:58:32.000 2           1900-01-01 00:10:04.000
2007-08-16 16:48:28.000 2007-08-16 16:51:28.000 3           1900-01-01 00:03:00.000

(3 行受影响)
*/