Search code examples
kdb

how do I write a proper query in kdb this case?


I would like to get all the groups that have a distinct price of 0 from my table, IE only if all prices are 0 in that group should it be returned.

My query & table look something like this.

tab:([]grp:`a`b`c`c`a`a`a;price:0 20 0 1 0 0 0)
select grp from tab where distinct price = 0

The output should only be `a since `a is the only group where all prices are 0.


Solution

  • Using an fby is one way to achieve the result here.

    q)tab:([]grp:`a`b`c`c`a`a`a;price:0 20 0 1 0 0 0)
    q)select from tab where 0=(max;abs price)fby grp
    grp price
    ---------
    a   0
    a   0
    a   0
    a   0
    q)distinct exec grp from tab where 0=(max;abs price)fby grp
    ,`a