Solved

SQL order of month and year

Posted on 2013-05-14
2
463 Views
Last Modified: 2013-05-14
sql.Format("SELECT DATEPART(year,A.Lieferscheindatum)AS year,\
            DATEPART(month,A.Lieferscheindatum)AS month,\
            SUM(B.Einheitbez13wert)AS Gewicht\
            FROM Lieferscheine AS A , Lieferscheinpositionen AS B\
                      WHERE A.LieferscheinNr=B.LieferscheinNr AND A.LieferscheinNr<200000\
           GROUP BY DATEPART(month,A.Lieferscheindatum),\
            DATEPART(year,A.Lieferscheindatum)\
            ORDER by DATEPART(month,A.Lieferscheindatum)ASC,\
            DATEPART(year,A.Lieferscheindatum)DESC");

Hello,
In my SQL Table I have data from 2006 Januar till today 2013 May.
With my SQL code I will get the data in this order

        year   month
        2013  Jan
       2012  Jan
       2011  Jan
         .
         .
         .
       2006 Jan

       2013  Feb
       2012  Feb
       2011      Feb  
 .
 .
 .
2006 Feb  and so on


 

But I would like to get the following order:
2006      Jan
2006      Feb
2006        Mar
2006        Apr
.
.
.
.
.
.
.
2013     May


Thank you for your help.
500 points with a solution.
Best regards,
Thomas
0
Comment
Question by:tsp2002
2 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 39164334
Flip your ORDER BY clause around and change the sort order for the year:

ORDER by DATEPART(year,A.Lieferscheindatum)ASC,
         DATEPART(month,A.Lieferscheindatum)ASC");

Open in new window

0
 

Author Closing Comment

by:tsp2002
ID: 39164417
thank you, that was easy......got a bit rusty over time with SQL
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Improvement  ( Speed) 14 28
SQL View nearest date 5 37
Replace the integer portion in CAST with a column 4 16
horizontal scale out 29 15
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

831 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