我需要帮助在Oracle上对查询结果进行排序,我需要NULLS位于顶部。
MIN(SYSDATE - expiry) KEEP (DENSE_RANK FIRST ORDER BY SYSDATE - expiry DESC NULLS FIRST) AS active
结果如下:
ID NAME ACTIVE
=================================
4 John Smith 139
4 John Smith NULL
4 John Smith 2700
7 Martha Snow NULL
我想要它,以便它将结果排序如下:
ID NAME ACTIVE
=================================
4 John Smith NULL
4 John Smith 139
4 John Smith 2700
7 Martha Snow NULL
感谢那些愿意帮助的人!
答案 0 :(得分:0)
您的陈述只是将值分配给第三列(可能)。它没有订购整个数据集。除非外部查询具有order by
,否则结果集无序。
所以:
select . . .
from . . .
order by id, active asc nulls first;
如果这些只是三列,那么您可能甚至不需要keep
。只是:
select id, name, (sysdate - expiry) as active
from . . .
order by id, active asc nulls first;
答案 1 :(得分:0)
Oracle 11g R2架构设置:
CREATE TABLE Data ( ID, NAME, ACTIVE ) AS
SELECT 4, 'John Smith', 139 FROM DUAL
UNION ALL SELECT 4, 'John Smith', NULL FROM DUAL
UNION ALL SELECT 4, 'John Smith', 2700 FROM DUAL
UNION ALL SELECT 7, 'Martha Snow', NULL FROM DUAL
查询1 :
SELECT *
FROM Data
ORDER BY ID, ACTIVE ASC NULLS FIRST
<强> Results 强>:
| ID | NAME | ACTIVE |
|----|-------------|--------|
| 4 | John Smith | (null) |
| 4 | John Smith | 139 |
| 4 | John Smith | 2700 |
| 7 | Martha Snow | (null) |