27,580
社区成员
发帖
与我相关
我的任务
分享
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
*/
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)
*/
select 客户编号,是否有病史=max(是否有病史),是否住院过=max(是否住院过)
from [Table] a
group by 客户编号