求一sql语句

wanghao2979 2010-06-04 08:08:29
有一个评价表 t_msg

字段
stu_ID stage(阶段)

比如表的内容的
B0001 一
B0001 二
B0001 三

现在需要这样的一条记录

学生ID 阶段一 阶段二 阶段三 阶段四
B0001 已评 已评 已评 未评


如果表中stage 含一 就证明阶段一已评,如果不含一就证明一未评

大家谁来试试看
...全文
84 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
dawugui 2010-06-04
  • 打赏
  • 举报
回复
select stuid , 
max(case stage when '一' then '已评' else '未评' end) [阶段一],
max(case stage when '二' then '已评' else '未评' end) [阶段二],
max(case stage when '三' then '已评' else '未评' end) [阶段三],
max(case stage when '四' then '已评' else '未评' end) [阶段四]
from t_msg
group by stuid
GOODlivelife 2010-06-04
  • 打赏
  • 举报
回复
if object_id('[t_msg]') is not null drop table [t_msg]
go
create table [t_msg]([stu_ID] varchar(5),[stage] varchar(2))
insert [t_msg]
select 'B0001','一' union all
select 'B0001','二' union all
select 'B0001','三'

select stu_ID,
阶段一=max(case when stage='一' then '已评' else '未评' end),
阶段二=max(case when stage='二' then '已评' else '未评' end),
阶段三=max(case when stage='三' then '已评' else '未评' end),
阶段四=max(case when stage='四' then '已评' else '未评' end)
from t_msg
group by stu_ID
/**
stu_ID 阶段一 阶段二 阶段三 阶段四
------ ---- ---- ---- ----
B0001 已评 已评 已评 未评

(1 行受影响)
**/
GOODlivelife 2010-06-04
  • 打赏
  • 举报
回复
select stu_ID,
阶段一=max(case when stage='一' then '已评' else '未评' end),
阶段二=max(case when stage='二' then '已评' else '未评' end),
阶段三=max(case when stage='三' then '已评' else '未评' end),
阶段四=max(case when stage='四' then '已评' else '未评' end),
from t_msg
group by stu_ID
wanghao2979 2010-06-04
  • 打赏
  • 举报
回复
因为只能局限用sql

所以有些棘手。如果没有好的方法。我考虑再建立个表.
nalnait 2010-06-04
  • 打赏
  • 举报
回复
行转列 精华帖多的是


破折号 2010-06-04
  • 打赏
  • 举报
回复
我也有类似问题 帮顶

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧