Sql Server Reverse Order After Using Desc

SQL Server reverse order after using desc

That should work as long as you alias the subquery.

SELECT q.* 
FROM (SELECT TOP 3 *
FROM table
ORDER BY id DESC) q
ORDER BY q.id ASC

Order by DESC reverse result

You could wrap your query with another query and order by with asc. Since you want to order by live.id, you must include it in the inner query so the outer one can sort by it:

SELECT   message, sender, sdate, online 
FROM (SELECT live.message, live.sender, live.sdate, users.online, live.id
FROM live, users
WHERE users.username = live.sender
ORDER BY live.id DESC
LIMIT 15) t
ORDER BY id ASC

MS-SQL sort output in descending order

For your actual requirement, you can use your current query as a derived table and order that result in the way you want:

SELECT *
FROM ( SELECT DISTINCT TOP 7 DATENAME(mm, mydatetime) + ' '
+ CAST(DAY(mydatetime) AS VARCHAR(2)) AS thedate,
MONTH(mydatetime) AS theMonth,
DAY(mydatetime) AS theDay,
COUNT(page) AS totalcount,
COUNT(DISTINCT page) AS visitors
FROM sometable
WHERE page LIKE '%AEC%'
GROUP BY DATENAME(mm, mydatetime) + ' '
+ CAST(DAY(mydatetime) AS VARCHAR(2)),
MONTH(mydatetime),
DAY(mydatetime)
ORDER BY MONTH(mydatetime) DESC,
DAY(mydatetime) DESC) A
ORDER BY theMonth, theDay

Reverse order of SQL query output

You can reorder that resultset by doing an outer select and ordering it the way you want (ASC):

SELECT * FROM (
select * from LastResult ORDER BY Date DESC LIMIT 10
) as
ORDER BY Date ASC;

If this fails (as it would in PostgreSQL 9.3.4), use an alias for the inner select to make it as below:

SELECT * FROM (
select * from LastResult ORDER BY Date DESC LIMIT 10
) as foo
ORDER BY foo.Date ASC;

Sorting in descending order in SQL

The ORDER BY keyword takes several arguments: https://www.w3schools.com/sql/sql_orderby.asp

SELECT * FROM table_name ORDER BY id1 DESC, id2 DESC

Select Top N Records Ordered by X, But Have Results in Reverse Order

SELECT * FROM 
(SELECT TOP 10 * FROM FooTable ORDER BY X DESC) as myAlias
ORDER BY X ASC

i.e. you might need an alias on your subquery, but other than that it should work.

SQL Query producing results in reverse order

You can multiply all your CASE rankings by -1 to get the order you want:

SELECT * FROM [TIMESTAMP_ORGANISER].[dbo].[TEST_DB]
ORDER BY timestamp, CASE WHEN remaining LIKE '%agentStateEvent%' THEN -1
WHEN remaining LIKE '%TerminalConnectionCreated%' THEN -2
ELSE -3 END

I also changed your LIKE matching conditions because from your results it appears that even things were out of order in the original query which you don't want.

Reverse order of a XML Column in SQL Server

There is no need to declare a variable first. You can (and you should!) read the needed values from your table column directly. Best was an inline table valued function (rather than a SP just to read something...)

  • Better performance
  • inlineable
  • You can query many InvoiceIDs at once
  • set-based

Try this (I drop the mock-table at the end - carefull with real data!):

CREATE TABLE Purchase(ID INT IDENTITY,statusXML XML, InvocieID INT, OtherValues VARCHAR(100));
INSERT INTO Purchase VALUES('<statuses>
<status>A</status>
<status>B</status>
<status>A</status>
<status>B</status>
<status>C</status>
</statuses>',100,'Other values of your row');
GO

WITH NumberedStatus AS
(
SELECT ID
,InvocieID
, ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS Nr
,stat.value('.','nvarchar(max)') AS [Status]
,OtherValues
FROM Purchase
CROSS APPLY statusXML.nodes('/statuses/status') AS A(stat)
WHERE InvocieID=100
)
SELECT *
FROM NumberedStatus
ORDER BY Nr DESC
GO
--Clean-Up
--DROP TABLE Purchase;

The result

+---+-----+---+---+--------------------------+
| 1 | 100 | 5 | C | Other values of your row |
+---+-----+---+---+--------------------------+
| 1 | 100 | 4 | B | Other values of your row |
+---+-----+---+---+--------------------------+
| 1 | 100 | 3 | A | Other values of your row |
+---+-----+---+---+--------------------------+
| 1 | 100 | 2 | B | Other values of your row |
+---+-----+---+---+--------------------------+
| 1 | 100 | 1 | A | Other values of your row |
+---+-----+---+---+--------------------------+


Related Topics



Leave a reply



Submit