Results 1 to 8 of 8
  1. #1
    udigold1 is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Dec 2010
    Posts
    29

    Select opposing actions only

    I have a table with list of stocks, trading days and order type (buy\sell), And I only want to select just stocks that had both buy and sell orders:



    this is the original database:
    Ticker Date Order Type Quantity
    MSFT 26/5/15 Buy 500
    AAPL 26/5/15 Buy 50
    GOOG 26/5/15 Buy 10
    GE 27/5/15 Sell -100
    AAPL 27/5/15 Sell -20
    BA 27/5/15 Buy 10
    GE 28/5/15 Buy 50












    I want my query to return:
    Ticker Date Order Type Quantity
    AAPL 26/5/15 Buy 50
    GE 27/5/15 Sell -100
    AAPL 27/5/15 Sell -20
    GE 28/5/15 Buy 50









    How Do I do that?
    Thanks

  2. #2
    CJ_London is offline VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,841
    ideally you need a unique id field as well but from the data you supplied this should work - change table and field names to suit

    Code:
    SELECT MyTable.*
    FROM myTableINNER JOIN myTable as Tmp
    ON MyTable.Ticker=Tmp.Ticker
    WHERE (myTable.OrderType='BUY' and Tmp.OrderType='SELL') OR (myTable.OrderType='SELL' and Tmp.OrderType='BUY')

  3. #3
    udigold1 is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Dec 2010
    Posts
    29
    Hi,
    Thanks, but when I run it, it gives me more than 120,000 lines (the database is only 17,000 lines),
    how come?

    thanks

  4. #4
    CJ_London is offline VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,841
    quite possibly

    if you have say 2 buys and 10 sells (total 12 records), then you will get 20 records where a buy has a sell, and 20 records where a sell has a buy - a total of 40 records

    Sounds like you may need to clarify what it is you actually require

  5. #5
    udigold1 is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Dec 2010
    Posts
    29
    Hi,
    I want to get the same result as my example suggests.
    In my example I had a total of 7 records and I want my query to return 4 records.
    And if we go by your example, I want the query to return 12 results (2 buys, 10 sells) not 40...

    thanks!

  6. #6
    CJ_London is offline VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,841
    OK, try changing SELECT to SELECT DISTINCT

  7. #7
    hapm is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2014
    Posts
    197
    Here is a quite different approach using subqueries.

    With having both sell and buy orders you mean sell/buy on the same date or on different dates too?

    Here is it with Date:

    Code:
    SELECT * FROM MyTable INNER JOIN (SELECT Ticker, Date, COUNT([Order Type]) AS OrderTypeCount FROM (SELECT DISTINCT Ticker, Date, [Order Type] FROM MyTable)) AS OrderTypeCountByTickerAndDate ON MyTable.Ticker = OrderTypeCountByTickerAndDate.Ticker AND MyTable.Date = OrderTypeCountByTickerAndDate.Date WHERE OrderTypeCountByTickerAndDate.OrderTypeCount > 1
    and here without:

    Code:
    SELECT * FROM MyTable INNER JOIN (SELECT Ticker, COUNT([Order  Type]) AS OrderTypeCount FROM (SELECT DISTINCT Ticker, [Order  Type] FROM MyTable)) AS OrderTypeCountByTicker ON MyTable.Ticker =  OrderTypeCountByTicker.Ticker WHERE  OrderTypeCountByTicker.OrderTypeCount > 1
    You can split up the query by defining subqueries for the derived tables if you like. Makes it much easier to maintain and understand.

  8. #8
    udigold1 is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Dec 2010
    Posts
    29
    Sorry for taking the time, but wanted to say thank you, it worked!

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Form Actions Not being Recorded
    By CementCarver in forum Forms
    Replies: 2
    Last Post: 06-15-2013, 06:24 PM
  2. User Actions
    By CementCarver in forum Forms
    Replies: 2
    Last Post: 06-15-2013, 06:22 PM
  3. Macro-How get Show all Actions
    By gg80 in forum Macros
    Replies: 4
    Last Post: 05-25-2013, 08:17 AM
  4. Help with actions
    By xdeimusx in forum Access
    Replies: 7
    Last Post: 09-12-2011, 10:34 PM
  5. Macro with 3 actions
    By seweryng in forum Access
    Replies: 4
    Last Post: 01-20-2011, 10:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums