一个数据合并问题

luoye20081031 2008-10-24 01:55:02
数据结构表如下:

编号 客户编号 是否有病史 是否住院过
1 111 Y N
2 111 N Y
3 222 N Y
4 222 Y N


现在想变成:

   客户编号 是否有病史 是否住院过
111 Y Y
22 Y Y

  即同一客户数据进行合并,是否有病史、是否住院过只要有一个Y植就是Y,否则为N

请问SQL怎么实现?谢谢!


...全文
50 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
叶子 2008-10-24
  • 打赏
  • 举报
回复

declare @table table(id int ,number nvarchar(20) , isone nvarchar(20),istwo nvarchar(200))
insert into
@table
select
1, '111','Y','N'
union all
select
2,'111','N','Y '
union all
select
3,'222','N','Y'
union all
select
4 ,'222','Y','N'


select number as '客户编号',是否有病史=max(isone),是否住院过=max(istwo)
from @table
group by number
/*
客户编号 是否有病史 是否住院过
-------------------- -------------------- ----------------------------
111 Y Y
222 Y Y
*/



还是一楼的厉害!
叶子 2008-10-24
  • 打赏
  • 举报
回复

declare @table table(id int ,number nvarchar(20) , isone nvarchar(20),istwo nvarchar(200))
insert into
@table
select
1, '111','Y','N'
union all
select
2,'111','N','Y '
union all
select
3,'222','N','Y'
union all
select
4 ,'222','Y','N'


select bb.*,cc.istwo from (
(select distinct number ,'Y' isone from @table where number in(
select number from @table where isone='Y' group by number)) bb
left join
(select distinct number ,'Y' istwo from @table where number in(
select number from @table where istwo='Y' group by number)) cc
on bb.number =cc.number )

/*结果
number isone istwo
-------------------- ----- -----
111 Y Y
222 Y Y

(2 row(s) affected)
*/
chuifengde 2008-10-24
  • 打赏
  • 举报
回复
select 客户编号,是否有病史=max(是否有病史),是否住院过=max(是否住院过)
from [Table] a
group by 客户编号

27,580

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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