0

Let's assume that I have a table in KBD named "Automotive" with following data:

Manufacturer    Country             Sales Id
Mercedes        United States       002
Mercedes        Canada              002
Mercedes        Germany             003
Mercedes        Switzerland         003
Mercedes        Japan               004
BMW             United States       002
BMW             Canada              002
BMW             Germany             003
BMW             Switzerland         003
BMW             Japan               004

How would I structure a query in Q such that I can fetch the records matching United States and Canada without using an OR clause?

In SQL, it would look something like:

SELECT Manufacturer, Country from Automotive WHERE Country IN ('United States', 'Canada')

Thanks in advance for helping this Q beginner!

equanimity
  • 2,371
  • 3
  • 29
  • 53
  • The official documentation page for `in` is pretty clear, this site should be your manual! https://code.kx.com/q/ref/in/ – terrylynch Nov 25 '20 at 10:47

1 Answers1

3

It's basically the same in kdb. The way you write you query depends on the data type. See below an example where manufacturer is a symbol, and country is a string.

q)tbl:([]manufacturer:`Merc`Merc`BMW`BMW`BMW;country:("United States";"Canada";"United States";"Germany";"Japan");ID:til 5)
q)
q)tbl
manufacturer country         ID
-------------------------------
Merc         "United States" 0
Merc         "Canada"        1
BMW          "United States" 2
BMW          "Germany"       3
BMW          "Japan"         4
q)meta tbl
c           | t f a
------------| -----
manufacturer| s
country     | C
ID          | j
q)select from tbl where manufacturer in `Merc`Ford
manufacturer country         ID
-------------------------------
Merc         "United States" 0
Merc         "Canada"        1
q)
q)select from tbl where country in ("United States";"Canada")
manufacturer country         ID
-------------------------------
Merc         "United States" 0
Merc         "Canada"        1
BMW          "United States" 2

Check out how to use Q-sql here: https://code.kx.com/q4m3/9_Queries_q-sql/

CWD
  • 323
  • 1
  • 6