执行SQL语句
Select  WValue From [SSBallDB].[dbo].[WMNumber] Where WNo=01     
Select  WValue From [SSBallDB].[dbo].[WMNumber] Where WNo=06    
Select  WValue From [SSBallDB].[dbo].[WMNumber] Where WNo=12 
Select  WValue From [SSBallDB].[dbo].[WMNumber] Where WNo=22  
Select  WValue From [SSBallDB].[dbo].[WMNumber] Where WNo=23    
Select  WValue From [SSBallDB].[dbo].[WMNumber] Where WNo=26 得到结果WValue
-----------
2(1 行受影响)WValue
-----------
9(1 行受影响)WValue
-----------
4(1 行受影响)WValue
-----------
9(1 行受影响)WValue
-----------
4(1 行受影响)WValue
-----------
3
(1 行受影响)但我希望结果是这样的,请问如何修改WNO1. .WNO2. .WNO3.  .WNO4.  .WNO5.  .WNO6
--------------------------------------------
. .2.  .9.    .4.     .9.     .4.     .3
(1 行受影响)备注:用“.  .”表示列与列之间的间隔

解决方案 »

  1.   


    可以这样啊!
    Select WValue From [SSBallDB].[dbo].[WMNumber] Where WNo in (01,06,12,22,23,26)还有就是你的查询记录的条数是否固定,也就是最后的结果的列数是否固定,如果不固定那就要用动态SQL语句来处理了。
      

  2.   

    select max(case when WNo=01 then WValue else null end),
           max(case when WNo=06 then WValue else null end),
           max(case when WNo=12 then WValue else null end),
           max(case when WNo=22 then WValue else null end)
    from [SSBallDB].[dbo].[WMNumber]
      

  3.   

    select id,
    WNO1=max(case when WNo=01 then WValue end),
    WNO2=max(case when WNo=06 then WValue end),
    WNO3=max(case when WNo=12 then WValue end),
    WNO4=max(case when WNo=22 then WValue end),
    WNO5=max(case when WNo=23 then WValue end),
    WNO6=max(case when WNo=26 then WValue end)
    from 
     (select 1 as id,WNO,WVALUE FROM [SSBallDB].[dbo].[WMNumber]) T
    GROUP BY ID  
      

  4.   


    执行后,结果如下
    WValue
    -----------
    2
    9
    4
    9
    4
    3(6 行受影响我要的结果是WValue
    -----------
    2 9  4  9  4  3(1 行受影响
      

  5.   


    Select 
    (Case When WNo = 01 Then WValue Else '' end) WNO1,
    (Case When WNo = 06 Then WValue Else '' end) WNO2,
    (Case When WNo = 12 Then WValue Else '' end) WNO3,
    (Case When WNo = 22 Then WValue Else '' end) WNO4, 
    (Case When WNo = 23 Then WValue Else '' end) WNO5, 
    (Case When WNo = 26 Then WValue Else '' end) WNO6  
     From [User]
      

  6.   


    Select 
    (Case When WNo = 01 Then WValue Else '' end) WNO1,
    (Case When WNo = 06 Then WValue Else '' end) WNO2,
    (Case When WNo = 12 Then WValue Else '' end) WNO3,
    (Case When WNo = 22 Then WValue Else '' end) WNO4, 
    (Case When WNo = 23 Then WValue Else '' end) WNO5, 
    (Case When WNo = 26 Then WValue Else '' end) WNO6  
     From [SSBallDB].[dbo].[WMNumber]
      

  7.   

    这个得用case when转换了吧。
    Insert Into 表
    Select 
      (Case When WNo = 01 Then WValue Else '' end) WNO1,
      (Case When WNo = 06 Then WValue Else '' end) WNO2,
      (Case When WNo = 12 Then WValue Else '' end) WNO3,
      (Case When WNo = 22 Then WValue Else '' end) WNO4, 
      (Case When WNo = 23 Then WValue Else '' end) WNO5, 
      (Case When WNo = 26 Then WValue Else '' end) WNO6  
    From [SSBallDB].[dbo].[WMNumber]
      

  8.   


    最后的结果的列数是固定7列的,就是WNO1. .WNO2. .WNO3. .WNO4. .WNO5. .WNO6和这6个数的和值WVALUE,一共7列,谢谢指导
      

  9.   

    select 
    WNO1=max(case when WNo=07 then WValue end),
    WNO2=max(case when WNo=17 then WValue end),
    WNO3=max(case when WNo=23 then WValue end),
    WNO4=max(case when WNo=24 then WValue end),
    WNO5=max(case when WNo=27 then WValue end),
    WNO6=max(case when WNo=32 then WValue end)
    into #临时表 
    from 
     (select WNO,WVALUE FROM [SSBallDB].[dbo].[WMNumber]) Tselect WNO1,WNO2,WNO3,WNO4,WNO5,WNO6,WNO1+WNO2+WNO3+WNO4+WNO5+WNO6 as WVALUEfrom #临时表
    drop table #临时表