Pun*_*dey 4 sql postgresql select sql-order-by
我有一个PG数据库表价格。结构如下:
id name total_sales created_at
1 A 0.0 2016-01-01
2 B 1.25 2016-01-01
3 C 8.17 2016-01-01
4 D 15.09 2016-01-01
5 E 0.0 2016-01-01
6 F NULL 2016-01-01
7 G 2.25 2016-01-01
8 H 19.34 2016-01-01
9 I 47.91 2016-01-01
10 J 0.0 2016-01-01
11 K NULL 2016-01-01
12 L 0.01 2016-01-01
13 M 5.11 2016-01-01
14 N 27.53 2016-01-01
15 O 3.53 2016-01-01
Run Code Online (Sandbox Code Playgroud)
我需要的很简单。我想订购这些记录:
按升序排列值 > 0.0 的项目首先出现,然后是 具有 0.0 的项目,然后是 NULLS LAST
简而言之,我需要按以下顺序进行操作:
1st: 12 => 0.01
2nd: 2 => 1.25,
3rd: 7 => 2.25,
4th: 15 => 3.53,
5th: 13 => 5.11,
6th: 3 => 8.17,
7th: 4 => 15.09,
8th: 8 => 19.34,
9th: 14 => 27.53,
10th: 9 => 47.91,
11th, 12th, 13th all 0.0
14th, 15th all NULLS
Run Code Online (Sandbox Code Playgroud)
到目前为止,我尝试了以下 SQL,但没有成功!
SELECT * FROM prices
ORDER BY CASE WHEN total_sales = 0.0 THEN 0 ELSE total_sales END ASC NULLS LAST
Run Code Online (Sandbox Code Playgroud)
order by total_sales = 0 nulls last, total_sales
Run Code Online (Sandbox Code Playgroud)
false之前的订单true