通过多列的SQL Server订单不起作用

时间:2019-03-18 06:18:25

标签: sql-server-2012

SELECT  TAID, CID, Name, Mobile, Email, Priority, UserID, Date
FROM            MstTaskAlign
WHERE        (UserID = 116) And (Status='True')
Order by Date desc, CAST(Priority AS INT) asc

请在下面找到结果

enter image description here

2 个答案:

答案 0 :(得分:0)

您可以想到状态列。如果表包含状态列,则查询是正确的,但如果状态不是表的列,则应更改以下条件,如波纹管

WHERE        (UserID = 116)

示例查询

SELECT  TAID, CID, Name, Mobile, Email, Priority, UserID, Date
FROM            MstTaskAlign
WHERE        (UserID = 116)
Order by Date desc, CAST(Priority AS INT) asc

希望这可能对您有用

答案 1 :(得分:0)

仅正确排序。您有两列ORDER BY:日期,用户ID。首先,它按日期的降序排序,然后按UserID的升序排序。

由于您的日期也带有时间戳,因此没有两个日期具有相同的值。因此,它们首先按降序排序,然后将userID按升序排序。如果有两个日期具有完全相同的值,则升序的UserID将起作用。但是,由于日期值不同,因此会分配相应的UserID。

如果还强制转换了第一个ORDER BY参数,则可以根据需要获得结果。

SELECT  TAID, CID, Name, Mobile, Email, Priority, UserID, Date
FROM            MstTaskAlign
WHERE        (UserID = 116) And (Status='True')
Order by CAST(Date AS Date) desc, CAST(Priority AS INT) asc

我在下面放置了示例代码供您参考。

 CREATE TABLE #MSTAlign
 (
 Priority char(2), UserID char(3), dateValue datetime)


 INSERT INTO #MSTAlign VALUES ('11',116,'2019-03-17T22:42:42'), 
 ('10',116,'2019-03-17T22:21:38'),('7',116,'2019-03-17T18:17:39'),
 ('11',116,'2019-03-17T12:32:53'), ('11',116,'2019-03-17T09:50:50');

 SELECT  Priority, UserID, DateValue
FROM            #MstAlign
WHERE        (UserID = 116) 
Order by cast(Datevalue as date) desc, CAST(Priority AS INT) asc    

|Priority   | UserID    | DateValue               |
--------------------------------------------------
|7          |116        | 2019-03-17 18:17:39.000 |
|10         |116        | 2019-03-17 22:21:38.000 |
|11         |116        | 2019-03-17 22:42:42.000 |
|11         |116        | 2019-03-17 12:32:53.000 |
|11         |116        | 2019-03-17 09:50:50.000 |
---------------------------------------------------