[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 428
  • Last Modified:

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
0
saabStory
Asked:
saabStory
1 Solution
 
AustinSevenCommented:
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
 
boblahCommented:
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
 
acampomaCommented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
mastooCommented:
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
 
justinbilligCommented:
does this work?

SELECT
      *
FROM
      (
            SELECT TOP 10
                  *
            FROM
                  ( SELECT * FROM tbl_company_revenue ORDER BY revenueyear DESC )
            ORDER BY
                  revenueyear ASC
      )
0
 
saabStoryAuthor Commented:
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
 
acampomaCommented:
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
 
saabStoryAuthor Commented:
Right - that's exactly what I wound up doing.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now