我有一个包含相关列的表格:
CREATE TABLE [dbo].[PrmsBlotter](
[ID] [int] IDENTITY(1,1) NOT NULL,
[BBox] [varchar](250) NOT NULL,
[RunDateStart] [datetime] NOT NULL,
[RunType] [varchar](50) NOT NULL,
[TestDateFrom] [date] NULL,
...
)
BBox是黑匣子的名称。 RunType可以是Live或Backtesting。每次实时运行都会进行回溯测试,以使回测的TestDateFrom等于Live测试的RunDateStart。
我需要知道是否有任何没有匹配回测的实时运行。我到目前为止:
SELECT t1.BBox, t1.dd, t2.BBox, t2.dd FROM
(SELECT BBox, CONVERT(date,RunDateStart) as dd FROM [PrimusGroup].[dbo].[PrmsBlotter]
WHERE RunType = 'Live') t1
LEFT JOIN
(SELECT BBox, CONVERT(date,TestDateFrom) as dd FROM [PrimusGroup].[dbo].[PrmsBlotter]
WHERE RunType = 'Backtesting') t2
ON (t1.BBox = t2.BBox AND t1.dd = t2.dd)
我正在加入两个查询,一个用于生命,一个用于回测并加入他们的bbox名称相等,而live的rundatestart等于backtest的testdatefrom。那就是我被困住的地方。我不知道怎么说哪里没有比赛。
有办法做到这一点吗?
答案 0 :(得分:1)
使用not exists()
返回'Live'
个测试,但没有对应的'Backtesting'
匹配BBox
和TestDateFrom
到RunDateStart
:
select
t.BBox
, convert(date, t.RunDateStart) as dd
from [PrimusGroup].[dbo].[PrmsBlotter] t
where t.RunType = 'Live'
and not exists (
select 1
from [PrimusGroup].[dbo].[PrmsBlotter] i
where i.RunType = 'Backtesting'
and i.BBox = t.BBox
and convert(date,i.TestDateFrom) = convert(date,t.RunDateStart)
)
答案 1 :(得分:0)
您需要WHERE
子句:
SELECT l.BBox, l.rdday, bt.BBox, bt.btday FROM
FROM (SELECT BBox, CONVERT(date, RunDateStart) as rdday
FROM [PrimusGroup].[dbo].[PrmsBlotter]
WHERE RunType = 'Live'
) l LEFT JOIN
(SELECT BBox, CONVERT(date, TestDateFrom) as btday
FROM [PrimusGroup].[dbo].[PrmsBlotter]
WHERE RunType = 'Backtesting'
) bt
ON l.BBox = bt.BBox AND l.rtday = bt.btday
WHERE bt.BBox IS NULL;
注意:dd
是列的错误名称,因为它是用于datepart()
函数的关键字。
我认为您也可以使用group by
和having
:
SELECT BBox, CONVERT(date, RunDateStart) as rdday
FROM [PrimusGroup].[dbo].[PrmsBlotter]
WHERE RunType IN ('Live', 'BackTesting')
GROUP BY BBox, CONVERT(date, RunDateStart)
HAVING SUM(CASE WHEN RunType = 'Live' THEN 1 ELSE 0 END) > 0 AND
SUM(CASE WHEN RunType = 'BackTesting' THEN 1 ELSE 0 END) = 0 ;