订购两个标准,SQL

时间:2012-05-09 14:35:50

标签: sql tsql

enter image description here

以上是我的查询输出。我想将放电类型按字母顺序排列,同时将地铁,阿巴拉契亚和乡村TRUES分组在一起,即地铁,乡村和阿巴拉契亚地区的所有1都是正确的顺序,但我如何按字母顺序排列第一列。以下是我的查询

SELECT     tblDischarge.dischargeType, COUNT(tblDischarge.dischargeType) AS counts, tblVisits.diabetes, tblVisits.hemiplegia, tblKentuckyCounties.Metro, 
                      tblKentuckyCounties.Appalachia, tblKentuckyCounties.Rural
FROM         tblKentuckyCounties INNER JOIN
                      tblVisits ON tblKentuckyCounties.countyID = tblVisits.countyID INNER JOIN
                      tblDischarge ON tblVisits.DischargeStatus = tblDischarge.dis_statID
WHERE     (tblVisits.diabetes = 1) AND (tblVisits.hemiplegia = 1)
GROUP BY tblDischarge.dischargeType, tblVisits.diabetes, tblVisits.hemiplegia, tblKentuckyCounties.Metro, tblKentuckyCounties.Appalachia, tblKentuckyCounties.Rural
HAVING      (tblDischarge.dischargeType LIKE '%routine%') OR
                      (tblDischarge.dischargeType LIKE '%short-term%') OR
                      (tblDischarge.dischargeType LIKE '%icf%') OR
                      (tblDischarge.dischargeType = 'home health') OR
                      (tblDischarge.dischargeType LIKE '%swing%') OR
                      (tblDischarge.dischargeType LIKE 'rehab%') OR
                      (tblDischarge.dischargeType LIKE '%long-term%')
ORDER BY tblKentuckyCounties.Appalachia, tblKentuckyCounties.Rural, tblKentuckyCounties.Metro

3 个答案:

答案 0 :(得分:2)

将最后一行更改为:

ORDER BY tblKentuckyCounties.Appalachia, tblKentuckyCounties.Rural, tblKentuckyCounties.Metro, tblDischarge.dischargeType

答案 1 :(得分:1)

ORDER BY tblKentuckyCounties.Appalachia, tblKentuckyCounties.Rural, tblKentuckyCounties.Metro, tblDischarge.dischargeType

应该这样做。

答案 2 :(得分:0)

只需将tblDischarge.dischargeType添加到ORDER BY作为第一个值。

ORDER BY tblDischarge.dischargeType, tblKentuckyCounties.Appalachia, tblKentuckyCounties.Rural, tblKentuckyCounties.Metro

修改

这是无法做到的,因为您尝试按两个完全不同的标准进行排序。您只能进行订购 - 每个步骤都会在上一步中对结果进行排序,但您只能有一个订单。