I have a table with orders for shares of stocks that looks like this:

[![enter image description here](https://i.stack.imgur.com/jsM0f.png)](https://i.stack.imgur.com/jsM0f.png)

The first column is the ID of the respective stock, the second column is the order price, third column is whether its a bid (buy) or an ask (sell) and finally the the last column is the volume remaining in this order. 

What I'm trying to do is get these values:

* Highest and lowest bid prices and highest and lowest ask prices.
* sum of the volume at highest bid and sum of volume at lowest ask

I realize this is pretty complicated query and it's likely going to involve one (or probably several sub-queries) but what I have so far is this:

```
SELECT
  SYMBOL_CODE as symbol_code,
  MAX(ORDER_PRICE) as highest_bid,
  MIN(ORDER_PRICE) as lowest_bid,
  SUM(VOLUME_TRADED)
FROM ORDERS
WHERE
  ORDER_TYPE = 'buy'
GROUP BY
  SYMBOL_CODE
order by
  SYMBOL_CODE;

```

There are two issues with this as I see it, first of all, it sums the volume of all the prices, not the prices of highest bid orders only, couldnt figure out how to do that and secondly, this only does the bids, I'd have to write another query for the asks. I tried grouping by order\_type but I didn't get the expected result (stocks had 1 or 2 rows in result depending on whether they had a bid/ask order or both)