我想根据两个列值从MSSQL Server检索记录: ColumnX和Id。
首先,我想检索ColumnX的空记录(在顶部),然后按Id排序(我只需要在列表的顶部对ColumnX的空记录进行排序)。有可能做到这一点吗?当我尝试这个查询时,我检索ColumnX的null值,但随后根据ColumnX值检索。但是,我希望在ColumnX的空值之后按Id列DESC排序。有什么想法吗?
SELECT Id, ColumnX
FROM Table
ORDER BY ColumnX , Id DESC发布于 2019-10-18 20:32:59
您可以在order by中包含多个表达式
order by (case when x is null then 1 else 2 end), -- put null values first
id desc发布于 2019-10-18 20:35:33
试试这个:
SELECT ID, ColumnX
FROM Table
order by (case when ColumnX is null then 1 else 2 end),
ID DESChttps://stackoverflow.com/questions/58451020
复制相似问题