Order By Asc and Desc both in single query

user1037747

I have a query which retrieves latest data from table with pagination which is working fine.

But once the data is older from current time it should also appear but after the latest one.

SELECT A.UserId,A.FirstName,A.LastName,A.PostDate
(SELECT ROW_NUMBER() OVER(ORDER BY CAST(M.PostDate AS DATETIMEOFFSET) DESC) AS 'RowNumber'
    M.UserId,
   M.FirstName,
            M.LastName,
    M.PostDate
    FROM Messages AS M
   Where M.PostDate >= GetDate()
) A
       WHERE A.RowNumber BETWEEN @RowStart AND @RowEnd 
 ORDER BY CAST(A.PostDate AS DATETIMEOFFSET)  DESC
jlee-tessik

I'm not sure if I'm exactly getting what you're looking for, but this should get you something close. I created a CTE with two computed columns:

  • BeforeAfter, which determines if the date happens before or after the passed in date
  • AbsDiff, which gives the absolute value of the date diff.

Then, I just sort using those two fields:

DECLARE @current datetime = GETDATE()
;WITH cteMessages AS 
(
    SELECT UserId, FirstName, LastName, PostDate, 
    CASE WHEN PostDate < @current THEN 1 ELSE 0 END AS BeforeAfter,
    ABS(DATEDIFF(SECOND, @current, PostDate)) AS AbsDiff
    FROM Messages
)
SELECT * FROM cteMessages
ORDER BY BeforeAfter, AbsDiff

From those results, you can see how they are sorted first by messages newer than the passed in date, then in reverse order from older messages. You can substitute that order by into your Row_Number function.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Custom Comparator implementation for both ASC and DESC order

From Dev

Order by both ASC and DESC at a time in single CASE WHEN statement SQL Server

From Dev

How to sort a same column both in asc order and desc order

From Dev

How to sort a same column both in asc order and desc order

From Dev

MySQL query faster in DESC order than ASC order

From Dev

PDO query appears to be returning DESC order. Ignoring ASC hardcode

From Dev

conditional asc or desc in in a order byexpression

From Dev

conditional asc or desc in in a order byexpression

From Dev

Order by in mysql without asc and desc

From Dev

Order table column in asc/desc order

From Dev

Algolia sorting only ASC or DESC not both

From Dev

SQL: Order by column, then by substring mix asc and desc

From Dev

Datastax Driver Mapping ORDER BY ASC/DESC

From Java

Room DAO Order By ASC or DESC variable

From Dev

Case statement for Order By clause with Desc/Asc sort

From Dev

PHP Mysql ASC AND DESC Order Proper use?

From Dev

We get data with ORDER BY ASC but NOT BY DESC

From Dev

how to order by <= 7 desc then by > 7 asc?

From Dev

PHP Mysql ASC AND DESC Order Proper use?

From Dev

Datastax Driver Mapping ORDER BY ASC/DESC

From Dev

Order by asc Sql query

From Dev

Conditionally reverse ASC/DESC sort order in Access SQL with IF ELSE in ORDER BY?

From Dev

My query is only showing the first line of data when I order by asc, but it works normally when I filter by desc

From Dev

Changing default ORDER BY from ASC to DESC globally for all queries

From Java

Sublime text 2 - how to order a list of words alphabetically (DESC / ASC)

From Dev

CASE Statement for Order By Clause with Multiple Columns and Desc/Asc Sort

From Dev

SQL order within cases, DESC and ASC odd behavior

From Dev

Mongoid Rails 4 sort by asc or desc order created_at

From Dev

Order by (asc|desc) in linq to SQL Server handles DateTime differently

Related Related

  1. 1

    Custom Comparator implementation for both ASC and DESC order

  2. 2

    Order by both ASC and DESC at a time in single CASE WHEN statement SQL Server

  3. 3

    How to sort a same column both in asc order and desc order

  4. 4

    How to sort a same column both in asc order and desc order

  5. 5

    MySQL query faster in DESC order than ASC order

  6. 6

    PDO query appears to be returning DESC order. Ignoring ASC hardcode

  7. 7

    conditional asc or desc in in a order byexpression

  8. 8

    conditional asc or desc in in a order byexpression

  9. 9

    Order by in mysql without asc and desc

  10. 10

    Order table column in asc/desc order

  11. 11

    Algolia sorting only ASC or DESC not both

  12. 12

    SQL: Order by column, then by substring mix asc and desc

  13. 13

    Datastax Driver Mapping ORDER BY ASC/DESC

  14. 14

    Room DAO Order By ASC or DESC variable

  15. 15

    Case statement for Order By clause with Desc/Asc sort

  16. 16

    PHP Mysql ASC AND DESC Order Proper use?

  17. 17

    We get data with ORDER BY ASC but NOT BY DESC

  18. 18

    how to order by <= 7 desc then by > 7 asc?

  19. 19

    PHP Mysql ASC AND DESC Order Proper use?

  20. 20

    Datastax Driver Mapping ORDER BY ASC/DESC

  21. 21

    Order by asc Sql query

  22. 22

    Conditionally reverse ASC/DESC sort order in Access SQL with IF ELSE in ORDER BY?

  23. 23

    My query is only showing the first line of data when I order by asc, but it works normally when I filter by desc

  24. 24

    Changing default ORDER BY from ASC to DESC globally for all queries

  25. 25

    Sublime text 2 - how to order a list of words alphabetically (DESC / ASC)

  26. 26

    CASE Statement for Order By Clause with Multiple Columns and Desc/Asc Sort

  27. 27

    SQL order within cases, DESC and ASC odd behavior

  28. 28

    Mongoid Rails 4 sort by asc or desc order created_at

  29. 29

    Order by (asc|desc) in linq to SQL Server handles DateTime differently

HotTag

Archive