skip to Main Content

I am trying to find out unique rows from the given data

In my case I am storing buy sell of the stock in stock table. So same client can buy sell same stock.

So ask is to just fetch stocks which are only BUY and not SELL by any other client.

Here is the sqlFiddle link where I have shared table structure and query I tried
http://sqlfiddle.com/#!9/df117d/2

I am expecting result as –

STOCKB
STOCKD
STOCKE

2

Answers


  1. You can do:

    SELECT DISTINCT s.symbol 
    FROM bulk s 
    WHERE s.buy_sell='BUY' AND 'SELL' not in (
      SELECT a.buy_sell
      FROM bulk a
      WHERE a.symbol = s.symbol AND a.buy_sell='SELL'
    )
    

    See SQLFiddle

    Login or Signup to reply.
  2. This is an other way to do it using group by and having clause, where total should equal total rows with flag BUY :

    select symbol
    from bulk
    group by symbol
    having sum(buy_sell = 'BUY') = count(*)
    

    Demo here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search