ORDER BY 子句的 CASE WHEN 语句
Posted
技术标签:
【中文标题】ORDER BY 子句的 CASE WHEN 语句【英文标题】:CASE WHEN statement for ORDER BY clause 【发布时间】:2013-10-21 05:31:38 【问题描述】:我正在使用 SQL Server 2008 R2。
我希望对表中的记录进行基于优先级的排序。
所以我在 ORDER BY 子句中使用 CASE WHEN 语句。 ORDER BY 子句如下:
ORDER BY
CASE WHEN TblList.PinRequestCount <> 0 THEN TblList.PinRequestCount desc, TblList.LastName ASC, TblList.FirstName ASC, TblList.MiddleName ASC END,
CASE WHEN TblList.HighCallAlertCount <> 0 THEN TblList.HighCallAlertCount desc, TblList.LastName ASC, TblList.FirstName ASC, TblList.MiddleName ASC END,
Case WHEN TblList.HighAlertCount <> 0 THEN TblList.HighAlertCount DESC, TblList.LastName ASC, TblList.FirstName ASC, TblList.MiddleName ASC END,
CASE WHEN TblList.MediumCallAlertCount <> 0 THEN TblList.MediumCallAlertCount DESC, TblList.LastName ASC, TblList.FirstName ASC, TblList.MiddleName ASC END,
Case WHEN TblList.MediumAlertCount <> 0 THEN TblList.MediumAlertCount DESC, TblList.LastName ASC, TblList.FirstName ASC, Patlist.MiddleName ASC END
但它给了Incorrect syntax near the keyword 'desc'
有什么解决办法吗?
我也可以:
TblList.PinRequestCount <> 0 and TblList.HighCallAlertCount <> 0 and
TblList.HighAlertCount <> 0` and TblList.MediumCallAlertCount <> 0 and
TblList.MediumAlertCount <> 0
同时。
【问题讨论】:
你能显示完整的查询吗? 你可以写 select * From TblList. 我猜你的查询中有case语句对吧? 你能举个例子吗? 【参考方案1】:CASE
是一个表达式 - 它返回一个单个 标量值(每行)。它不能返回解析树的复杂部分,例如 SELECT
语句的 ORDER BY
子句。
看起来你只需要:
ORDER BY
CASE WHEN TblList.PinRequestCount <> 0 THEN TblList.PinRequestCount END desc,
CASE WHEN TblList.HighCallAlertCount <> 0 THEN TblList.HighCallAlertCount END desc,
Case WHEN TblList.HighAlertCount <> 0 THEN TblList.HighAlertCount END DESC,
CASE WHEN TblList.MediumCallAlertCount <> 0 THEN TblList.MediumCallAlertCount END DESC,
Case WHEN TblList.MediumAlertCount <> 0 THEN TblList.MediumAlertCount END DESC,
TblList.LastName ASC, TblList.FirstName ASC, TblList.MiddleName ASC
或者可能:
ORDER BY
CASE
WHEN TblList.PinRequestCount <> 0 THEN TblList.PinRequestCount
WHEN TblList.HighCallAlertCount <> 0 THEN TblList.HighCallAlertCount
WHEN TblList.HighAlertCount <> 0 THEN TblList.HighAlertCount
WHEN TblList.MediumCallAlertCount <> 0 THEN TblList.MediumCallAlertCount
WHEN TblList.MediumAlertCount <> 0 THEN TblList.MediumAlertCount
END desc,
TblList.LastName ASC, TblList.FirstName ASC, TblList.MiddleName ASC
判断上述哪个(或其他)是您要查找的内容有点棘手,因为您 a) 没有解释您想要实现的实际排序顺序,并且 b) 未提供任何 样本数据 和预期结果,我们可以尝试从中推断您尝试实现的实际排序顺序。
这可能是我们正在寻找的答案:
ORDER BY
CASE
WHEN TblList.PinRequestCount <> 0 THEN 5
WHEN TblList.HighCallAlertCount <> 0 THEN 4
WHEN TblList.HighAlertCount <> 0 THEN 3
WHEN TblList.MediumCallAlertCount <> 0 THEN 2
WHEN TblList.MediumAlertCount <> 0 THEN 1
END desc,
CASE
WHEN TblList.PinRequestCount <> 0 THEN TblList.PinRequestCount
WHEN TblList.HighCallAlertCount <> 0 THEN TblList.HighCallAlertCount
WHEN TblList.HighAlertCount <> 0 THEN TblList.HighAlertCount
WHEN TblList.MediumCallAlertCount <> 0 THEN TblList.MediumCallAlertCount
WHEN TblList.MediumAlertCount <> 0 THEN TblList.MediumAlertCount
END desc,
TblList.LastName ASC, TblList.FirstName ASC, TblList.MiddleName ASC
【讨论】:
在这种情况下,序列没有得到维护。我希望 PinRequestCount0 记录在顶部,FirstName、LastName 和 MiddleName 的升序排序。然后 HighCallAlertCount 记录0,FirstName、LastName 和 MiddleName 的升序排序计数。然后 HighAlertCount 0 以 FirstName、LastName 和 MiddleName 等升序排列... @Dev - 是否会有任何行出现多个Count
列不等于 0 的情况?
此外,我为其提供降序排序的每一列不能同时为零。
@Dev - 我添加了一个新的猜测 - 就像我说的那样,如果您的问题实际上向我们提供了有关您想要实现的目标的信息,那就更容易了。
好吧..让我给你一些期望结果的示例数据。【参考方案2】:
来自here..的另一个简单示例
SELECT * FROM dbo.Employee
ORDER BY
CASE WHEN Gender='Male' THEN EmployeeName END Desc,
CASE WHEN Gender='Female' THEN Country END ASC
【讨论】:
【参考方案3】:declare @OrderByCmd nvarchar(2000)
declare @OrderByName nvarchar(100)
declare @OrderByCity nvarchar(100)
set @OrderByName='Name'
set @OrderByCity='city'
set @OrderByCmd= 'select * from customer Order By '+@OrderByName+','+@OrderByCity+''
EXECUTE sp_executesql @OrderByCmd
【讨论】:
欢迎来到 Stack Overflow!我已经编辑了你的答案;通过将代码缩进 4 个空格,将其放入code markdown
。我建议您在答案中添加一些解释,说明此代码如何解决问题。您可以将edit 解释成您的答案。祝你好运!以上是关于ORDER BY 子句的 CASE WHEN 语句的主要内容,如果未能解决你的问题,请参考以下文章
ORDER BY 中带有 CASE WHEN 的 SQL 语句导致类型转换错误
关于sql中动态 ORDER BY+CASE WHEN中排序字段类型的问题
Oracle order by case when 多条件排序