CASE Statement SQL Server 2012 -
using pubs db have created following union all, trying same case stmt.
select t.title_id 'title id', t.ytd_sales 'ytd sales', t.price 'original price', 'new price' = case t.ytd_sales when (t.ytd_sales < 2500.00) convert(decimal(9,2),round (t.price*1.15,2)) when (t.ytd_sales between 2500.00 , 10000.00) convert(decimal(9,2),round(t.price*1.10,2)) when (t.ytd_sales > 10000.00) convert(decimal(9,2),round(t.price*1.05,2)) else convert(decimal(9,2),round(t.price*1.00,2)) end titles t ;
it not comparison/special operators. possible case stmt? thanks
syntactically, martin smith indicates, question duplicate of sql server case .. when .. expression.
the proper syntax query be
select t.title_id [title id], t.ytd_sales [ytd sales], t.price [original price], case when (t.ytd_sales < 2500.00) convert(decimal(9,2),round (t.price*1.15,2)) when (t.ytd_sales between 2500.00 , 10000.00) convert(decimal(9,2),round(t.price*1.10,2)) when (t.ytd_sales > 10000.00) convert(decimal(9,2),round(t.price*1.05,2)) else convert(decimal(9,2),round(t.price*1.00,2)) end [new price] dbo.titles t ;
at risk of being preachy: column aliases should in square brackets (as [title id]
), not single quotes (that string literals). ref: tsql select clause
Comments
Post a Comment