我想要db中每个EntityCode的最高BID和最低ASK(价格)的表。
以下两组代码返回两个结果集但我还无法找到/弄清楚如何加入它们:
获得最高出价
(SELECT * FROM
(SELECT
Contracts.Orders2Buy.EntityCode,
Contracts.Orders2Buy.Bid,
Contracts.Orders2Buy.NumContractsStillAvailable,
Contracts.Orders2Buy.TimePlaced,
RANK() OVER (PARTITION BY EntityCode
ORDER BY Contracts.Orders2Buy.Bid DESC,
Contracts.Orders2Buy.TimePlaced DESC) AS Rank
FROM
Contracts.Orders2Buy
WHERE
Contracts.Orders2Buy.NumContractsStillAvailable > 0) AS temp
WHERE temp.Rank = 1)
获得最低要求
(SELECT * FROM
( SELECT
Contracts.Orders2Sell.EntityCode,
Contracts.Orders2Sell.Ask,
Contracts.Orders2Sell.NumContractsStillAvailable AS AskVolume,
Contracts.Orders2Sell.TimePlaced,
RANK () OVER ( PARTITION BY EntityCode
ORDER BY Contracts.Orders2Sell.Ask ASC,
Contracts.Orders2Sell.TimePlaced DESC) AS Rank
FROM
Contracts.Orders2Sell
WHERE
Contracts.Orders2Sell.NumContractsStillAvailable > 0) AS temp2
WHERE temp2.Rank = 1)
答案 0 :(得分:0)
使用JOIN将三个结果集合并为一个结果,而不是UNION。
SELECT *
FROM highestBid hb
JOIN lowestAsk la ON hb.stock_symbol = la.stock_symbol
JOIN mostRecent mr ON la.stock_symbol = mr.stock_symbol
将每个集合中所需的字段替换为*
答案 1 :(得分:0)
我想要完成的任务需要加入上面的代码块,如下所示:
SELECT * FROM
(Code block 1) t1
INNER JOIN
(Code block 2) t2
ON t1.EntityCode = t2.EntityCode