Query based on sorting based on month

  •  

     

    SELECT BusinessEntityID,FirstName,MiddleName,LastName,

    YEAR(OrderDate) AS OrderYear,

    DATENAME(MM,OrderDate) AS OrderMonth

    FROM Person.Person Pp

    INNER JOIN Sales.Customer Sc

    ON Sc.PersonID=Pp.BusinessEntityID

    INNER JOIN Sales.SalesOrderHeader Ss

    ON Sc.CustomerID=Ss.CustomerID

    ORDER BY BusinessEntityID,YEAR(OrderDate),MONTH(OrderDate)

    Month is not sorted properly.

    Could you please let me know what could be the issue

    291 Gustavo NULL Achong 2011 August

    291 Gustavo NULL Achong 2012 January

    291 Gustavo NULL Achong 2012 April

    291 Gustavo NULL Achong 2012 July

    291 Gustavo NULL Achong 2012 October

    291 Gustavo NULL Achong 2013 January

     

  • Define 'properly'. It looks OK to me, unless you want to sort on the name of the month, rather than its ordinal.

    The absence of evidence is not evidence of absence
    - Martin Rees
    The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
    - Phil Parkin

  • Yep, looks right to me too.

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

  • got it thanks for the clarification . I should have observed i am sorting based on year and month.

    thanks hguys

  • Also, why not just use

    ORDER BY BusinessEntityID, OrderDate

    ?

    It might perform better.

    The absence of evidence is not evidence of absence
    - Martin Rees
    The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
    - Phil Parkin

  • Phil Parkin wrote:

    Also, why not just use

    ORDER BY BusinessEntityID, OrderDate

    ?

    It might perform better.

    +1000

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply