开发者

Sql Server view - Select certain column depending on the value of another column

开发者 https://www.devze.com 2023-04-04 08:35 出处:网络
I have a table \'Products\' that has these columns ProductId OriginalPrice SalePrice IsOnSale (bit) Is it possible to create a view \'ProductsView\' that has these columns

I have a table 'Products' that has these columns

ProductId

OriginalPrice

SalePrice

IsOnSale (bit)

Is it possible to create a view 'ProductsView' that has these columns

ProductId

Price

where price is either OriginalPrice or SalePr开发者_开发技巧ice depending on the value of IsOnSale?

Thanks in advance!


Yes, use a case statement:

Create view CurrentPrice AS
SELECT ProductId
     , Price = CASE 
WHEN IsOnSale = 1 THEN SalePrice
ELSE OriginalPrice
END
0

精彩评论

暂无评论...
验证码 换一张
取 消