跟大家请教一个问题,或许是很简单的。不过俺搞了几个小时了啊。唉,悲哀。特此求救!!!
比如说,在数据库中有 很多条数据,其中一列是 DateTime 列,这一列是日期,现在的问题是   日期列相同的列我只取一行出来。比如说: 2008年12月23日 这天有很多数据,我需要只取 2008年12月23日  这天的  一条数据
不知道我说清楚了没有,如果不清楚,我再补充吧、

解决方案 »

  1.   

    select top 1 *
    from tb
    where 日期列>'2008-12-23' and 日期列<'2008-12-24' 
      

  2.   

    --取时间最小的一条
    select * from tb as a where not exists(select 1 from tb where datefield<a.datefield)
      

  3.   

    select top 1 * from tb where datediff(day , 日期列 , '2008-12-23') = 0
      

  4.   

    select * from tab a
    where 日期列 = (
     select top 1 日期列 from tab
     where 日期列>=convert(varchar(10),a.日期列,120)
     and 日期列<convert(varchar(10),dateadd(day,1,a.日期列),120)
     )
      

  5.   


    --按某一字段分组取最大(小)值所在行的数据
    --(爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开) 2007-10-23于浙江杭州)
    /*
    数据如下:
    name val memo
    a    2   a2(a的第二个值)
    a    1   a1--a的第一个值
    a    3   a3:a的第三个值
    b    1   b1--b的第一个值
    b    3   b3:b的第三个值
    b    2   b2b2b2b2
    b    4   b4b4
    b    5   b5b5b5b5b5
    */
    --创建表并插入数据:
    create table tb(name varchar(10),val int,memo varchar(20))
    insert into tb values('a',    2,   'a2(a的第二个值)')
    insert into tb values('a',    1,   'a1--a的第一个值')
    insert into tb values('a',    3,   'a3:a的第三个值')
    insert into tb values('b',    1,   'b1--b的第一个值')
    insert into tb values('b',    3,   'b3:b的第三个值')
    insert into tb values('b',    2,   'b2b2b2b2')
    insert into tb values('b',    4,   'b4b4')
    insert into tb values('b',    5,   'b5b5b5b5b5')
    go--一、按name分组取val最大的值所在行的数据。
    --方法1:
    select a.* from tb a where val = (select max(val) from tb where name = a.name) order by a.name
    --方法2:
    select a.* from tb a where not exists(select 1 from tb where name = a.name and val > a.val)
    --方法3:
    select a.* from tb a,(select name,max(val) val from tb group by name) b where a.name = b.name and a.val = b.val order by a.name
    --方法4:
    select a.* from tb a inner join (select name , max(val) val from tb group by name) b on a.name = b.name and a.val = b.val order by a.name
    --方法5
    select a.* from tb a where 1 > (select count(*) from tb where name = a.name and val > a.val ) order by a.name
    /*
    name       val         memo                 
    ---------- ----------- -------------------- 
    a          3           a3:a的第三个值
    b          5           b5b5b5b5b5
    */--二、按name分组取val最小的值所在行的数据。
    --方法1:
    select a.* from tb a where val = (select min(val) from tb where name = a.name) order by a.name
    --方法2:
    select a.* from tb a where not exists(select 1 from tb where name = a.name and val < a.val)
    --方法3:
    select a.* from tb a,(select name,min(val) val from tb group by name) b where a.name = b.name and a.val = b.val order by a.name
    --方法4:
    select a.* from tb a inner join (select name , min(val) val from tb group by name) b on a.name = b.name and a.val = b.val order by a.name
    --方法5
    select a.* from tb a where 1 > (select count(*) from tb where name = a.name and val < a.val) order by a.name
    /*
    name       val         memo                 
    ---------- ----------- -------------------- 
    a          1           a1--a的第一个值
    b          1           b1--b的第一个值
    */--三、按name分组取第一次出现的行所在的数据。
    select a.* from tb a where val = (select top 1 val from tb where name = a.name) order by a.name
    /*
    name       val         memo                 
    ---------- ----------- -------------------- 
    a          2           a2(a的第二个值)
    b          1           b1--b的第一个值
    */--四、按name分组随机取一条数据。
    select a.* from tb a where val = (select top 1 val from tb where name = a.name order by newid()) order by a.name
    /*
    name       val         memo                 
    ---------- ----------- -------------------- 
    a          1           a1--a的第一个值
    b          5           b5b5b5b5b5
    */--五、按name分组取最小的两个(N个)val
    select a.* from tb a where 2 > (select count(*) from tb where name = a.name and val < a.val ) order by a.name,a.val
    select a.* from tb a where val in (select top 2 val from tb where name=a.name order by val) order by a.name,a.val
    select a.* from tb a where exists (select count(*) from tb where name = a.name and val < a.val having Count(*) < 2) order by a.name,a.val
    /*
    name       val         memo                 
    ---------- ----------- -------------------- 
    a          1           a1--a的第一个值
    a          2           a2(a的第二个值)
    b          1           b1--b的第一个值
    b          2           b2b2b2b2
    */--六、按name分组取最大的两个(N个)val
    select a.* from tb a where 2 > (select count(*) from tb where name = a.name and val > a.val ) order by a.name,a.val
    select a.* from tb a where val in (select top 2 val from tb where name=a.name order by val desc) order by a.name,a.val
    select a.* from tb a where exists (select count(*) from tb where name = a.name and val > a.val having Count(*) < 2) order by a.name , a.val
    /*
    name       val         memo                 
    ---------- ----------- -------------------- 
    a          2           a2(a的第二个值)
    a          3           a3:a的第三个值
    b          4           b4b4
    b          5           b5b5b5b5b5
    */
    --七,如果整行数据有重复,所有的列都相同。
    /*
    数据如下:
    name val memo
    a    2   a2(a的第二个值)
    a    1   a1--a的第一个值
    a    1   a1--a的第一个值
    a    3   a3:a的第三个值
    a    3   a3:a的第三个值
    b    1   b1--b的第一个值
    b    3   b3:b的第三个值
    b    2   b2b2b2b2
    b    4   b4b4
    b    5   b5b5b5b5b5
    */
    --在sql server 2000中只能用一个临时表来解决,生成一个自增列,先对val取最大或最小,然后再通过自增列来取数据。
    --创建表并插入数据:
    create table tb(name varchar(10),val int,memo varchar(20))
    insert into tb values('a',    2,   'a2(a的第二个值)')
    insert into tb values('a',    1,   'a1--a的第一个值')
    insert into tb values('a',    1,   'a1--a的第一个值')
    insert into tb values('a',    3,   'a3:a的第三个值')
    insert into tb values('a',    3,   'a3:a的第三个值')
    insert into tb values('b',    1,   'b1--b的第一个值')
    insert into tb values('b',    3,   'b3:b的第三个值')
    insert into tb values('b',    2,   'b2b2b2b2')
    insert into tb values('b',    4,   'b4b4')
    insert into tb values('b',    5,   'b5b5b5b5b5')
    goselect * , px = identity(int,1,1) into tmp from tbselect m.name,m.val,m.memo from
    (
      select t.* from tmp t where val = (select min(val) from tmp where name = t.name)
    ) m where px = (select min(px) from
    (
      select t.* from tmp t where val = (select min(val) from tmp where name = t.name)
    ) n where n.name = m.name)drop table tb,tmp/*
    name       val         memo
    ---------- ----------- --------------------
    a          1           a1--a的第一个值
    b          1           b1--b的第一个值(2 行受影响)
    */
    --在sql server 2005中可以使用row_number函数,不需要使用临时表。
    --创建表并插入数据:
    create table tb(name varchar(10),val int,memo varchar(20))
    insert into tb values('a',    2,   'a2(a的第二个值)')
    insert into tb values('a',    1,   'a1--a的第一个值')
    insert into tb values('a',    1,   'a1--a的第一个值')
    insert into tb values('a',    3,   'a3:a的第三个值')
    insert into tb values('a',    3,   'a3:a的第三个值')
    insert into tb values('b',    1,   'b1--b的第一个值')
    insert into tb values('b',    3,   'b3:b的第三个值')
    insert into tb values('b',    2,   'b2b2b2b2')
    insert into tb values('b',    4,   'b4b4')
    insert into tb values('b',    5,   'b5b5b5b5b5')
    goselect m.name,m.val,m.memo from
    (
      select * , px = row_number() over(order by name , val) from tb
    ) m where px = (select min(px) from
    (
      select * , px = row_number() over(order by name , val) from tb
    ) n where n.name = m.name)drop table tb/*
    name       val         memo
    ---------- ----------- --------------------
    a          1           a1--a的第一个值
    b          1           b1--b的第一个值(2 行受影响)
    */
      

  6.   

    select * from tab a
    where 日期列 = (
     select top 1 日期列 from tab
     where 日期列>=convert(varchar(10),a.日期列,120)
     and 日期列<convert(varchar(10),dateadd(day,1,a.日期列),120)
     order by 日期列
     )
      

  7.   


    假设有一个ID字段,时间字段为DT,按同天的DT取最大(最小)IDselect t.* from tb t where id = (select max(id) from tb where convert(varchar(10),dt,120) = convert(varchar(10),t.dt,120))
    select t.* from tb t where id = (select min(id) from tb where convert(varchar(10),dt,120) = convert(varchar(10),t.dt,120))
      

  8.   

    以上是取每天的第一条,如果要最后一条,把
    order by 日期列
    改成
    order by 日期列 desc
    如果要随便一条,把
    order by 日期列
    改成
    order by newid()
      

  9.   

    select 1* 
    from tb
    where 日期列=convert(varchar(10),日期,120)
      

  10.   

    --try
    select * from tb 
    where 日期列 in ( select max(日期列) from tb group by convert(varchar(10),日期列,120))
      

  11.   

    --取一天中的最小时间那条:
    select * from tb 
    where 日期列 in ( select min(日期列) from tb group by convert(varchar(10),日期列,120))
      

  12.   

    select * from tb 
    where 日期列 in ( select min(日期列) from tb group by convert(varchar(10),日期列,120))orselect * from tb 
    where 日期列 in ( select max(日期列) from tb group by convert(varchar(10),日期列,120))
      

  13.   

    取那条日期相同的,并且最新生成的记录
    select top 1 * 
    from tb 
    where 日期列 like '2008-12-23%' order by 日期列 desc
      

  14.   

    select distinct * from table1 where datetime=d1
    保证没有重复的纪录,不知道你是不是这个意思。
      

  15.   

    如果表里面有自增长的列(假设叫ID)就好办了,相同日期的记录取最大ID或最小ID就行了。
      

  16.   

    假设表中有自增长ID:select * from tb where id in (select min(id) from tb group by DateTime_col )
      

  17.   

    select * from (select *,rn=row_number()over(partition by convert(varchar(10),日期,120) order by 日期)
    from 表)a where rn=1
      

  18.   

    select top 1 * from tb where datediff(day,'日期','2008-12-23') = 0 order by '日期' desc
      

  19.   

    那随便一条简单的很,上面好多都可以,top最好用了
      

  20.   

    ConID       ConDateTime               conModey    
    1 2008-12-21 00:00:00.000         10
    2 2008-12-21 00:00:00.000 100
    3 2008-12-22 00:00:00.000         1000
    4 2008-12-22 00:00:00.000 1
    5 2008-12-23 00:00:00.000         20
    6 2008-12-23 00:00:00.000 3.8
    7 2008-12-23 00:00:00.000         20
    8 2008-12-23 00:00:00.000 3.8要得到的结果ConDateTime
    2008-12-21 00:00:00.000
    2008-12-22 00:00:00.000
    2008-12-23 00:00:00.000  
      

  21.   

    select distinct ConDateTime from table where ConDateTime between '20081221' and '20081223'当写成存贮过程的时候。传递俩个字符串参数。当然格式必须可以隐式转化为日期型。
      

  22.   


    select  c.ConDateTime  from ConsumeData c where c.ConDateTime='2008-12-22'  group by c.ConDateTime唉,现在都鄙视自己了,这么点问题都解决不了呀。呜呜……老了~~~~
      

  23.   

    select distinct ConDateTime from tb order by 1