Order against two columns at the same time (intersecting)

被刻印的时光 ゝ 提交于 2019-12-06 15:01:50

问题


I have a table with the fields CommonName and FirstName. Only either field has data, never both. Is there a way to order rows in an intersecting manner on SQL Server?

Example:

CommonName FirstName
Bern
           Wade
Ashley
Boris
           Ayana

I want records ordered like this:

CommonName FirstName
Ashley
           Ayana
Bern
Boris
           Wade

Is this possible, and if so, how?


回答1:


Use a CASE statement to select the value for that row and ORDER BY that.




回答2:


ORDER BY
  CASE
    WHEN CommonName is null
    THEN FirstName
    ELSE CommonName
  END



回答3:


ORDER BY CommonName + FirstName, with appropriate ISNULL(<column>, '') if they are nullable.




回答4:


order by coalesce(CommonName, FirstName)


来源:https://stackoverflow.com/questions/2907033/order-against-two-columns-at-the-same-time-intersecting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!