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
Post a Comment