日期:2014-05-17 浏览次数:20767 次
----------------------------
-- Author :DBA_Huangzj(發糞塗牆)
-- Date :2013-04-17 18:07:23
-- Version:
-- Microsoft SQL Server 2008 R2 (SP1) - 10.50.2500.0 (X64)
-- Jun 17 2011 00:54:03
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition (64-bit) on Windows NT 6.1 <X64> (Build 7601: Service Pack 1, v.721)
--
----------------------------
--> 测试数据:[huang]
if object_id('[huang]') is not null drop table [huang]
go
create table [huang]([id] int,[高血压] int,[冠心病] int,[糖尿病] int)
insert [huang]
select 1,0,1,0 union all
select 2,1,0,1 union all
select 3,1,1,1
--------------开始查询--------------------------
select *, (CASE WHEN 高血压=1 THEN '高血压' ELSE '' END )+(CASE WHEN 冠心病=1 AND 高血压=1 THEN '、冠心病' WHEN 冠心病=1 AND 高血压=0 THEN '冠心病' ELSE '' END )
+(CASE WHEN (冠心病=1 AND 高血压=1 AND 糖尿病=1) OR(冠心病=0 AND 高血压=1 AND 糖尿病=1) OR (冠心病=1 AND 高血压=0 AND 糖尿病=1) THEN '、糖尿病' WHEN 冠心病=0 AND 高血压=1 AND 糖尿病=1 THEN '糖尿病' ELSE '' END ) AS [合并后新字段]
from [huang]
----------------结果----------------------------
/*
id 高血压 冠心病 糖尿病 合并后新字段
----------- ----------- ----------- ----------- ----------------------
1 0 1 0 冠心病
2 1 0 1 高血压、糖尿病
3 1 1 1 高血压、冠心病、糖尿病
*/