Solved

How to get last 10 records in table in reverse order?

Posted on 2004-10-08
8
402 Views
Last Modified: 2010-05-18
Sorry if this has been asked before - I look in this group and the asp group but didn't find anything specifically for MSSQL - Access and Oracle but nothing that would work here.

I have a table of simple company revenue info that I'm using to populate a chart.  The current range starts at 1990 and goes to the present (the first record is 1990 and the most recent is 2004).  I need to get the most recent 10 records and display them in descending order (i.e. from 1994 to 2004) in order to feed the data to the chart in the proper order.  My trouble is, if I do a straight top 10 query, I get 1990 to 2000 - correct order but wrong range.  If I use desc to re-order the query, then I get 2004 to 1994 - right range but wrong order.

Is there a way to re-order the results of a query in MSSQL?  I have the code I'm using from the stored procedure below - it uses another query to get the query range (which is another question) and chart colors from another table, hence the added complexity.

Thanks in advance for your time and help.
--------------------------------------------------
DECLARE @chartRange int,@chartColor char(10)
SELECT @chartRange = chartRange from tbl_CompanyRevenueChart
SELECT @chartColor = colorGrossSales from tbl_CompanyRevenueChart
begin
      SELECT  top @chartRange revenueYear,revenueAmtGross,@chartColor as chartColor
      FROM    tbl_CompanyRevenue
      ORDER BY       revenueYear
end
0
Comment
Question by:saabStory
8 Comments
 
LVL 10

Expert Comment

by:AustinSeven
ID: 12259045
I'm guessing that you need to do this query...

ORDER BY revenue year desc -- for descending

If your oldest data is 1990 and you get from 1990 to 2000, it looks like your ORDER BY needs turning around.

AustinSeven
0
 
LVL 6

Expert Comment

by:boblah
ID: 12259055
Hi saabStory,

if you're in a stored procedure, then select into a temp table and select out of that in the correct order.

Alternatively, you could do it with a single query by using a sub query to return the correct records, then sort them in the outer query.

Cheers!
0
 
LVL 6

Accepted Solution

by:
acampoma earned 500 total points
ID: 12259056
DECLARE @chartRange int,@chartColor char(10)
SELECT @chartRange = chartRange from tbl_CompanyRevenueChart
SELECT @chartColor = colorGrossSales from tbl_CompanyRevenueChart
begin
     SELECT * FROM (
     SELECT  top @chartRange revenueYear,revenueAmtGross,@chartColor as chartColor
     FROM    tbl_CompanyRevenue
     ORDER BY      revenueYear) A
Order By RevenueYear Desc

end
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 21

Expert Comment

by:mastoo
ID: 12259084
DECLARE @chartRange int,@chartColor char(10)
SELECT @chartRange = chartRange from tbl_CompanyRevenueChart
SELECT @chartColor = colorGrossSales from tbl_CompanyRevenueChart
begin
     SELECT  top @chartRange revenueYear,revenueAmtGross,@chartColor as chartColor
     FROM    tbl_CompanyRevenue
  WHERE revenueYear >= ( Select Max( revenueYear ) - 10 FROM tbl_CompanyRevenue )
     ORDER BY      revenueYear
end
0
 
LVL 15

Expert Comment

by:justinbillig
ID: 12259089
does this work?

SELECT
      *
FROM
      (
            SELECT TOP 10
                  *
            FROM
                  ( SELECT * FROM tbl_company_revenue ORDER BY revenueyear DESC )
            ORDER BY
                  revenueyear ASC
      )
0
 

Author Comment

by:saabStory
ID: 12259097
Thanks - that'll work.  I had everything on my own except for the 'A' - have to find out what that's all about.  Thanks for the help.
0
 
LVL 6

Expert Comment

by:acampoma
ID: 12259103
More simply,
All you need to do is embed you original query as a subquery into a query that sorts your results in the opposite order

select * from (
select top 10 * from tbl_CompanyRevenue Order by RevenueYear  desc)
a
Order by RevenueYear  asc
0
 

Author Comment

by:saabStory
ID: 12259123
Right - that's exactly what I wound up doing.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question