sql - How to order result in ascending order if it is Month Name -


i writing query sum values 2 different tables , find sale , purchase per month. after ordering in ascending order , expected giving false result, i.e august coming before february.

how can modify query result correctly?

here query:

 select coalesce(o.month , p.month) month,         isnull([sale cost],0) [sale cost],         isnull([purchase cost],0) [purchase cost]  (      select sum(s.cost) [purchase cost],             datename( month , dateadd( month , month(s.[date_added]) , -1 ) ) month      dbo.spare_inventory s      year(getdate()) = year(s.[date_added])      group month(s.[date_added])      ) o  full join (      select sum(b.[total price]) [sale cost],             datename(month, dateadd( month , month(b.[dateofsale]) , -1 ) ) month      dbo.spare_sale_db b      year(getdate()) = year(b.[dateofsale])      group month(b.[dateofsale])) p  on o.month = p.month  order month asc 

you have kind of given answer yourself; want rows oredered month number, not month name. order month number.

select    coalesce(o.month , p.month) month,   isnull([sale cost],0) [sale cost] ,    isnull([purchase cost],0)[purchase cost]   (   select      sum(s.cost) [purchase cost],      dateadd( month , month(s.[date_added]) , -1 ) monthno,     datename( month , dateadd( month , month(s.[date_added]) , -1 ) ) month    dbo.spare_inventory s    year(getdate())=year(s.[date_added])    group month(s.[date_added])  ) o full join  (   select      sum(b.[total price]) [sale cost],      datename( month , dateadd( month , month(b.[dateofsale]) , -1 ) ) month    dbo.spare_sale_db b    year(getdate())=year(b.[dateofsale]) group month(b.[dateofsale]) ) p on o.month = p.month order o.monthno asc; 

Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -