[英]Combining 2 SQL SELECT queries Into one
I have (2) tables (1) is Prices
and the other is Orders
我有(2)个表(1)是Prices
,另一个是Orders
Prices: 价格:
Symbol varchar
Open decimal
High decimal
Low decimal
Date DateTime
Orders: 命令:
Ticker varchar
Enter decimal
EntDate datetime
Exit decimal
ExtDate datetime
I'd like a query that give me all the pricing data for a specific datetime range and all the orders for that symbol for that same datetime range: 我想要一个查询,该查询可以为我提供特定日期时间范围内的所有定价数据以及该符号在相同日期时间范围内的所有订单:
SELECT
T1.Symbol
, P1.Open
, P1.High
, P1.Low
, P1.Close
, P1.Date
, O1.EntDate
, O1.Enter
, O1.ExtDate
, O1.Exit
FROM Prices AS P1
INNER JOIN ORDERS AS O1 ON O1.Ticker = P1.Symbol
WHERE P1.Date < CONVERT(datetime, '01/01/2012 10:00 AM')
Obviously this just does not work, I get multiple listings for Orders repeated for every line of Price Data. 显然,这是行不通的,对于价格数据的每一行,我都会获得多个重复的订单清单。
Example Prices Table 价格表示例
Sym Open High Low Close Date
ABC 1 3 1 2 1/1/2011 10:01 AM
ABC 1 3 1 2 1/1/2011 10:02 AM
ABC 1 3 1 2 1/1/2011 10:03 AM
ABC 1 3 1 2 1/1/2011 10:04 AM
ABC 1 3 1 2 1/1/2011 10:05 AM
ABC 1 3 1 2 1/1/2011 10:06 AM
ABC 1 3 1 2 1/1/2011 10:07 AM
ABC 1 3 1 2 1/1/2011 10:08 AM
ABC 1 3 1 2 1/1/2011 10:09 AM
ABC 1 3 1 2 1/1/2011 10:10 AM
Example Orders Table 订单表示例
Sym Enter EntDate Exit ExtDate
ABC 1 1/1/2011 10:-00 3 1/1/2011 10:02 AM
ABC 1 1/1/2011 10:-03 3 1/1/2011 10:04 AM
Example Output for Prices and Orders Query with Date and EntDate < 1/1/2011 10:07 AM AND Symbol = ABC 日期和查询日期<1/1/2011 10:07 AM的价格和订单查询的示例输出,且符号= ABC
Sym Open High Low Close Date Enter EntDate Exit ExtDate
ABC 1 3 1 2 1/1/2011 10:01 AM 1 1/1/2011 10:-00 3 1/1/2011 10:02 AM
ABC 1 3 1 2 1/1/2011 10:02 AM 1 1/1/2011 10:-03 3 1/1/2011 10:04 AM
ABC 1 3 1 2 1/1/2011 10:03 AM
ABC 1 3 1 2 1/1/2011 10:04 AM
ABC 1 3 1 2 1/1/2011 10:05 AM
ABC 1 3 1 2 1/1/2011 10:06 AM
ABC 1 3 1 2 1/1/2011 10:07 AM
您需要拥有P1.Symbol而不是T1.Symbol
Consider joining only the orders for the same day as the Price
row: 考虑只加入与Price
行同一天的订单:
from Prices as P1
left join
Orders as O1
on O1.Ticker = P1.Symbol
and p1.Date <= O1.ExtDate and O1.ExtDate < dateadd(day,1,p1.Date)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.