SQL ORDER BY Doubts

时间:2016-08-24 08:43:36

标签: sql sql-server

|---------------------     |------------------           |
|      AttendanceCode      |     AttendanceDescription   |
|---------------------     |------------------           |
|          MC              |         Medical Leave       |
|---------------------     |------------------           |
|         NAPFA            |            NAPFA            |
|---------------------     |------------------           |
|        Present           |            Present          |
|---------------------     |------------------           |

我正在使用SQL Server 2014.我有一个包含两列的表,AttendanceCode和AttendanceDescription,我使用下拉列表来显示AttendanceDescription。我希望下拉列表顺序显示" Present"第一。我尝试使用此查询:

SELECT AttendanceCode, AttendanceDescription
FROM journalattendancestatus
ORDER BY (CASE AttendanceDescription WHEN 'P' THEN 1 ELSE NULL END) AttendanceDescription

这个

SELECT AttendanceCode, AttendanceDescription
FROM journalattendancestatus
ORDER BY CASE WHEN AttendanceDescription = 'Present' THEN 1 ELSE 2 END,
         AttendanceDescription";

但它们都不起作用。我也尝试将其更改为AttendanceCode,其中P' P'代表'呈现'

7 个答案:

答案 0 :(得分:1)

仅在Exception in thread "main" [Error: failed to subEval expression] [Near : {... name == 'Raja' && age == 30 && joiningDate <="24-Aug-2016" ....}] ^ [Line: 1, Column: 46] at org.mvel2.compiler.AbstractParser.reduce(AbstractParser.java:2627) at org.mvel2.compiler.AbstractParser.arithmeticFunctionReduction(AbstractParser.java:2526) at org.mvel2.MVELInterpretedRuntime.parseAndExecuteInterpreted(MVELInterpretedRuntime.java:147) at org.mvel2.MVELInterpretedRuntime.parse(MVELInterpretedRuntime.java:47) at org.mvel2.MVEL.eval(MVEL.java:270) at org.mvel2.MVEL.evalToBoolean(MVEL.java:623) at com.scb.mvel.App.isCriteriaMatch(App.java:27) at com.scb.mvel.App.main(App.java:21) Caused by: java.lang.RuntimeException: uncomparable values <<Wed Aug 24 12:52:37 IST 2016>> and <<24-Aug-2016>> at org.mvel2.math.MathProcessor.doOperationNonNumeric(MathProcessor.java:321) at org.mvel2.math.MathProcessor._doOperations(MathProcessor.java:234) at org.mvel2.math.MathProcessor.doOperations(MathProcessor.java:79) at org.mvel2.math.MathProcessor.doOperations(MathProcessor.java:48) at org.mvel2.util.ExecutionStack.op(ExecutionStack.java:178) at org.mvel2.compiler.AbstractParser.reduce(AbstractParser.java:2567) ... 7 more Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Date at java.util.Date.compareTo(Date.java:129) at org.mvel2.math.MathProcessor.doOperationNonNumeric(MathProcessor.java:318) ... 12 more 子句

中使用AttendanceDescription列一次
ORDER BY

答案 1 :(得分:0)

排序依据只是按列对数据进行排序。

示例:

SELECT * FROM user WHERE AGE=18 ORDER BY NAME DESC

这将采用您的SQL并按反向字母顺序对其进行排序。

你到底想要做什么?我或许可以帮助您解决您想要完成的任务。

答案 2 :(得分:0)

对于此查询,空值将首先出现..

ORDER BY (CASE AttendanceDescription WHEN 'P' THEN 1 ELSE NULL END) AttendanceDescription

将其更改为

WHEN 'P' THEN 1 ELSE 2 END

第二个查询几乎接近..

ORDER BY CASE WHEN AttendanceDescription = 'Present' THEN 1 ELSE 2 END,
         AttendanceDescription";

但是当1有关系时它再次回到AttendanceDescription来打破关系,所以在这种情况下它可以是任何东西..所以删除那个

答案 3 :(得分:0)

您的第二个查询正在运行。它有语法错误。我在END语句附近删除了',',在查询结束时删除了双引号。

SELECT AttendanceCode, AttendanceDescription
FROM journalattendancestatus
ORDER BY CASE WHEN AttendanceDescription = 'Present' THEN 1 ELSE 2 END
         AttendanceDescription;

在第一个查询中,您正在搜索列中不存在的字母“P”。你可以像这里一样使用。

ORDER BY (CASE when AttendanceDescription like 'Pre%' THEN 1 ELSE NULL END) AttendanceDescription

答案 4 :(得分:0)

不是最佳选择,只是一个想法:

SELECT AttendanceCode, AttendanceDescription, 1 AS OrderIndex 
FROM journalattendancestatus
WHERE AttendanceDescription = 'P' 
UNION
SELECT AttendanceCode, AttendanceDescription, 2 AS OrderIndex 
FROM journalattendancestatus
WHERE AttendanceDescription != 'P' 
ORDER BY OrderIndex, AttendanceCode, AttendanceDescription

答案 5 :(得分:0)

这个适用于我的HANA

.header{
    width:100%;
    height:88px;
    position: fixed;
    left:0;
    top:0;
    z-index: 9;
    background: #009ddc; /* for test */
}

答案 6 :(得分:0)

declare @flttaxrate float
declare @fltArea float
declare @fltbasetax float

set @fltArea=139.3
set @flttaxrate=50

set @fltbasetax=@fltArea*@flttaxrate
select @fltbasetax
select ceiling(@fltbasetax)

请检查输出选择@fltbasetax,选择ceiling(@fltbasetax)