Querying Data Using qSQL with Licensing

This page explains how to query your data with Licensing using qSQL.

Querying tables using qSQL

Create a sample table:

Python

Copy
>>> import pykx as kx
>>> trades = kx.Table(data={
        'sym': kx.random.random(100, ['AAPL', 'GOOG', 'MSFT']),
        'date': kx.random.random(100, kx.q('2022.01.01') + [0,1,2]),
        'price': kx.random.random(100, 1000.0
    })

>>> # Assign the table to a named object in q memory to allow name based query later
>>> kx.q['trades'] = trades

Query a table, by name, using qSQL:

Python

Copy
>>> kx.q('select from trades')
pykx.Table(pykx.q('
sym  date       price   
------------------------
GOOG 2022.01.02 805.0147
AAPL 2022.01.03 847.6275
AAPL 2022.01.03 329.8159
GOOG 2022.01.02 982.5155
MSFT 2022.01.02 724.9456
..
'))

Query a pykx.Table passing it as an argument:

Python

Copy
>>> kx.q('{select from x}', trades)
pykx.Table(pykx.q('
sym  date       price   
------------------------
GOOG 2022.01.02 805.0147
AAPL 2022.01.03 847.6275
AAPL 2022.01.03 329.8159
GOOG 2022.01.02 982.5155
MSFT 2022.01.02 724.9456
..
'))

Passing multiple arguments:

Python

Copy
>>> from datetime import date
>>> kx.q('{[x;y] select from trades where date = x, price < y}', date(2022, 1, 2), 500.0)
pykx.Table(pykx.q('
sym  date       price   
------------------------
GOOG 2022.01.02 214.9847
AAPL 2022.01.02 126.2957
AAPL 2022.01.02 184.4151
AAPL 2022.01.02 217.0378
GOOG 2022.01.02 423.6121
..
'))

Next steps

Now that you have learned how to query your data using the Pythonic API you may be interested in other methods for querying your data:

Now that you have learned how to query your data using q you may be interested in other methods for querying your data:

  • If you want to query your data in a more Python-first way follow the guide here.

  • If you wish to query your data using SQL, you can follow the introduction to this functionality here.

  • To learn how to make your queries more performant following the tips and tricks here.

For some further reading, here are some related topics:

  • If you don't have a historical database available see here.

  • To learn about creating PyKX Table objects see here.