日期:2014-05-18  浏览次数:20415 次

SQL 数据显示疑难问题
问题如下


------解决方案--------------------
SQL code

if OBJECT_ID('test')is not null
drop table test
go
create table test(
[900类型] varchar(20),
[1800类型] varchar(20),
[TD类型] varchar(20)
)
go
insert test
select 'BTS-008','BTS-850','ABS-22' union all
select '','BTS-851','ABS-23' union all
select 'KJH-55','LJD-002','' union all
select 'BTS-010','','' union all
select '','BTS-853','' union all
select '','',''
go
with t
as(
select px=ROW_NUMBER()over(order by getdate()),
case when [900类型]='' then 1 else 0 end value1,
case when [1800类型]='' then 1 else 0 end as value2,
case when [TD类型]='' then 1 else 0 end as value3,
* from test
)
select [900类型],[1800类型],[TD类型],
case when value1=1 and value2 =1 and value3=1 then '900类型_1800类型_TD类型'
     when value1=1 and value2=1 and value3=0 then '900类型_1800类型'
     when value1=1 and value2=0 and value3=1 then '900类型_TD类型'
     when value1=0 and value2=1 and value3=1 then '1800类型_TD类型'
     when value1=0 and value2 =0 and value3=1 then 'TD类型单系统'
     when value1=1 and value2 =0 and value3=0 then '900类型单系统'
     when value1=0 and value2 =1 and value3=0 then '1800类型单系统'
     else '无' end as 系统类型
from t
/*
900类型    1800类型    TD类型    系统类型
-----------------------------------------
BTS-008    BTS-850    ABS-22    无
    BTS-851    ABS-23    900类型单系统
KJH-55    LJD-002        TD类型单系统
BTS-010            1800类型_TD类型
    BTS-853        900类型_TD类型
            900类型_1800类型_TD类型
*/

------解决方案--------------------
SQL code


select *,系统类型=case 
                when [900类型] <> '' and [1800类型] <> '' and [TD类型]<>'' then '900+1800+TD'
                when [900类型] <> '' and [1800类型] <> ''  then '900+1800'
                when [900类型] <> '' and [TD类型]<>'' then '900+TD'
                when [1800类型] <> '' and [TD类型] <>'' then '1800+TD'
                when [900类型] <> '' then '900单系统'
                when [1800类型] <> '' then '1800单系统'
                when [TD类型] <> '' then 'TD单系统'
                else '无'
            end
 from test