是这样的..在数据到表中是这样录的:
ID(自动编号) date         name        field1      field2
-------------------------------------------------
1            2007-2-10     张三        text1       text2
2            2007-2-11     张三        text1       text2
3            2007-2-12     李四        text3       text4
4            2007-2-10     张十        text5       text6
5            2007-2-11     张十        text5       text6
...........
在以上记录中,最多存在2条记录除日期字段(且日期是相邻的)外其他字段信息都相同当我在查询时候,如果选择时间段查询,比如查询2007-2-10到2007-2-11期间张三的数据,我想在查询结果中显示为如下的形式:
date                       name        field1      field2
2007-2-10  2007-2-11       张三        text1       text2或者查询2007-2-10到2007-2-11之间所有的数据,则查询显示信息为:
date                       name        field1      field2
---------------------------------------------------------
2007-2-10  2007-2-11       张三        text1       text2
2007-2-12                  李四        text3       text4
2007-2-10  2007-2-11       张十        text5       text6就是除日期不同其他字段信息相同的则将日期字段信息显示一起....有什么方法实现?
或者更改数据库设置也行啊!
高手请指点!!!!已经郁闷了一个星期了?

解决方案 »

  1.   

    --创建测试环境
    create table t(ID int,[date] datetime,name varchar(10),field1 varchar(10),field2 varchar(10))--插入测试数据
    insert t(ID,[date],name,field1,field2)
    select '1','2007-2-10','张三','text1','text2' union all
    select '2','2007-2-11','张三','text1','text2' union all
    select '3','2007-2-12','李四','text3','text4' union all
    select '4','2007-2-10','张十','text5','text6' union all
    select '5','2007-2-11','张十','text5','text6'--求解过程
    select min(date),case when count(1) > 1 then max(date)else null end
    ,name,field1,field2
    from t
    group by name,field1,field2--删除测试环境
    drop table t/*--测试结果
    ------------------------- -------------------------- ---------- ---------- ---------- 
    2007-02-12 00:00:00.000   NULL                      李四         text3      text4
    2007-02-10 00:00:00.000   2007-02-11 00:00:00.000   张三         text1      text2
    2007-02-10 00:00:00.000   2007-02-11 00:00:00.000   张十         text5      text6(所影响的行数为 3 行)
    */
      

  2.   

    谢谢楼上的。。请教下:case when count(1) > 1 then max(date)这个是什么意思呢??
      

  3.   

    case when 条件 then 取值 else 取值 end 判断条件,根据不同的条件取值。
    如果有两个张三即有两个日期,则前面显示小的,后面显示大的。
    如果只有一个李四,即只有一个日期,则前面显示日期,后面为空。
      

  4.   

    改数据结构就这样
    ID(自动编号) begin_date   end_date    name     field1      field2