Impala ORDER BY子句用于根據(jù)一個(gè)或多個(gè)列以升序或降序?qū)?shù)據(jù)進(jìn)行排序。 默認(rèn)情況下,一些數(shù)據(jù)庫(kù)按升序?qū)Σ樵兘Y(jié)果進(jìn)行排序。
以下是ORDER BY子句的語(yǔ)法。
select * from table_name ORDER BY col_name [ASC|DESC] [NULLS FIRST|NULLS LAST]
可以使用關(guān)鍵字ASC或DESC分別按升序或降序排列表中的數(shù)據(jù)。
以同樣的方式,如果我們使用NULLS FIRST,表中的所有空值都排列在頂行; 如果我們使用NULLS LAST,包含空值的行將最后排列。
假設(shè)我們?cè)跀?shù)據(jù)庫(kù)my_db中有一個(gè)名為customers的表,其內(nèi)容如下 -
[quickstart.cloudera:21000] > select * from customers; Query: select * from customers +----+----------+-----+-----------+--------+ | id | name | age | address | salary | +----+----------+-----+-----------+--------+ | 3 | kaushik | 23 | Kota | 30000 | | 1 | Ramesh | 32 | Ahmedabad | 20000 | | 2 | Khilan | 25 | Delhi | 15000 | | 6 | Komal | 22 | MP | 32000 | | 4 | Chaitali | 25 | Mumbai | 35000 | | 5 | Hardik | 27 | Bhopal | 40000 | +----+----------+-----+-----------+--------+ Fetched 6 row(s) in 0.51s
以下是使用order by子句按照其ID的升序排列customers表中的數(shù)據(jù)的示例。
[quickstart.cloudera:21000] > Select * from customers ORDER BY id asc;
在執(zhí)行時(shí),上述查詢產(chǎn)生以下輸出。
Query: select * from customers ORDER BY id asc +----+----------+-----+-----------+--------+ | id | name | age | address | salary | +----+----------+-----+-----------+--------+ | 1 | Ramesh | 32 | Ahmedabad | 20000 | | 2 | Khilan | 25 | Delhi | 15000 | | 3 | kaushik | 23 | Kota | 30000 | | 4 | Chaitali | 25 | Mumbai | 35000 | | 5 | Hardik | 27 | Bhopal | 40000 | | 6 | Komal | 22 | MP | 32000 | +----+----------+-----+-----------+--------+ Fetched 6 row(s) in 0.56s
同樣,您可以使用order by子句按降序排列customers表的數(shù)據(jù),如下所示。
[quickstart.cloudera:21000] > Select * from customers ORDER BY id desc;
在執(zhí)行時(shí),上述查詢產(chǎn)生以下輸出。
Query: select * from customers ORDER BY id desc +----+----------+-----+-----------+--------+ | id | name | age | address | salary | +----+----------+-----+-----------+--------+ | 6 | Komal | 22 | MP | 32000 | | 5 | Hardik | 27 | Bhopal | 40000 | | 4 | Chaitali | 25 | Mumbai | 35000 | | 3 | kaushik | 23 | Kota | 30000 | | 2 | Khilan | 25 | Delhi | 15000 | | 1 | Ramesh | 32 | Ahmedabad | 20000 | +----+----------+-----+-----------+--------+ Fetched 6 row(s) in 0.54s
更多建議: