/ jq, an implementation of q on the JVM
/ Warning first ever load will take up to 50 seconds.
/ Within this editor:
/ Press Control+Enter to run a line
/ Press Control+E to run the highlighted text
/ It supports most the simple mathematical operations
2+2
10%2
// Both against atoms and lists
1 2 3 - 100 200 10
100 * 1 2 3
where (0=til[1000] mod 5) or 0=til[1000] mod 5
/ storing values
a:13
b:til 10
a + based
// Many of the types are supported
.z.t
.z.d
ts:2019.01.13D12:22:11.1234
`minute$ts
`second$ts
// These behave as you would expect
2013.07.17+1
08:01 + 1 2 3
// Almost all casting and parsing
`int$1.5 2.9
`$"hello world"
// A subset of operations
type `ppp`ooo
l:20?20
l
first l
l 2 3 2 2 / and indexing
/ We can create dictionaries
`a`b`c!1 2 3
/ of various types
`pp`oo`ii!(100 200; 0.1 0.2 0.3; "asda")
type `pp`oo`ii!(100 200; 0.1 0.2 0.3; "asda")
/ and tables
t:([] date:2030.01.01+0 1 2; sym:`pp`oo`ii; time:09:00+ 1 2 3; v:3?10)
t
/ What you are seeing within the web version
/ is a javascript based JVM running the q application.
/ The full version can be downloaded from:
/ http://timestored.com/jq
`a set 13; get "a"
@[neg;1]
a:*[;3];a 2
.[*;2 3]
asc 3 1 2
null 3 0N 1 2
/ Strings
upper "test"
lower "TEst"
// Boolean logic
1100b and 1010b
all 111b
any 0001b
-1^1.1 2.2 0n 4.4 0n 5.5
system "cd"
\cd
/ ########## TimeStored Training Course - Getting Started #######
/ http://www.timestored.com/kdb-training
/ type math expressions directly into kdb
10+3
900 - 88
13*2
90%20 / division is the percentage symbol
/ Expressions are evaluated right to left
100%10+10
/ Defining a variable
a:13
a
b:10*9
b
a+b
c:a+b
c
/ comments must be space then slash then comment
/ within a script there's another format / on new line then closed later
b: 1 / comments
b: 1 / whitespace doesn't matter in kdb
/ What objects exist on the server
\v / variables
\a / tables
system "v"
system "a"
/ can alter settings e.g. Precision and Console size
\P
\c
\c 22 88
\l script.q
/ If the slash command isn't recognised as a kdb call
/ it's passed to the underlying OS, e.g. dos commands
\cd
\echo test
system "cd"
/ See function list in timestored booklet
\\
exit 0;
/ ########## TimeStored Training Course - Casting and Parsing #######
/ http://www.timestored.com/kdb-training
/ ######## Casting ########
/ The $ symbol is used to perform casting from one native
/ type to another in kdb. The right hand side contains our value,
/ The left hand specifies the type we want to cast to,
/ this can be specified in three ways:
/ 1. Type Symbol name
/ 2. Type char
/ 3. Type short number
`int$7.3
"i"$7.3
6h$7.3
n:7.321
`long$n
"j"$n
7h$n
/ casting rounds integers
"j"$7.5
"j"$7.4
/ depends on binary representation
"j"$7.49999999999999999999999999999999
"e"$8
"f"$8
/ When casting to some types the behaviour may not be intuitive
/ When casting to boolean, Zero/0 is false, everything else is true.
`boolean$9 / anything non-zero is true
`boolean$-9
`boolean$0
/ #### Dates / Times
/ Earlier we saw `int$date returned the underlying value of the bytes. i.e.
/ Dates - number of days since 1st January 2000
/ Times - number of milliseconds since midnight
`int$2000.01.05
`int$00:30:59
/ these can in fact be cast back to date/time etc.
/ where the integer number is the number of units of the smallest measure
`date$4
`second$1859
`time$2
`minute$2
`second$2
`timestamp$2
`timespan$2
/ #### Chars / Symbols ####
`int$"A" / casting char to int returns ascii value
`char$65 / reversible
/ kdb will attempt to convert almost anything, while rarely throwing an error
`time$-1.4 / floats are rounded
`time$"sad" / characters to time!?
`time$`asd / gives an error
/ operations on date time types work on the underlying numeric values
/ types are automatically converted to the more complex type.
2013.07.17+1
2013.07.17+2
09:30:22 + 18
1b+13
2013.08.09D13:05:24.237584000 + 100
/ ######## Parsing ########
/ parsing from strings, use upper case letter
"I"$"99"
"J"$"99"
"E"$"99.5"
"F"$"99.5"
/ dates and times are the same format as expected.
dayb:"2013.07.17"
"D"$dayb;
"D"$"2013-07-17"; / accepts hyphens aswell as dots in dates.
"T"$"10:42:27.824"
/ if we tried the lowercase, it attempts to convert each character to a number instead.
"i"$"99"
"e"$"99.5"
/ any items which fail to parse return a null rather than fail.
"J"$"99.4"
"M"$"2013.03.14"
/ ########## TimeStored Training Course - Primitive Operations #######
/ http://www.timestored.com/kdb-training
show `pppppp
tables[]
system "p"
\p
/### dyadic functions
1 + 3
/ single atom - expanded to list size
1 + 0 2 4
10 20 30 40 + 15
/ lists of equal length apply item wise
1 2 3 4 + 10 20 30 40
/### relational / logical
15 <= 10 20 30 15
15 < 10 20 30 15
15 > 10 20 30 15
15 >= 10 20 30 15
any 15 < 10 20 30 15
not any 15 < 10 20 30 15
all 15 < 10 20 30 15
// where converts boolean list to list of indices where true
l:10 20 30 15
15<l
where 15<l
l where 15<l
1100b and 1010b
1100b or 1010b
1100b ^ 1010b
/### match / equals
/ equals performs an item-wise comparison
2 = 1 2 3 4 5 4 3 2 1
1 2 3 = 1 2 3
/ equals is tolerant of type and slight differences
1 2 3 = 1 2 3.0
3=1 2 3.00000000000001
/ match checks for full equality of entire object
2 ~ 1 2 3
1 2 3 ~ 1 2 3
/ match demands types are the same
1 2 3 ~ 1 2 3.0
/ this also ensures exact equality
/ equals is tolerant
all 1 2 3.00000000000001 = 1 2 3
1 2 3.00000000000001 ~ 1 2 3
// math
l: 10*til 9
max l
min l
med l
avg l
log l
sqrt l
2 xexp 1 2 3 4 5
1 2 3 4 5 6 7 mod 3
sums l
prds l
3 msum l
3 mavg l
// sets
l: 10 20 30 40 50
k: 20 40 100 1000
l in k
l except k
l inter k
distinct 10 20 20 10 40 30 10
/ general list functions
raze (1 2 3; 60 70 80)
reverse (1 2 3; `A`B`C)
reverse each (1 2 3; `A`B`C)
reverse "hello world"
count 20 10 30
// string maniuplation
string 10 20 30
"hello aa where is aa" ss "aa"
ssr["hello aa where is aa"; "aa"; "bb"]
upper "Hello World!"
lower "Hello World!"
/ ########## TimeStored Training Course - Functions - define,return,project #######
/ http://www.timestored.com/kdb-training
/### Defining and Calling Functions
f:{[a] a*a}
/ Create function calculating square of its argument
/ The first square bracket specifies the arguments
/ The remainder of the function body contained within curly braces
/ calling
f[3]
f 5
f@7
/ 2 args
add:{[a;b] a+b}
add[10;3]
/### Anonymous functions
{[a] a*a} 4
{[a;b] a+b}[10;3]
/### Implicit Arguments
/ up to three x/y/z implicit args.
{x*x} 5
{x+y+z}[1;2;3]
{x+z}[1;2;3] / see how this still needed three args
/ ##### variables
f:{a:1; b:2; a+b*x}
f[11]
/ globals
d:10
f:{d+x}
f 1
/ locals take precedence
f:{d:20; d+x}
f 1
/ ##### projection
/ Functions with 1+ argument can have some of the arguments set as constants.
raise:{ x xexp y};
tenToPower:raise[10;];
tenToPower 4
tenToPower:raise 10;
square: raise[;2];
cube: raise[;3];
square 12
cube 3
/ ##### functional form
raise . (2 2 2 2;3 4 5 6)
.[raise; (2 2 2 2;3 4 5 6)]
@[square ; 3 4 5]
@[cube ; 3 4 5]
/ ########## TimeStored Training Course - Table creation #######
/ http://www.timestored.com/kdb-training
/ ##### Creating a table #####
d:`company`employees!(`ford`bmw;300 100)
/ table = flipped rectangular dictionary
t:flip d
type d
type t
/ table shorthand definition
/ column name to vector data sepearated by semi-colons.
/ the square bracket is for defining keyed tables
t~([] company:`ford`bmw; employees:300 100)
/ must enlist data if creating one row table
([] company:`ford; employees:300)
([] company:enlist `ford; employees:enlist 300)
([] syms:`a`b`c; floats:1.1 2.2 3.3; strings:("bob";"jim";"john"))
([] syms:`a`b`c; chars:"aaa"; times:08:00 08:30 09:00)
/ atoms get expanded to fill columns
([] syms:`a`b`c; num:33)
/ ### empty tables
t:([] company:`ford`bmw; employees:300 100)
meta t
t:([] company:(); employees:())
meta t
t:([] company:`symbol$(); employees:`int$())
meta t
/ where this becomes important later is when we look at inserts
/ ##### Common Table Functions #####
t:([] company:`ford`bmw; employees:300 100)
t
type t
count t
cols t
meta t
/ List the tables that exist
tables `.
/ ##### qsql select query format #####
select from t
select from t where company=`ford
/ ##### inserts #####
t:([] company:(); employees:())
meta t
meta t
insert[`t; ([] company:`subaru`hyundai; employees:55 56)]
/ ##### Accessing #####
t:([] company:`ferrari`ford`rover`bmw`AA; employees:3 66 200 88 1)
/ Three methods to access an unkeyed table
/ qSQL
select from t
/ as a dictionary
t[`company]
/ as a list
-3#t