Skip to content

Commit bd48841

Browse files
committed
Add kdb tutorial notebook. Saved snapshot is at: https://www.timestored.com/sqlnotebook/examples/kdb-tutorial-notebook.html
1 parent 4bf22a6 commit bd48841

File tree

6 files changed

+1166
-0
lines changed

6 files changed

+1166
-0
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
---
2+
server: localhost:5000
3+
---
4+
5+
# kdb+ Tutorials using SQL Notebooks
6+
7+
These tutorials are available free at: https://www.timestored.com/kdb-guides/
8+
9+
These particular pages were generated using [SQL Notebooks](https://www.timestored.com/sqlnotebook/) within [QStudio](https://www.timestored.com/qstudio/) to demonstrate how easy it is to create tutorials.
10+
11+
[QStudio](https://www.timestored.com/qstudio/) is a free SQL Client.
12+
13+
# Getting Started
14+
15+
### type math expressions directly into kdb
16+
17+
```sql showcodeonly
18+
10+3
19+
900-88
20+
13*2
21+
90%20 // division is the percentage symbol
22+
```
23+
24+
Expressions are evaluated right to left
25+
26+
```sql showcodeonly
27+
100%10+10
28+
```
29+
30+
```sql
31+
100%10+10
32+
```
33+
34+
## Defining a variable
35+
36+
```sql showcodeonly
37+
a:13
38+
a
39+
b:10*9
40+
b
41+
a+b
42+
c:a+b
43+
c
44+
```
45+
46+
## Comments
47+
48+
comments must be space then slash then comment
49+
within a script there's another format / on new line then closed later
50+
51+
```sql showcodeonly
52+
b: 1 // comments
53+
b:1/ error as similar to adjectives we will see later
54+
b: 1 // whitespace doesn't matter in kdb
55+
```
56+
57+
## Slash Commands
58+
59+
What objects exist on the server
60+
61+
```sql showcodeonly
62+
\v // variables
63+
\a // tables
64+
system "v"
65+
system "a"
66+
```
67+
68+
Can alter settings e.g. Precision and Console size
69+
70+
```sql showcodeonly
71+
\P
72+
\c
73+
\c 22 88
74+
75+
\l script.q
76+
```
77+
78+
If the slash command isn't recognised as a kdb call
79+
it's passed to the underlying OS, e.g. dos commands
80+
81+
```sql showcodeonly
82+
\cd
83+
\echo test
84+
system "cd"
85+
```
86+
87+
```sql showcodeonly
88+
\\
89+
exit 0;
90+
```
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
---
2+
server: localhost:5000
3+
---
4+
5+
# kdb+ Joins
6+
7+
Sql joins allow pulling corresponding data from one table, onto another to give a combined result table with columns from both.
8+
Most kdb joins rely on column names corresponding (rather than standard sql's more verbose explicit naming).
9+
10+
# Example Tables
11+
12+
```sql showcodeonly
13+
stock:([sym:`s#`AAPL`C`FB`MS]
14+
sector:`Tech`Financial`Tech`Financial;
15+
employees:72800 262000 4331 57726);
16+
17+
trades:([] dt:`s#2015.01.01+0 1 2 3 3 4 5 6 6;
18+
sym:`C`C`MS`C`DBK`AAPL`AAPL`MS`MS;
19+
price:10 10.5 260 11 35.6 1010 1020 255 254;
20+
size:10 100 15 200 55 20 300 200 400);
21+
22+
fbTrades:([] dt:`s#2015.01.01+1 2 4; sym:`FB; size:1000; book:`A`B`A);
23+
```
24+
25+
## stock
26+
27+
```
28+
stock:([sym:`s#`AAPL`C`FB`MS]
29+
sector:`Tech`Financial`Tech`Financial;
30+
employees:72800 262000 4331 57726);
31+
stock
32+
```
33+
34+
## trades
35+
```
36+
trades:([] dt:`s#2015.01.01+0 1 2 3 3 4 5 6 6;
37+
sym:`C`C`MS`C`DBK`AAPL`AAPL`MS`MS;
38+
price:10 10.5 260 11 35.6 1010 1020 255 254;
39+
size:10 100 15 200 55 20 300 200 400);
40+
trades
41+
```
42+
43+
## fbTrades
44+
45+
```
46+
fbTrades:([] dt:`s#2015.01.01+1 2 4; sym:`FB; size:1000; book:`A`B`A);
47+
fbTrades
48+
```
49+
50+
51+
52+
53+
# lj - Left Join
54+
55+
The format of lj is: ``t lj kt`` where t is your source table and kt is your lookup table that MUST be keyed.
56+
**Lj - left join - means for each row in table t, try to look up corresponding values in keyed-table kt,
57+
where there is no match use nulls**. The columns used for mathing are the key columns of kt. i.e.
58+
The key columns of kt, must appear in t and their column names MUST match exactly.
59+
60+
Note:
61+
- Where a lookup table contains non-key columns with the same name as existing columns, the matched columns overwrite the original value.
62+
- Where multiple matches are possible (duplicate keys in keyed table), the first match is always taken.
63+
64+
65+
```
66+
trades lj stock
67+
```
68+
69+
70+
# pj - Plus Join
71+
72+
``t pj kt`` - Same principle as lj, but existing values are added to where column names match.
73+
74+
**Pj - plus join - means for each row in table t, try to look up corresponding values in keyed-table kt, where there are matching numeric columns add their values.**
75+
76+
```
77+
stock pj ([sym:`FB`C] employees:100000 -260000)
78+
```
79+
80+
# ij - Inner Join
81+
82+
``t ij kt`` - **Where matches occur between t and kt on primary key columns, update or add that column.**
83+
Non-matches are not returned in the result. The columns used for matching are the key columns of kt.
84+
i.e. The key columns of kt, must appear in t and their column names MUST match exactly.
85+
86+
```
87+
trades ij stock
88+
```
89+
90+
As you can see the result has one less row than there was in trade, row 4 `DBK did not have a match by key lookup in the stock table so was dropped from the result.
91+
92+
### Different inner join than standard sql
93+
94+
Notice above that we said the join returns ONLY the first match from the lookup table. This is different than standard SQL which returns the cartesian join of all matches. The code below showns how we could replicate an SQL standard inner join:
95+
96+
#### t1
97+
```sql type="table"
98+
q)t1:([] sym:`a`b`c; v:1 2 3);
99+
t1
100+
```
101+
#### t2
102+
```sql type="table"
103+
t2:([sym:`a`a`b] s:100 200 300);
104+
t2
105+
```
106+
107+
```sql type="table"
108+
t1 ij t2
109+
```
110+
111+
```sql type="table"
112+
ungroup t1 ij `sym xgroup t2
113+
```
114+
115+
# uj - Union Join
116+
117+
**uj - Union-Join all rows/columns from two tables, upserting when keyed, appending when unkeyed or no existing match found.**
118+
119+
```
120+
trades uj fbTrades
121+
```
122+
123+
Notice: Columns with common names are now one column in the result. However columns that occurred in only one table are included in the result, however where no values existed they are filled with nulls.
124+
125+
126+
127+
128+
129+
130+
131+
132+
133+
134+
135+
136+
137+

0 commit comments

Comments
 (0)