Using a case statement to SELECT AS in SQL Server -


i have piece of code joining 2 tables , selecting columns, 1 of columns (netmarkdown) summed , wish select 3 different columns based on value of separate column (mkup_mkdn_rsn_cd). code have follow:

select item_id, mvndr_prty_id 'mvndr_prty_id', sum(netmarkdown)=(case mkup_mkdn_rsn_cd when 4 'code4'  when 23 'code 23' else 'code other' end),  fscl_wk_desc ##temp strmkdn left join timehierarchy on cal_prd_end_dt=cal_dt group item_id, mvndr_prty_id, fscl_wk_desc, mkup_mkdn_rsn_cd 

i error in line 1 saying "incorrect syntax near '='."

am approaching wrong? correct way approach this? help

do 3 separate sums:

select item_id, mvndr_prty_id 'mvndr_prty_id',     sum(case when mkup_mkdn_rsn_cd = 4 netmarkdown end) code4,     sum(case when mkup_mkdn_rsn_cd = 23 netmarkdown end) code23,     sum(case when mkup_mkdn_rsn_cd != 4 ,                   mkup_mkdn_rsn_cd != 23 netmarkdown end) codeother, fscl_wk_desc ##temp strmkdn left join timehierarchy on cal_prd_end_dt=cal_dt group item_id, mvndr_prty_id, fscl_wk_desc 

the problem existing code you're trying change columns appear in result set (i.e. imagine if no row had reason code of 4) - can't outside of dynamic sql.

at moment, produce nulls if particular reason code never seen - if want avoid that, wrap sum(...)s coalesce(sum(...),0)


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -