我正在尝试查找货币对的开盘价、收盘价、最高价、最低价。我得到了它的工作,但想知道是否有更简单的方法来做到这一点。
我的源表如下所示:
CREATE TABLE [dbo].[RATES](
[SYSID] [bigint] IDENTITY(1,1) NOT NULL,
[Item] [nvarchar](255) NULL,
[Bid] [float] NULL,
[Ask] [float] NULL,
[Spread] [float] NULL,
[DT] [datetime] NULL
) ON [PRIMARY]
这是一些示例数据
INSERT INTO dbo.RATES (ITEM,BID,ASK,SPREAD,DT) VALUE ('USD/CAD',1.04805,1.04828,0.000230000000000175,'8/29/2013 5:02')
INSERT INTO dbo.RATES (ITEM,BID,ASK,SPREAD,DT) VALUE ('USD/CAD',1.04808,1.04832,0.000240000000000018,'8/29/2013 5:02')
INSERT INTO dbo.RATES (ITEM,BID,ASK,SPREAD,DT) VALUE ('USD/CAD',1.04808,1.04831,0.000230000000000175,'8/29/2013 5:02')
INSERT INTO dbo.RATES (ITEM,BID,ASK,SPREAD,DT) VALUE ('USD/CAD',1.04808,1.04832,0.000240000000000018,'8/29/2013 5:02')
INSERT INTO dbo.RATES (ITEM,BID,ASK,SPREAD,DT) VALUE ('USD/CAD',1.04808,1.04833,0.000250000000000083,'8/29/2013 5:02')
INSERT INTO dbo.RATES (ITEM,BID,ASK,SPREAD,DT) VALUE ('USD/CAD',1.04808,1.04832,0.000240000000000018,'8/29/2013 5:02')
INSERT INTO dbo.RATES (ITEM,BID,ASK,SPREAD,DT) VALUE ('USD/CAD',1.04802,1.04827,0.000250000000000083,'8/29/2013 5:02')
INSERT INTO dbo.RATES (ITEM,BID,ASK,SPREAD,DT) VALUE ('USD/CAD',1.04806,1.04831,0.000250000000000083,'8/29/2013 5:02')
INSERT INTO dbo.RATES (ITEM,BID,ASK,SPREAD,DT) VALUE ('USD/CAD',1.04808,1.04833,0.000250000000000083,'8/29/2013 5:02')
INSERT INTO dbo.RATES (ITEM,BID,ASK,SPREAD,DT) VALUE ('USD/CAD',1.04801,1.04827,0.000259999999999927,'8/29/2013 5:02')
INSERT INTO dbo.RATES (ITEM,BID,ASK,SPREAD,DT) VALUE ('USD/CAD',1.04806,1.04831,0.000250000000000083,'8/29/2013 5:02')
INSERT INTO dbo.RATES (ITEM,BID,ASK,SPREAD,DT) VALUE ('USD/CAD',1.04806,1.04832,0.000259999999999927,'8/29/2013 5:02')
INSERT INTO dbo.RATES (ITEM,BID,ASK,SPREAD,DT) VALUE ('USD/CAD',1.04808,1.04832,0.000240000000000018,'8/29/2013 5:03')
INSERT INTO dbo.RATES (ITEM,BID,ASK,SPREAD,DT) VALUE ('USD/CAD',1.04808,1.04833,0.000250000000000083,'8/29/2013 5:03')
INSERT INTO dbo.RATES (ITEM,BID,ASK,SPREAD,DT) VALUE ('USD/CAD',1.04808,1.04829,0.000210000000000043,'8/29/2013 5:03')
INSERT INTO dbo.RATES (ITEM,BID,ASK,SPREAD,DT) VALUE ('USD/CAD',1.04808,1.04833,0.000250000000000083,'8/29/2013 5:03')
INSERT INTO dbo.RATES (ITEM,BID,ASK,SPREAD,DT) VALUE ('USD/CAD',1.04809,1.04833,0.000240000000000018,'8/29/2013 5:03')
INSERT INTO dbo.RATES (ITEM,BID,ASK,SPREAD,DT) VALUE ('USD/CAD',1.04809,1.0483,0.000210000000000043,'8/29/2013 5:03')
INSERT INTO dbo.RATES (ITEM,BID,ASK,SPREAD,DT) VALUE ('USD/CAD',1.04809,1.04833,0.000240000000000018,'8/29/2013 5:03')
INSERT INTO dbo.RATES (ITEM,BID,ASK,SPREAD,DT) VALUE ('USD/CAD',1.04809,1.04832,0.000229999999999952,'8/29/2013 5:03')
INSERT INTO dbo.RATES (ITEM,BID,ASK,SPREAD,DT) VALUE ('USD/CAD',1.04809,1.04833,0.000240000000000018,'8/29/2013 5:03')
INSERT INTO dbo.RATES (ITEM,BID,ASK,SPREAD,DT) VALUE ('USD/CAD',1.04807,1.04833,0.000259999999999927,'8/29/2013 5:04')
INSERT INTO dbo.RATES (ITEM,BID,ASK,SPREAD,DT) VALUE ('USD/CAD',1.04809,1.04828,0.000190000000000135,'8/29/2013 5:04')
INSERT INTO dbo.RATES (ITEM,BID,ASK,SPREAD,DT) VALUE ('USD/CAD',1.04809,1.04833,0.000240000000000018,'8/29/2013 5:04')
INSERT INTO dbo.RATES (ITEM,BID,ASK,SPREAD,DT) VALUE ('USD/CAD',1.04809,1.04829,0.000199999999999978,'8/29/2013 5:04')
我的查询看起来像这样
DECLARE @OPENCLOSE AS TABLE (
GRP INT,
TF DATETIME,
DT DATETIME,
ASK FLOAT
)
DECLARE @CLOSE_ID AS TABLE (
TF DATETIME,
GRP INT
)
INSERT INTO @OPENCLOSE (GRP,TF,DT,ASK)
select
ROW_NUMBER() OVER (PARTITION BY dateadd(mi, datediff(mi, 0, r.DT), 0) ORDER BY r.DT) AS GRP,
dateadd(mi, datediff(mi, 0, r.DT), 0) AS TF,
r.DT,
r.Ask
from dbo.RATES r
group by dateadd(mi, datediff(mi, 0, r.DT), 0), r.DT,r.Ask
ORDER BY dateadd(mi, datediff(mi, 0, r.DT), 0)
INSERT INTO @CLOSE_ID
SELECT
TF,
MAX(GRP) AS CLOSE_ID
FROM @OPENCLOSE
group by TF
SELECT X.TF,X.O,X.C,Y.HIGH,Y.LOW
FROM (
SELECT
oc1.TF,
oc2.ASK AS [O],
oc1.ASK AS [C]
FROM @OPENCLOSE oc1
JOIN @OPENCLOSE oc2
ON oc2.TF = oc1.TF
AND oc2.GRP = 1
JOIN @CLOSE_ID cid
ON cid.TF = oc1.TF
AND cid.GRP = oc1.GRP
GROUP BY oc1.TF,Oc2.ASK,oc1.ASK
) x
JOIN
(
SELECT
oc1.TF,
MAX(oc1.ASK) AS HIGH,
MIN(oc1.ASK) AS LOW
FROM @OPENCLOSE oc1
GROUP BY oc1.TF
) y ON y.TF = x.TF