34,576
社区成员
发帖
与我相关
我的任务
分享
select id ,
max(case user_sex when 0 then [user_name] else '' end) '男',
max(case user_sex when 1 then [user_name] else '' end) '女'
from userinfo
group by id , [user_name]
id 男 女
001 null 张三
002 李四 null
create table userInfo
(
id int ,
[user_name] varchar(20) not null,---------用户名
user_sex int
)
insert into userInfo values('001' ,'张三',0)
insert into userInfo values('002' ,'李四',1)
select id , [user_name] , case when user_sex = 0 then '男' else '女' end user_sex from userinfo
/*
id user_name user_sex
----------- -------------------- --------
1 张三 男
2 李四 女
(所影响的行数为 2 行)
*/
select id , [user_name] ,
max(case user_sex when 0 then '男' else '' end) '男',
max(case user_sex when 1 then '女' else '' end) '女'
from userinfo
group by id , [user_name]
/*
id user_name 男 女
----------- -------------------- ---- ----
2 李四 女
1 张三 男
(所影响的行数为 2 行)
*/
drop table userInfo
select id,
max(case when user_sex ='男' then user_name else '' end) as '男',
max(case when user_sex ='女' then user_name else '' end) as '女'
from tb
group by id
select SUBSTRING('男女',user_sex+1,1) from tb
select id,
max(case when user_sex ='男' then user_name else null end) '男',
max(case when user_sex ='女' then user_name else null end) '女'
from tb
group by id
select id , user_name , case when user_sex when 0 then '男' else '女' end user_sex from userinfo