按日期范围对结果进行排序SQL Server

时间:2020-03-03 03:31:18

标签: sql sql-server

我有如下表格数据。一共有3列:@Start "" "%__AppDir__%WindowsPowerShell\v1.0\powershell.exe" -NoLogo -NoProfile -Command "(Add-Type '[DllImport(\"user32.dll\")]public static extern int SendMessage(int hWnd,int hMsg,int wParam,int lParam);' -Name a -Pas)::SendMessage(-1,0x0112,0xF170,2)" IDdate1

date2

我想像这样对它们进行排序

01 | 2020-01-01  | 2020-01-28 
02 | 2020-01-05  | 2020-01-15 
03 | 2020-01-05  | 2020-01-23 

两个日期列中的最新日期都在第一位。这意味着我必须在两个日期之前订购。我尝试了此查询

03 | 2020-01-05  | 2020-01-23
01 | 2020-01-05  | 2020-01-15 
02 | 2020-01-01  | 2020-01-28 

此查询返回如下数据集:

SELECT * 
FROM schema.TESTTABLE 
ORDER BY date1, date2 DESC

02 | 2020-01-01 | 2020-01-28 03 | 2020-01-05 | 2020-01-23 01 | 2020-01-05 | 2020-01-15 的排序不符合我的预期。有什么建议..?

2 个答案:

答案 0 :(得分:2)

默认值为asc,所以您需要像这样为desc寻址date1

 SELECT * FROM schema.TESTTABLE ORDER BY date1 desc,date2 desc

ORDER BY关键字默认以升序对记录进行排序。 要按降序对记录进行排序,请使用 DESC 关键字。

https://www.w3schools.com/sql/sql_orderby.asp

中的更多详细信息

答案 1 :(得分:1)

尝试尝试

SELECT * FROM schema.TESTTABLE ORDER BY date1 desc,date2 desc