Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

SqlServer Series Notes-- case when statement

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

CASE usage

CASE

WHEN condition1 THEN returnvalue1

WHEN condition 2 THEN returnvalue2

WHEN condition 3 THEN returnvalue3

ELSE defaultreturnvalue

END

Equivalent to if... Else... Else... .

SELECT FName, FWeight

(CASE

WHEN FWeight50 THEN 'Feifei'

ELSE 'ok'

END)

As isnormal

FROM T_Person

Create table bs

(

Bsname char (20)

Bsflag char (20)

Bstime datetime

)

Insert bs values ('Rocket', 'win', '2008-01-10')

Insert bs values ('Rocket', 'negative', '2008-01-11')

Insert bs values ('Rocket', 'win', '2008-01-10')

Insert bs values ('Bull', 'negative', '2008-01-10')

Insert bs values ('Bull', 'Sheng', '2008-01-11')

Insert bs values ('Bull', 'negative', '2008-01-10')

Select * from bs

Select bsname, win = sum (case when bsflag= 'win' then 1 else 0 end)

Negative = sum (case when bsflag=' negative 'then 1 else 0 end)

From bs group by bsname

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report