Q 程式語言



Kdb+ 自帶其內建的程式語言,稱為q。它包含標準 SQL 的超集,擴充套件用於時間序列分析,並提供比標準版本許多優勢。任何熟悉 SQL 的人都可以在幾天內學習q,並能夠快速編寫自己的臨時查詢。

啟動“q”環境

要開始使用 kdb+,您需要啟動q會話。有三種方法可以啟動q會話:

  • 在執行終端上鍵入“c:/q/w32/q.exe”。

  • 啟動 MS-DOS 命令終端並鍵入q

  • q.exe檔案複製到“C:\Windows\System32”,然後在執行終端上只需鍵入“q”。

這裡我們假設您正在 Windows 平臺上工作。

資料型別

下表提供了受支援的資料型別列表:

名稱 示例 字元 型別 大小
布林值 1b b 1 1
位元組 0xff x 4 1
短整型 23h h 5 2
整型 23i i 6 4
長整型 23j j 7 8
實數 2.3e e 8 4
浮點型 2.3f f 9 8
字元 “a” c 10 1
可變長字元 `ab s 11 *
月份 2003.03m m 13 4
日期 2015.03.17T18:01:40.134 z 15 8
分鐘 08:31 u 17 4
08:31:53 v 18 4
時間 18:03:18.521 t 19 4
列舉 `u$`b, where u:`a`b * 20 4

原子和列表的構成

原子是單個實體,例如單個數字、字元或符號。在上表(不同資料型別)中,所有受支援的資料型別都是原子。列表是原子或其他型別的序列,包括列表。

將任何型別的原子傳遞給單元(即單引數函式)型別函式將返回負值,即–n,而將這些原子的簡單列表傳遞給型別函式將返回正值n

示例 1 – 原子和列表的構成

/ Note that the comments begin with a slash “ / ” and cause the parser
/ to ignore everything up to the end of the line.

x: `mohan              / `mohan is a symbol, assigned to a variable x
type x                 / let’s check the type of x
-11h                   / -ve sign, because it’s single element.

y: (`abc;`bca;`cab)    / list of three symbols, y is the variable name.

type y
11h                    / +ve sign, as it contain list of atoms (symbol).

y1: (`abc`bca`cab)     / another way of writing y, please note NO semicolon

y2: (`$”symbols may have interior blanks”)   / string to symbol conversion
y[0]                   / return `abc
y 0                    / same as y[0], also returns `abc
y 0 2                  / returns `abc`cab, same as does y[0 2]

z: (`abc; 10 20 30; (`a`b); 9.9 8.8 7.7)      / List of different types,
z 2 0                  / returns (`a`b; `abc),
z[2;0]                 / return `a. first element of z[2]

x: “Hello World!”      / list of character, a string
x 4 0                  / returns “oH” i.e. 4th and 0th(first)
element
廣告