KDB+ Architecture
Q Programming Language
- Q - Message Handler (.Z Library)
- Q - Inter-Process Communication
- Q Language - Queries
- Q Language - Built-in Functions
- Q Language - Functions
- Q Language - Joins
- Q Language - Verb & Adverbs
- Q Language - Table
- Q Language - Dictionaries
- Q Language - Indexing
- Q Language - Lists
- Q Language - Temporal Data
- Q Language - Type Casting
- Q Programming Language
Q Advanced Topics
- Q Language - Maintenance Functions
- Q Language - Tables on Disk
- Q Language - Table Arithmetic
- Q Language - Functional Queries
- Q Language - Attributes
KDB+ Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Q Language - Functional Queries
Functional (Dynamic) queries allow specifying column names as symbols to typical q-sql select/exec/delete columns. It comes very handy when we want to specify column names dynamically.
The functional forms are −
?[t;c;b;a] / for select ![t;c;b;a] / for update
where
t is a table;
a is a dictionary of aggregates;
b the by-phrase; and
c is a pst of constraints.
Note −
All q entities in a, b, and c must be referenced by name, meaning as symbols containing the entity names.
The syntactic forms of select and update are parsed into their equivalent functional forms by the q interpreter, so there is no performance difference between the two forms.
Functional select
The following code block shows how to use functional select −
q)t:([]n:`ibm`msft`samsung`apple;p:40 38 45 54) q)t n p ------------------- ibm 40 msft 38 samsung 45 apple 54 q)select m:max p,s:sum p by name:n from t where p>36, n in `ibm`msft`apple name | m s ------ | --------- apple | 54 54 ibm | 40 40 msft | 38 38
Example 1
Let’s start with the easiest case, the functional version of “select from t” will look pke −
q)?[t;();0b;()] / select from t n p ----------------- ibm 40 msft 38 samsung 45 apple 54
Example 2
In the following example, we use the enpst function to create singletons to ensure that appropriate entities are psts.
q)wherecon: enpst (>;`p;40) q)?[`t;wherecon;0b;()] / select from t where p > 40 n p ---------------- samsung 45 apple 54
Example 3
q)groupby: enpst[`p] ! enpst `p q)selcols: enpst [`n]!enpst `n q)?[ `t;(); groupby;selcols] / select n by p from t p | n ----- | ------- 38 | msft 40 | ibm 45 | samsung 54 | apple
Functional Exec
The functional form of exec is a simppfied form of select.
q)?[t;();();`n] / exec n from t (functional form of exec) `ibm`msft`samsung`apple q)?[t;();`n;`p] / exec p by n from t (functional exec) apple | 54 ibm | 40 msft | 38 samsung | 45
Functional Update
The functional form of update is completely analogous to that of select. In the following example, the use of enpst is to create singletons, to ensure that input entities are psts.
q)c:enpst (>;`p;0) q)b: (enpst `n)!enpst `n q)a: (enpst `p) ! enpst (max;`p) q)![t;c;b;a] n p ------------- ibm 40 msft 38 samsung 45 apple 54
Functional delete
Functional delete is a simppfied form of functional update. Its syntax is as follows −
![t;c;0b;a] / t is a table, c is a pst of where constraints, a is a / pst of column names
Let us now take an example to show how functional delete work −
q)![t; enpst (=;`p; 40); 0b;`symbol$()] / delete from t where p = 40 n p --------------- msft 38 samsung 45 apple 54Advertisements