0

I want one table of the highest BID and lowest ASK (price) for each EntityCode in the db.

The following two sets of code return two result sets I but cannot yet find/figure out how to join them:

Get highest Bid

(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)

Get Lowest Ask

(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)
Beth
  • 9,531
  • 1
  • 24
  • 43
Mike S
  • 157
  • 3
  • 13

2 Answers2

0

Instead of UNION, use JOIN to combine three result sets into one result.

SELECT  *
FROM    highestBid hb
        JOIN lowestAsk la ON hb.stock_symbol = la.stock_symbol
        JOIN mostRecent mr ON la.stock_symbol = mr.stock_symbol

put the fields you need from each set in place of *

JamieD77
  • 13,796
  • 1
  • 17
  • 27
0

What I sought to accomplish requires joining the code blocks above as follows:

SELECT * FROM 

      (Code block 1) t1

INNER JOIN

      (Code block 2) t2

ON t1.EntityCode = t2.EntityCode
Mike S
  • 157
  • 3
  • 13