Search code examples
kdb

KDB/Q query for rows matching criteria per month


I would like to perform a query in Q/KDB that retrieves rows the fulfills the following criteria.

  1. Rows with the same name in a month must only have completed marked.
  2. Names should be distinct i.e. if they fulfill condition 1, they should only appear as a singular entry.

Initial Table:

completed name month
--------------------
yes       x    1    
no        x    1    
yes       y    2
yes       y    2    
no        a    3    
yes       a    4    
yes       b    4    
no        b    4    
no        b    4  
yes       y    5

Initial Table Divvied Up by Months

completed name month
--------------------
yes       x    1    
no        x    1
---------------------    
yes       y    2
yes       y    2    
--------------------
no        a    3    
--------------------
yes       a    4    
yes       b    4    
no        b    4    
no        b    4  
--------------------
yes       y    5

Resultant Table:

completed name month
--------------------
yes       y    2    
yes       a    4 
yes       y    5   

Explanation:

  1. In month 1, only x transacted but has not completed one deal, hence it is not in our final table.

  2. In month 2, only y transacted with both deals being completed, hence it is in our final table.

  3. In month 3, only a transacted but the deal is not completed.

  4. In month 4, both a and b transacted, but since b has uncompleted deals, it is left out.

  5. In month 5, only y transacted, so it is added to the final table.


Solution

  • Hi Carrein you can use the following using the each right adverb and the fby filter

    distinct select from tab where ({all`yes=/:x};completed)fby([]name;month)
    

    Additionally you might want to make use of a Boolean vector for the completed column for simplicity