2017-06-27 104 views
0

是否有一種方法僅在單個列的select case語句中進行排序?讓我們只能說在城市上升的順序。在select - case語句中排序

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 
+0

你有也是FROM,WHERE? – EvgenyKolyakov

+0

可能我不明白目的。但是,爲什麼你不去做這樣的SELECT * FROM(SELECT StudentLocation = CASE WHEN IsRegistered IS NOT NULL,當IsRegistered IS NOT NULL和IsInClass爲null,則s.OnlineLocation ELSE小號IsInClass IS NOT NULL THEN s.InClassLocation 。 City +','+ s.State END)order by – Dasma

回答

0

你忽略了你的問題的表和/ where子句,但假設s是一個別名你查詢的表,你可以使用順序相同的方式,你總是做到這一點:

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; 

更正市