Link to home
Start Free TrialLog in
Avatar of saabStory
saabStoryFlag for United States of America

asked on

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

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
Avatar of AustinSeven
AustinSeven

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
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!
ASKER CERTIFIED SOLUTION
Avatar of acampoma
acampoma

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
does this work?

SELECT
      *
FROM
      (
            SELECT TOP 10
                  *
            FROM
                  ( SELECT * FROM tbl_company_revenue ORDER BY revenueyear DESC )
            ORDER BY
                  revenueyear ASC
      )
Avatar of saabStory

ASKER

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.
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
Right - that's exactly what I wound up doing.