Solved

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

Posted on 2004-10-08
8
415 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

 
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

Increase Agility with Enabled Toolchains

Connect your existing build, deployment, management, monitoring, and collaboration platforms. From Puppet to Chef, HipChat to Slack, ServiceNow to JIRA, Splunk to New Relic and beyond, hand off data between systems to engage the right people.

Connect with xMatters.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

717 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