[英]ordering within select - case statement
Is there a way to ordering within a select case statement by a single column only? 有没有一种方法可以仅通过单列在select case语句中进行排序? lets say order in ascending by city only. 只能说按城市升序。
SELECT
StudentLocation =
CASE
WHEN @IsRegistered IS NOT NULL AND @IsInClass IS NOT NULL THEN s.InClassLocation
WHEN @IsRegistered IS NOT NULL AND @IsInClass IS NULL THEN s.OnlineLocation
ELSE s.City + ', ' + s.State
END
You left out of your question the table and from/where clause, but assuming s is an alias to the table you're querying, you can use order the same way you always do it: 您没有考虑表和from / where子句的问题,但是假设s是您要查询的表的别名,则可以使用与通常相同的方式使用order:
SELECT
StudentLocation =
CASE
WHEN @IsRegistered IS NOT NULL AND @IsInClass IS NOT NULL THEN s.InClassLocation
WHEN @IsRegistered IS NOT NULL AND @IsInClass IS NULL THEN s.OnlineLocation
ELSE s.City + ', ' + s.State
END
FROM someTable as s
ORDER BY s.City ASC;
Corrected City 校正后的城市
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.