股票,时间,字段1,字段2
SH600000 2011-04-12 09:33:41.000 18729 27159500.00
SH600000 2011-04-12 09:33:46.000 18766 27213370.00
SH600000 2011-04-12 09:33:51.000 18967 27506040.00
SH600001 2011-04-12 09:33:56.000 19047 27622470.00
SH600001 2011-04-12 09:34:01.000 19465 28230780.00
SH600002 2011-04-12 09:34:06.000 19558 28366180.00
SH600003 2011-04-12 09:34:11.000 19838 28774780.00
SH600004 2011-04-12 09:34:16.000 19881 28837220.00
SH600005 2011-04-12 09:34:21.000 19996 29005120.00
------------------------------------------
怎么统计相同股票,时间相隔最短,的字段1的差额和字段2的差额,这个表的数据量大概200w,要 把统计的结果快速插入另外一个表,求问比较快速的插入统计方法
SH600000 2011-04-12 09:33:41.000 18729 27159500.00
SH600000 2011-04-12 09:33:46.000 18766 27213370.00
SH600000 2011-04-12 09:33:51.000 18967 27506040.00
SH600001 2011-04-12 09:33:56.000 19047 27622470.00
SH600001 2011-04-12 09:34:01.000 19465 28230780.00
SH600002 2011-04-12 09:34:06.000 19558 28366180.00
SH600003 2011-04-12 09:34:11.000 19838 28774780.00
SH600004 2011-04-12 09:34:16.000 19881 28837220.00
SH600005 2011-04-12 09:34:21.000 19996 29005120.00
------------------------------------------
怎么统计相同股票,时间相隔最短,的字段1的差额和字段2的差额,这个表的数据量大概200w,要 把统计的结果快速插入另外一个表,求问比较快速的插入统计方法
SH600000 2011-04-12 09:33:41.000 18729 27159500.00
SH600000 2011-04-12 09:33:46.000 18766 27213370.00
SH600000 2011-04-12 09:33:51.000 18967 27506040.00都是相差五秒,去哪两条来计算?
SH600000 2011-04-12 09:33:41.000 18729 27159500.00 ------A
SH600000 2011-04-12 09:33:46.000 18766 27213370.00 ------B
SH600000 2011-04-12 09:33:51.000 18967 27506040.00 -------C
---
比如A和B,b和c,
a没有上一条记录,就a当做一条统计结果
--准备
create table tb(股票 varchar(50),时间 datetime,字段1 int ,字段2 decimal)
insert into tb select 'SH600000' ,'2011-04-12 09:33:41.000', 18729,27159500.00
union all select 'SH600000','2011-04-12 09:33:46.000', 18766, 27213370.00
union all select 'SH600000' ,'2011-04-12 09:33:51.000', 18967, 27506040.00
union all select 'SH600001' ,'2011-04-12 09:33:56.000', 19047, 27622470.00
union all select 'SH600001' ,'2011-04-12 09:34:01.000', 19465, 28230780.00
union all select 'SH600002' ,'2011-04-12 09:34:06.000', 19558, 28366180.00
union all select 'SH600003' ,'2011-04-12 09:34:11.000', 19838, 28774780.00
union all select 'SH600004' ,'2011-04-12 09:34:16.000', 19881, 28837220.00
union all select 'SH600005' ,'2011-04-12 09:34:21.000', 19996, 29005120.00select * from tb
--SQL
with
cr as
(select a.*,b.[字段1] as [b字段1],b.[字段2] as [b字段2],b.[时间] as [b时间] from tb as a ,tb as b where a.[股票]=b.[股票]),
--select * from cr
cr1 as
(select a.[股票]
,[时间],[b时间]
,abs((datediff(ss,[时间],[b时间]))) as [时间差(秒)]
,[字段1],[b字段1]
, abs([字段1]-[b字段1]) as [字段1差值]
,[字段2],[b字段2]
,abs([字段2]-[b字段2]) as [字段2差值]
from cr as a where not exists
(select * from cr where a.[股票] = [股票] and (datediff(ss,a.[时间],a.[b时间]) >datediff(ss,[时间],[b时间]))))
--select * from cr1 --cr1是筛选出来的最后结果--添加到另一个表里面(前提这个表是不存在的。存在的话用INSET INTO)
select * into tr from cr1 --插入到 tr表select * from tr --查看结果/*
--如果目标表存在的话
insert into 已经存在的目标表 select * from cr1
select * from 已经存在的目标表
*/
/*
股票 时间 b时间 时间差(秒) 字段1 b字段1 字段1差值 字段2 b字段2 字段二差值
-------------------------------------------------------------------------------------------------------------------------------
SH600000 2011-04-12 09:33:51.000 2011-04-12 09:33:41.000 10 18967 18729 238 27506040 27159500 346540
SH600001 2011-04-12 09:34:01.000 2011-04-12 09:33:56.000 5 19465 19047 418 28230780 27622470 608310
SH600002 2011-04-12 09:34:06.000 2011-04-12 09:34:06.000 0 19558 19558 0 28366180 28366180 0
SH600003 2011-04-12 09:34:11.000 2011-04-12 09:34:11.000 0 19838 19838 0 28774780 28774780 0
SH600004 2011-04-12 09:34:16.000 2011-04-12 09:34:16.000 0 19881 19881 0 28837220 28837220 0
SH600005 2011-04-12 09:34:21.000 2011-04-12 09:34:21.000 0 19996 19996 0 29005120 29005120 0*/
(
股票 VARCHAR(10),
时间 DATETIME,
字段1 INT,
字段2 DECIMAL(18, 2)
)
INSERT #temp
select 'SH600000', '2011-04-12 09:33:41.000', '18729', '27159500.00' union all
select 'SH600000', '2011-04-12 09:33:46.000', '18766', '27213370.00' union all
select 'SH600000', '2011-04-12 09:33:51.000', '18967', '27506040.00' union all
select 'SH600001', '2011-04-12 09:33:56.000', '19047', '27622470.00' union all
select 'SH600001', '2011-04-12 09:34:01.000', '19465', '28230780.00' union all
select 'SH600002', '2011-04-12 09:34:06.000', '19558', '28366180.00' union all
select 'SH600003', '2011-04-12 09:34:11.000', '19838', '28774780.00' union all
select 'SH600004', '2011-04-12 09:34:16.000', '19881', '28837220.00' union all
select 'SH600005', '2011-04-12 09:34:21.000', '19996', '29005120.00'
GO
--SQL:
;WITH
cte_1 AS
(SELECT rowno = ROW_NUMBER() OVER(PARTITION BY 股票 ORDER BY 时间), * FROM #temp),
cte_2 AS
(SELECT rowno = ROW_NUMBER() OVER(PARTITION BY 股票 ORDER BY 时间), * FROM #temp),
cte_3 AS
(
SELECT A.股票, A.rowno, diff=DATEDIFF(SECOND, A.时间, B.时间), 字段1差额=B.字段1-A.字段1, 字段2差额=B.字段2-A.字段2
FROM cte_1 A
INNER JOIN cte_2 B
ON A.股票 = B.股票 AND A.rowno = B.rowno-1
)
SELECT * FROM
(SELECT DISTINCT 股票 FROM cte_3) A
CROSS APPLY
(SELECT TOP(1) 股票, 字段1差额, 字段2差额 FROM cte_3 WHERE 股票 = A.股票 ORDER BY diff DESC, rowno DESC) B
/*
股票 股票 字段1差额 字段2差额
---------- ---------- ----------- ---------------------------------------
SH600000 SH600000 201 292670.00
SH600001 SH600001 418 608310.00
*/
create table tb(股票 varchar(50),时间 datetime,字段1 int ,字段2 decimal)
insert into tb select 'SH600000' ,'2011-04-12 09:33:41.000', 18729,27159500.00
union all select 'SH600000','2011-04-12 09:33:46.000', 18766, 27213370.00
union all select 'SH600000' ,'2011-04-12 09:33:51.000', 18967, 27506040.00
union all select 'SH600001' ,'2011-04-12 09:33:56.000', 19047, 27622470.00
union all select 'SH600001' ,'2011-04-12 09:34:01.000', 19465, 28230780.00
union all select 'SH600002' ,'2011-04-12 09:34:06.000', 19558, 28366180.00
union all select 'SH600003' ,'2011-04-12 09:34:11.000', 19838, 28774780.00
union all select 'SH600004' ,'2011-04-12 09:34:16.000', 19881, 28837220.00
union all select 'SH600005' ,'2011-04-12 09:34:21.000', 19996, 29005120.00select * from tb
--SQL
with
cr as
(select a.*,b.[字段1] as [b字段1],b.[字段2] as [b字段2],b.[时间] as [b时间],abs(datediff(ss,a.[时间],b.[时间])) as [时间差]
from tb as a , tb as b where a.[股票]=b.[股票] and (a.[时间]>b.[时间] or (select count(1) from tb where [股票]=a.[股票])=1 ))
,cr1 as
(select
a.[股票]
,[时间],[b时间]
,abs((datediff(ss,[时间],[b时间]))) as [时间差(秒)]
,[字段1],[b字段1]
, abs([字段1]-[b字段1]) as [字段1差值]
,[字段2],[b字段2]
,abs([字段2]-[b字段2]) as [字段2差值]
from cr as a where not exists
(select top 1 * from cr where a.[股票] = [股票] and a.[时间差]> [时间差]))
--select * from cr1 --cr1是筛选出来的最后结果--添加到另一个表里面(前提这个表是不存在的。存在的话用INSET INTO)
select * into tr from cr1 --插入到 tr表
select * from tr --查看结果/*
--如果目标表存在的话
insert into 已经存在的目标表 select * from cr1
select * from 已经存在的目标表
*/
/*
股票 时间 b时间 时间差(秒) 字段1 b字段1 字段1差值 字段2 b字段2 字段二差值
-------------------------------------------------------------------------------------------------------------------------------
SH600000 2011-04-12 09:33:46.000 2011-04-12 09:33:41.000 5 18766 18729 37 27213370 27159500 53870
SH600000 2011-04-12 09:33:51.000 2011-04-12 09:33:46.000 5 18967 18766 201 27506040 27213370 292670
SH600001 2011-04-12 09:34:01.000 2011-04-12 09:33:56.000 5 19465 19047 418 28230780 27622470 608310
SH600002 2011-04-12 09:34:06.000 2011-04-12 09:34:06.000 0 19558 19558 0 28366180 28366180 0
SH600003 2011-04-12 09:34:11.000 2011-04-12 09:34:11.000 0 19838 19838 0 28774780 28774780 0
SH600004 2011-04-12 09:34:16.000 2011-04-12 09:34:16.000 0 19881 19881 0 28837220 28837220 0
SH600005 2011-04-12 09:34:21.000 2011-04-12 09:34:21.000 0 19996 19996 0 29005120 29005120 0*/