Solved

Last 12 records

Posted on 2008-10-17
4
272 Views
Last Modified: 2010-04-21
I need to pick out the last 12 records from a dataset. My SQL code uses some casting so I think it has problems with Desc along with TOP 12. Here is my SQL

SELECT TOP 12 CAST(YEAR([Rdate]) AS VarChar(20)) + '-' + CAST(MONTH([Rdate]) AS VarChar(20)) AS Month, Count(*)  from
table1
GROUP BY CAST(YEAR([Rdate]) AS VarChar(20)) + '-' + CAST(MONTH([Rdate]) AS VarChar(20))
ORDER BY CAST(YEAR([Rdate]) AS VarChar(20)) + '-' + CAST(MONTH([Rdate]) AS VarChar(20)) Desc
0
Comment
Question by:Mr_Shaw
  • 2
  • 2
4 Comments
 
LVL 43

Expert Comment

by:TimCottee
ID: 22739159
Hello Mr_Shaw,

Probably because the months 1-9 are cast as single digits and will interfere with your expected ordering. It might be better to use:

Select Top 12 Convert(varchar(7),Rdate,120),Count(*)
From table1
Group By Convert(varchar(7),Rdate,120)
Order By Convert(varchar(7),Rdate,120) Desc

Regards,

TimCottee
0
 

Author Comment

by:Mr_Shaw
ID: 22739182
Thanks, code worked.

I need the dates to read month-year not year-month. Is there any way I can do this?
0
 
LVL 43

Accepted Solution

by:
TimCottee earned 500 total points
ID: 22739452
Mr_Shaw,

You can select a different value than the order by expression so:

SELECT TOP 12 CAST(Month([Rdate]) AS VarChar(20)) + '-' + CAST(Year([Rdate]) AS VarChar(20)) AS Month, Count(*)  from
table1
GROUP BY CAST(Month([Rdate]) AS VarChar(20)) + '-' + CAST(Year([Rdate]) AS VarChar(20))
Order By Convert(varchar(7),Rdate,120) Desc

Would display in MM-yyyy format but the ordering would be in yyyy-mm order.
TimCottee
0
 

Author Closing Comment

by:Mr_Shaw
ID: 31507085
thanks
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

There are some very powerful Data Management Views (DMV's) introduced with SQL 2005. The two in particular that we are going to discuss are sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats.   Recently, I was involved in a discu…
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

820 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