I have a sales records table that looks like this:
Name | Item | Action | Price |
---|---|---|---|
Ann | Apple | Buy | 5.00 |
Ann | Apple | Sell | 6.00 |
Ann | Bread | Sell | 9.00 |
Bob | Apple | Buy | 5.00 |
Bob | Salt | Buy | 1.00 |
Bob | Salt | Buy | 1.00 |
Bob | Salt | Sell | 2.00 |
sample_data: ([] Name: `Ann`Ann`Ann`Bob`Bob`Bob`Bob; Item: `Apple`Apple`Bread`Apple`Salt`Salt`Salt; Action: `Buy`Sell`Sell`Buy`Buy`Buy`Sell; Price: 5.00 6.00 9.00 5.00 1.00 1.00 2.00)
My end result is to get the combination of Names and Items that have been both bought and sold at least once.
My thought process is to create a table like the one below, which collects the number of buys and sells for each Name-Item pair:
Name | Item | Buys | Sells |
---|---|---|---|
Ann | Apple | 1 | 1 |
Ann | Bread | 0 | 1 |
Bob | Apple | 1 | 0 |
Bob | Salt | 2 | 1 |
Then, extract only those Name-Item pairs that have >=1 Buys and Sells:
Name | Item |
---|---|
Ann | Apple |
Bob | Salt |
An fby might be the simplest way:
q)select distinct Name,Item from sample_data where({all`Buy`Sell in x};Action)fby([]Name;Item)
Name Item
----------
Ann Apple
Bob Salt