Select * from (
Select id,name,'1' As sm,sm1 As smvalue from tablea
Union All
Select id,name,'2' As sm,sm2 As smvalue from tablea
Union All
Select id,name,'3' As sm,sm3 As smvalue from tablea
Union All
Select id,name,'4' As sm,sm4 As smvalue from tablea
Union All
Select id,name,'5' As sm,sm5 As smvalue from tablea
) A
Where smvalue<>'' And smvalue Is Not Null
Order By id,name,sm

解决方案 »

  1.   

    --建立测试环境
    Create Table tablea
    (id Varchar(10),
     name Varchar(10),
     sm1 Int,
     sm2 Int,
     sm3 Int,
     sm4 Int,
     sm5 Int)
    --插入数据
    Insert tablea Values('0001',    'aa',       7,      8,       3,      4,       6)
    Insert tablea Values('0002',    'bb',       3,      1,       4,      2,       Null)
    Insert tablea Values('0003',    'cc',       2,      5,       7,      Null,    Null)
    Insert tablea Values('0004',    'dd',       4,      6,       3,      1,       Null)
    Insert tablea Values('0005',    'ee',       7,      5,       2,      8,       Null)
    Insert tablea Values('0006',    'ff',       5,      3,       4,      1,       7)
    GO
    --测试
    Select * from (
    Select id,name,'1' As sm,sm1 As smvalue from tablea
    Union All
    Select id,name,'2' As sm,sm2 As smvalue from tablea
    Union All
    Select id,name,'3' As sm,sm3 As smvalue from tablea
    Union All
    Select id,name,'4' As sm,sm4 As smvalue from tablea
    Union All
    Select id,name,'5' As sm,sm5 As smvalue from tablea
    ) A
    Where smvalue<>'' And smvalue Is Not Null And id='0004'
    Order By id,name,sm
    --删除测试环境
    Drop table tablea
    GO
    --结果
    /*
    id name sm smvalue
    0004 dd 1 4
    0004 dd 2 6
    0004 dd 3 3
    0004 dd 4 1
    */
      

  2.   

    Select id,name,'1' As sm,sm1 As smvalue from tablea
    where id='0004' and isnull(sm1,'')<>''
    Union All
    Select id,name,'2' As sm,sm2 As smvalue from tablea
    where id='0004' and isnull(sm1,'')<>''
    Union All
    Select id,name,'3' As sm,sm3 As smvalue from tablea
    where id='0004' and isnull(sm1,'')<>''
    Union All
    Select id,name,'4' As sm,sm4 As smvalue from tablea
    where id='0004' and isnull(sm1,'')<>''
    Union All
    Select id,name,'5' As sm,sm5 As smvalue from tablea
    where id='0004' and isnull(sm1,'')<>''
      

  3.   

    如果是要生成一个新表--建立测试环境
    Create Table tablea
    (id Varchar(10),
     name Varchar(10),
     sm1 Int,
     sm2 Int,
     sm3 Int,
     sm4 Int,
     sm5 Int)
    --插入数据
    Insert tablea Values('0001',    'aa',       7,      8,       3,      4,       6)
    Insert tablea Values('0002',    'bb',       3,      1,       4,      2,       Null)
    Insert tablea Values('0003',    'cc',       2,      5,       7,      Null,    Null)
    Insert tablea Values('0004',    'dd',       4,      6,       3,      1,       Null)
    Insert tablea Values('0005',    'ee',       7,      5,       2,      8,       Null)
    Insert tablea Values('0006',    'ff',       5,      3,       4,      1,       7)
    GO
    --测试
    Select * Into tableb from (
    Select id,name,'1' As sm,sm1 As smvalue from tablea
    Union All
    Select id,name,'2' As sm,sm2 As smvalue from tablea
    Union All
    Select id,name,'3' As sm,sm3 As smvalue from tablea
    Union All
    Select id,name,'4' As sm,sm4 As smvalue from tablea
    Union All
    Select id,name,'5' As sm,sm5 As smvalue from tablea
    ) A
    Where smvalue<>'' And smvalue Is Not Null And id='0004'
    Order By id,name,smSelect * from tableb
    --删除测试环境
    Drop table tablea,tableb
    GO
    --结果
    /*
    id name sm smvalue
    0004 dd 1 4
    0004 dd 2 6
    0004 dd 3 3
    0004 dd 4 1
    */