sql server - Display all months even if values are NULL -


good day! working on chart need display months in year show sales per month. far, able display month there corresponding values. here stored procedure query far.

select (datename (month, dateadd ( month, datepart(month, order_date), -1) )) month_name,   sum ([order].net_amount) total_sales   [order], order_details   [order].order_id = order_details.order_id   --and (datename (month, dateadd ( month, datepart(month, order_date), -1) )) = (datename (month, dateadd ( month, datepart(month, @order_month), -1) ))    group month([order].order_date)   order month_name 

it displays 1 month , sales month. can me out on this? in advanced!

at first please use proper join syntax , aliases.

you can create cte months , cte output , join them:

;with mcte (     select cast('2016-01-01' datetime) month_name     union     select dateadd(month,1,month_name)     mcte     datepart(month,month_name) < 12 ), octe (     select  (datename (month, dateadd ( month, datepart(month, order_date), -1) )) month_name,             sum (o.net_amount) total_sales     [order] o     inner join order_details od         on o.order_id = od.order_id         --and (datename (month, dateadd ( month, datepart(month, order_date), -1) )) = (datename (month, dateadd ( month, datepart(month, @order_month), -1) ))      group month(o.order_date) )   select  datename(month,m.month_name) month_name,         o.total_sales mcte m left join octe o     on o.month_name = datename(month,m.month_name) 

this gave month names , total sales, if there no total sales - show nulls.


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -