Search code examples
kdb+

Add condition to where clause in q/kdb+


Table Tab

minThreshold maxThreshold point
1000 10000 10

wClause,:enlist((';~:;<);`qty;Tab[`minThreshold])

trying to incorporate maxThreshold column to where clause

qty >= MinThreshold
qty <= MaxThreshold

something like wClause,:enlist((';~:;<);`qty;Tab[`minThreshold]);Tab[`maxThreshold])


Solution

  • q)Tab:([] minThreshold:500 1000;maxThreshold:700 2000;point:5 10)
    q)Tab
    minThreshold maxThreshold point
    -------------------------------
    500          700          5
    1000         2000         10
    q)select from Tab where minThreshold>=900,maxThreshold<=2500
    minThreshold maxThreshold point
    -------------------------------
    1000         2000         10
    q)parse"select from Tab where minThreshold>=900,maxThreshold<=2500"
    ?
    `Tab
    ,(((';~:;<);`minThreshold;900);((';~:;>);`maxThreshold;2500))
    0b
    ()
    q)?[Tab;((>=;`minThreshold;900);(<=;`maxThreshold;2500));0b;()]
    minThreshold maxThreshold point
    -------------------------------
    1000         2000         10
    

    See the whitepaper for more information on functional selects: