select tmp=0,temp=0,intime=convert(datetime,0,120) ,cardNo,tT,iDU into #T from t_ca
where tT>='2010-09-14' and tT<='2010-09-20' order by cardNo,tTdeclare @tmp int,@temp int,@biaozhi int,@name varchar(10),@tT datetime,@intime datetime
Update #T
set @tmp= case when (iDU=1) and (@biaozhi>1) and cardNo=@name then 1
else case when (iDU>1) and (@biaozhi=1) and cardNo=@name then 2 else 0 end end,
@temp = case when @tmp=1 then isnull(@temp,0)+1 else isnull(@temp,0) end,
@intime= case when (@biaozhi>1)and (iDU=1) and @name=cardNo then @tT else tT end,
@name=cardNo,@biaozhi=iDU,tmp=@tmp,temp=@temp,@tT=tT,intime=@intimeselect cardNo as 卡号,min(case when tmp=1 then intime end) as 开始时间,
max(case when tmp=2 then tT end) as 结束时间
into #1
from #T where tmp>0 group by temp,cardNoselect convert(varchar(10),开始时间,2) as 日期,卡号,convert(varchar(20),开始时间,120) as 开始时间,
convert(varchar(20),结束时间,120) as 结束时间 from #1 where 结束时间>='2010-09-15' and 结束时间<='2010-09-16'
order by 卡号,下井时间
drop table #T,#T1,#1这个效率是不是很差?请问大哥们该如何改正?
解决方案 »
- 求一sql语句
- 两个查询,帮我看看谢谢
- 现有sqlserver6.5下的数据库备份一个,如何导入到2000中?
- 简单的储存问题
- SQL Server数据表数据量达到多大会影响查询和插入效率
- 挑战T-SQL 高手,sql server高手请帮看这个要求能否用sql实现,大送分
- 求助:使用BULK INSERT导入CSV文件到SQL SERVER 2005中遇到数据重复问题
- 怎样把int转化成定长的varchar?
- SQL的问题,求助,请多多帮忙!
- 怎样把n个小书店的数目信息汇集到一个本地数据库,然后发布到租赁的虚拟主机上的数据库。
- 请教sql语句疑难问题
- SSIS读取文件并把数据存入数据库,在线等!急
1 2010-09-09 08:12 1
1 2010-09-10 08:14 1
1 2010-09-10 08:18 2
1 2010-09-09 05:12 2
1 2010-09-09 08:12 1
1 2010-09-09 03:12 1
2 2010-09-09 08:30 1
2 2010-09-09 12:12 2
2 2010-09-09 03:12 1
2 2010-09-09 08:12 2这样类似的数据。希望得到的是每个cardNo当字段iDU发生变化时的tT,当iDU从1变成2的tT和iDU从2变成1的tT.
类似这样的卡号 开始时间 结束时间
1 2010-09-10 08:14 2010-09-09 08:12
2 2010-09-09 08:30 2010-09-09 03:12
2 2010-09-09 03:12 -
case when @tmp=1 then isnull(@temp,0)+1 else isnull(@temp,0) end
这样的SQL语句,效率是很差的。
还有我这个SQL用到了三个临时表,数据量大时,对他们进行插入操作效率也挺差的。怎么改进?
if object_id('tempdb.dbo.#tb') is not null drop table #tb
go
create table #tb (cardNo int,tT datetime,iDU int)
insert into #tb
select 1,'2010-09-09 08:12',1 union all
select 1,'2010-09-10 08:14',1 union all
select 1,'2010-09-10 08:18',2 union all
select 1,'2010-09-19 05:12',2 union all
select 1,'2010-09-19 08:12',1 union all
select 1,'2010-09-19 13:12',1 union all
select 2,'2010-09-09 08:30',1 union all
select 2,'2010-09-09 12:12',2 union all
select 2,'2010-09-09 13:12',1 union all
select 2,'2010-09-09 18:12',2;with cte as
(
select px=row_number()over(order by getdate()),* from #tb
)
select cardNo,
开始时间=min(tT), 结束时间=max(tt)from cte t
where iDU=1
and not exists(select 1 from cte where idu=1 and cardNo=t.cardNo and px=t.px+1)
group by cardNo
cardNo 开始时间 结束时间
----------- ----------------------- -----------------------
1 2010-09-10 08:14:00.000 2010-09-19 13:12:00.000
2 2010-09-09 08:30:00.000 2010-09-09 13:12:00.000(2 行受影响)
with cte as
代替临时表
@temp = case when @tmp=1 then isnull(@temp,0)+1 else isnull(@temp,0) end,
另外,如果你想知道每个语句执行多长时间,可以使用:
Print convert(varchar,getdate(),121)