对表tableA中有记录:
字段  index    file    file2    file3    file4
记录    1      name     null     null     name4
        2      null     null     null     name4
        3      null     null     name3    null
        4      name     null     null     null
如上,为null即为空,以index作为条件查询4条记录,最后生成一条结果如下
字段  index    file    file2    file3    file4
记录    1      name    null    name3    name4
4条记录中对相应字段,如果值不为空,取其中一个非空值作为最后值,如果相应字段全为空最终也为空
index 以最小值作为查询后最后值
求教怎样实现?

解决方案 »

  1.   

    select MIN(index) as [index],MAX(file) as file, MAX(file1) as file1,MAX(file2) as file2,
    MAX(file3) as file3,MAX(file4) as file4
    from tablea
      

  2.   

    -- =========================================
    -- -----------t_mac 小编-------------
       ---希望有天成为大虾---- 
    -- =========================================IF OBJECT_ID('tb') IS NOT NULL
      DROP TABLE tb
    GO
    CREATE TABLE tb([index] int,[file] varchar(10),file2 varchar(10),file3 varchar(10),file4 varchar(10))
    go
    insert into tb
    select 1,'name',null,null,'name4' union all
    select 2,null,null,null,'name4' union all
    select 3,null,null,'name3',null union all
    select 4,'name',null,null,null 
    go
    select MIN([index]) as [index],MAX([file]) as [file],MAX(file2) as file2,
    MAX(file3) as file3,MAX(file4) as file4
    from tb/*------------
    1 name NULL name3 name4
    -------*/
      

  3.   

    谢谢,按照你的方法,我已经解决问题了,以前没用过min,max函数,学习了
      

  4.   

    我SQL好烂的,一定虚心学习,好好学基础~~~
      

  5.   

    有什么好的专门讲SQL的基础书没,麻烦推荐一本吧!
      

  6.   

    呵呵呵 多来CSDN ~~~
    多提问题 这里高手不是一般多!!
      

  7.   

    我平时上班上不了网,很少来上,不过过去几个SQL问题都是在这得到解决的,厉害的人太多了,有压力...