开发者

Order against two columns at the same time (intersecting)

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?

开发者_C百科

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?


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


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


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


order by coalesce(CommonName, FirstName)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜