在 SQL 中,我们可以做
SELECT from tbl ORDER BY col1, col2 DESC
Run Code Online (Sandbox Code Playgroud)
在 KDB,人们可以做
`col1 xasc select from tbl
Run Code Online (Sandbox Code Playgroud)
或者
`col2 xdesc select from tbl
Run Code Online (Sandbox Code Playgroud)
但是在 KDB/Q 中如何按 col1 升序排序然后按 col2 降序排序呢?
2种。
创建示例数据:
q)show tbl:([]a:10?10;b:10?10;c:10?10)
a b c
-----
8 4 8
1 9 1
7 2 9
2 7 5
4 0 4
5 1 6
4 9 6
2 2 1
7 1 8
8 8 5
Run Code Online (Sandbox Code Playgroud)
进行排序:
q)`a xasc `b xdesc tbl
a b c
-----
1 9 1
2 7 5
2 2 1
4 9 6
4 0 4
5 1 6
7 2 9
7 1 8
8 8 5
8 4 8
Run Code Online (Sandbox Code Playgroud)