假定有一个表
  表名:table1
有如下数据列
 id  col1 col2  col3  col4
  1    a         aa    aa
  2    b    d    aa    aa
  3    c         aa    aa 
我想查询出如下效果,应该怎样写  id    col1   col2  value1  value2 
  1     yes     no     a     
  2     yes     yes    b       d
  3     yes     no     c   请各位帮帮忙,谢谢!
  

解决方案 »

  1.   

    把数据再排一下!估计要用到 case when ... then ... else ... end
      

  2.   

    假定有一个表
      表名:table1
    有如下数据列
     id   col1   col2   col3   col4
      1    a             aa     aa
      2    b      d      aa     aa
      3    c             aa     aa  
    我想查询出如下效果,应该怎样写   id    col1   col2   value1  value2  
      1     yes    no      a   
      2     yes    yes     b       d
      3     yes    no      c   请各位帮帮忙,谢谢!
      

  3.   

    假定有一个表
      表名:table1
    有如下数据列
     id      col1     col2     col3      col4
      1       a                 aa        aa
      2       b         d       aa        aa
      3       c                 aa        aa  
    我想查询出如下效果,应该怎样写   id     col1     col2     value1    value2  
      1      yes      no        a   
      2      yes      yes       b         d
      3      yes      no        c   请各位帮帮忙,谢谢!
    这样排行不行,看的明白吗?
      

  4.   

    select id ,when case col1 is null then 'no' else 'yes' end as col1, when case col2 is null then 'no' else 'yes' end as col2,col1 as value1,col2 as value2
      

  5.   

    --------------------SQL Server数据格式化工具-------------------
    ---------------------------------------------------------------
    -- DESIGNER :happycell188(喜喜)
    --       QQ :584738179
    -- Development Tool :Microsoft Visual C++ 6.0    C Language 
    -- FUNCTION :CONVERT DATA TO T-SQL
    ---------------------------------------------------------------
    -- Microsoft SQL Server  2005
    -- Developer Edition on Microsoft Windows XP [版本 5.1.2600]
    ---------------------------------------------------------------
    ---------------------------------------------------------------use test
    go
    if object_id('test.dbo.tb') is not null drop table tb
    -- 创建数据表
    create table tb
    (
    id int,
    col1 char(2),
    col2 char(2),
    col3 char(3),
    col4 char(3)
    )
    go
    --插入测试数据
    insert into tb select 1,'a',null,'aa','aa'
    union all select 2,'b','d','aa','aa'
    union all select 3,'c',null,'aa','aa'
    go
    --代码实现select id,col1=case when col1 is null then 'no' else 'yes' end,
    col2=case when col2 is null then 'no' else 'yes' end,
    value1=isnull(col1,''),
    value2=isnull(col2,'')
    from tb/*测试结果id   col1   col2   value1   value2   
    -----------------------------------
    1 yes no a    
    2 yes yes b  d 
    3 yes no c    (3 行受影响)
    */