我有一个似乎有效的查询,但它需要永远。我想如果我将查询分成两部分,并将第一个查询的结果传递给第二个,那么它可能会更有效率。
这个想法是,该表包含许多符号的数据。有没有办法只查询表中的那些符号='AAPL',然后将该结果集传递给下一个查询?
这是在SQL Server 2008 R2上。
SELECT
T1.CompSymbol,
T1.TradeDate,
T1.Expiry,
MIN(T2.Expiry) AS 'FrontMonthContract' ,
MIN(T1.CompATMSynthStrike) AS 'CompATMSynthStrike',
MIN(T1.CompATMSynthIV) AS 'CompATMSynthIV'
FROM
BandHistory1M T1, BandHistory1M T2
WHERE T1.TradeDate <= T2.Expiry AND T1.CompSymbol = 'AAPL'
GROUP BY T1.CompSymbol , T1.TradeDate, T1.Expiry
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Data.Linq.Mapping;
namespace ConsoleGetFrontMonth
{
class Program
{
static void Main(string[] args)
{
Dictionary<int, int> frontMonthContract = new Dictionary<int, int>();
frontMonthContract.Add(4, 17);
frontMonthContract.Add(5, 16);
frontMonthContract.Add(6, 20);
frontMonthContract.Add(7, 18);
frontMonthContract.Add(8, 22);
frontMonthContract.Add(9, 19);
frontMonthContract.Add(10, 17);
frontMonthContract.Add(11, 21);
//frontMonthContract.Add(5, 16);
//frontMonthContract.Add(5, 16);
BandsEntities bandsContext = new BandsEntities();
IQueryable<BandHistory1M> symbolQuery =
from source in bandsContext.BandHistory1M
where source.CompSymbol == "AAPL"
select source;
foreach (var Symbol in symbolQuery)
{
int dayExpires;
if(false == frontMonthContract.TryGetValue(Symbol.TradeDate.Month, out dayExpires))
continue;
if (Symbol.TradeDate.Month == Symbol.Expiry.Month)
{
if (Symbol.TradeDate.Day < dayExpires)
{
Console.WriteLine("{0} {1} {2}", Symbol.CompSymbol, Symbol.TradeDate, Symbol.Expiry);
}
}
else if(1 == (Symbol.Expiry.Month - Symbol.TradeDate.Month))
{
if(Symbol.TradeDate.Day > dayExpires)
Console.WriteLine("{0} {1} {2}", Symbol.CompSymbol, Symbol.TradeDate, Symbol.Expiry);
}
}
Console.ReadLine();
}
}
}
答案 0 :(得分:0)
没有Table Defs我只是猜测,但试试这样:
(哎呀,第一个很糟糕......)
或者甚至是这样:
SELECT
T1.CompSymbol,
T1.TradeDate,
T1.Expiry,
MIN(T2.Expiry) AS 'FrontMonthContract' ,
MIN(T1.CompATMSynthStrike) AS 'CompATMSynthStrike',
MIN(T1.CompATMSynthIV) AS 'CompATMSynthIV'
FROM BandHistory1M T1
JOIN BandHistory1M T2 ON T1.TradeDate <= T2.Expiry
GROUP BY T1.CompSymbol , T1.TradeDate, T1.Expiry
HAVING T1.CompSymbol = 'AAPL'
呃,......不,我在上面的评论中说得对。这是臭名昭着的“运行总计”或“triangular join”问题。在客户端代码中很容易做到,但只能在TSQL中使用称为伪焦点或“古怪更新”的极其棘手(且不受支持)的设备高效地完成。