sql server - SQL CASE statement with boolean operator -
see statement:
select t1.region, count(t1.orders) as orders, case t1.week when < '5' then 'apr' when < '10' then 'may' when < '14' then 'jun' when < '19' then 'jul' when < '23' then 'aug' when < '27' then 'sep' when < '32' then 'oct' when < '36' then 'nov' when < '40' then 'dec' when < '45' then 'jan' when < '49' then 'feb' when < '53' then 'mar' end as month, dbo.inf_dates.months dbo.[nonvoice weekly_inflowcomcan] as t1 inner join dbo.inf_dates on t1.week = dbo.inf_dates.fin_wk notes = 'weekly completed' , ([2mb/sub]) = 'eth' , dbo.inf_dates.date > convert(datetime, '2017-04-03 00:00:00', 102) group by t1.region ,dbo.inf_dates.months
as can see trying group rows months instead of weeks have in table creating month column , grouping weeks together. when case statement case when t1.week < '5' 'apr'
right results forces me group weeks not want. 2 months column in table different way.
this right format, comparing varchar
<
symbol quite odd,
maybe have cast weeks integers first.
way, if week on or equal 53 ??
also, better use corresponding table rather hardcode cases.
select t1.region, count(t1.orders) orders, case when t1.week < 5 'apr' when t1.week < 10 'may' when t1.week < 14 'jun' when t1.week < 19 'jul' when t1.week < 23 'aug' when t1.week < 27 'sep' when t1.week < 32 'oct' when t1.week < 36 'nov' when t1.week < 40 'dec' when t1.week < 45 'jan' when t1.week < 49 'feb' when t1.week < 53 'mar' else 'default value' end month, dbo.inf_dates.months dbo.[nonvoice weekly_inflowcomcan] t1 inner join dbo.inf_dates on t1.week = dbo.inf_dates.fin_wk notes = 'weekly completed' , [2mb/sub]) = 'eth' , dbo.inf_dates.date > convert(datetime, '2017-04-03 00:00:00', 102) group t1.region ,dbo.inf_dates.months
Comments
Post a Comment