you might need to use cursor and temporary table
解决方案 »
- 系统过程死锁 求解
- 求一条简单的SQL升级版的改进版-_-!!!
- 谁能详解一下exists同一张表的比较
- 请教一个sql问题?
- SQL Server2000 的Server是不是只能安装在WIN2000 server上?
- 使用bulk insert导入网络文件,老是报“拒绝访问”
- 疑难的序列问题
- 求救高人:SSIS包当中无法保存oracle密码
- 非常棘手的问题,大家帮帮我啊~~
- 这样的SQL语句应该怎么写? ^-^
- 关于SQL Server2000下的 Group by 的用法 请大虾给我一个最终答案吧--天都快亮了 我还是没办法
- MS Sql Server7.0 存储过程中RAISERROR出来的错误我怎样在程序里捕获它? (千山求教)欢迎UP
FROM table1
GROUP BY field1,field2
select t9.field1,t9.field2,sum(field3)
from (
select t1.field1,t1.field2,t2.field3
from test t1,test t2
where t1.field1=t2.field1
and (
t1.field2=t2.field2 or
( datediff(dd,t1.field2,t2.field2)>0
and datediff(dd,t1.field2,t2.field2)=
(select count(*)-1 from test t3 where
t3.field1=t1.field1 and
t3.field2 between t1.field2 and t2.field2)))
and t1.field2 not in
(select t6.field2 from test t5 ,test t6 where t5.field1=t1.field1 and
t5.field1=t6.field1 and t6.field2=dateadd(day,1,t5.field2))
) t9
group by t9.field1,t9.field2
order by t9.field1,t9.field2
2. 建立一个存储过程:先对TAB按时间从大到小排序,设立一个变量用于保存相邻记录的和,每遇到一个不连续点就insert一次。一个循环搞掂。返回RESULT表就是了。看来只能这样了,你想用一条SQL语句看来是不现实的。
from (
select t1.field1,t1.field2,t2.field3
from test t1,test t2
where t1.field1=t2.field1
and (t1.field2=t2.field2 or
(datediff(dd,t1.field2,t2.field2)>0
and datediff(dd,t1.field2,t2.field2)=
(select count(*)-1 from test t3 where
t3.field1=t1.field1 and
t3.field2 between t1.field2 and t2.field2)))
and t1.field2 not in
(select t6.field2 from test t5 ,test t6 where t5.field1=t1.field1 and
t5.field1=t6.field1 and t6.field2=dateadd(day,1,t5.field2))
) t9
group by t9.field1,t9.field2
order by t9.field1,t9.field2