首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

急关于数据库和水晶报表有关问题两个,大侠请进!

2012-02-13 
急!关于数据库和水晶报表问题两个,大侠请进!!!!!!!!第一个问题:我有一个表(TabProduction)两列strDatTime(

急!关于数据库和水晶报表问题两个,大侠请进!!!!!!!!
第一个问题:我有一个表(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,1insert into @t select '2007-8-16 16:48:28',2,1insert into @t select '2007-8-16 16:58:32',2,0insert into @t select '2007-8-16 16:59:31',1,0insert into @t select '2007-8-16 16:48:28',3,1insert 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.0002007-08-16 16:48:28.000 2007-08-16 16:58:32.000 2           1900-01-01 00:10:04.0002007-08-16 16:48:28.000 2007-08-16 16:51:28.000 3           1900-01-01 00:03:00.000(3 行受影响)*/
------解决方案--------------------


问题1
select SubString(strDatTime, 1, 6) as [年度月份], Sum(intFinalProductionS1) as [月产量]
from TabProduction
group by SubString(strDatTime, 1, 6)
order by [年度月份] asc

问题2: 
select [时间] as [开始时间], 
(select Min([时间])
from View2
where Min([时间]) > A.[时间] and [按钮号] = A.[按钮号]) as [结束时间],
[按钮号], 
(select Min([时间]) - A.[时间]
from View2
where Min([时间]) > A.[时间] and [按钮号] = A.[按钮号]) as [时间长度]
from View1 A
--其中View1, View2表示你提到的两个分别表示开、灭的视图


[解决办法]
create table #t
( 时间 datetime , 按钮号 nvarchar, 状态 int) 
insert into #t
select '2007-8-16 16:48:27', 1 , 1 union all
select '2007-8-16 16:48:28', 2 , 1 union all
select '2007-8-16 16:58:32', 2 , 0 union all 
select '2007-8-16 16:59:31', 1 , 0 union all
select '2007-8-16 16:48:28', 3 , 1 union all
select '2007-8-16 16:51:28', 3 , 0 

select 
开始时间=min(时间),结束时间=max(时间), 按钮号 ,时间长度=datediff(ss,min(时间),max(时间))
from 
#t
group by 按钮号

开始时间 结束时间 按钮号 时间长度
------------------------------------------------------ ------------------------------------------------------ ---- ----------- 
2007-08-16 16:48:27.000 2007-08-16 16:59:31.000 1 664
2007-08-16 16:48:28.000 2007-08-16 16:58:32.000 2 604
2007-08-16 16:48:28.000 2007-08-16 16:51:28.000 3 180

(所影响的行数为 3 行)
[解决办法]

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) 

[解决办法]
我的时间长度是按秒来算的,如果要按分,天算都可以 把ss,改为day

热点排行