Solved

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

Posted on 2004-10-08
8
390 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
 
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

759 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now