表一有一时间表,例:
..
1991.1.1
1991.2.3
1992.2.10
1993.5.10
1996.12.5
1999.2.5
2008.5.7
2010.6.8
..
现在假如有一个开始时间startdate '1993.1.1',一个结束时间enddate '2003.12.1'
SQL查出这样的结果
1992.2.10
1993.5.10
1996.12.5
1999.2.5
查询语句如下,
select 时间 from sheet2 where 时间 between (select MAX( 时间) from sheet2 where 时间<=startdate) and (select MAX( 时间) from sheet2 where 时间<=enddate)
求更简洁的写法?

解决方案 »

  1.   

    ----------------------------------------------------------------
    -- Author  :DBA_Huangzj(發糞塗牆)
    -- Date    :2013-09-27 16:29:20
    -- Version:
    --      Microsoft SQL Server 2014 (CTP1) - 11.0.9120.5 (X64) 
    -- Jun 10 2013 20:09:10 
    -- Copyright (c) Microsoft Corporation
    -- Enterprise Evaluation Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: ) (Hypervisor)
    --
    ----------------------------------------------------------------
    --> 测试数据:[sheet2]
    if object_id('[sheet2]') is not null drop table [sheet2]
    go 
    create table [sheet2]([时间] datetime)
    insert [sheet2]
    select '1991.1.1' union all
    select '1991.2.3' union all
    select '1992.2.10' union all
    select '1993.5.10' union all
    select '1996.12.5' union all
    select '1999.2.5' union all
    select '2008.5.7' union all
    select '2010.6.8'
    --------------开始查询--------------------------select * from [sheet2] WHERE [时间] BETWEEN '1993.1.1' AND  '2003.12.1'
    ----------------结果----------------------------
    /* 
    时间
    -----------------------
    1993-05-10 00:00:00.000
    1996-12-05 00:00:00.000
    1999-02-05 00:00:00.000
    */
      

  2.   

    select * from [sheet2] where [时间]=(select MAX([时间]) from [sheet2] where [时间]<'1993.1.1')
    union all
    select * from [sheet2] WHERE [时间] BETWEEN '1993.1.1' AND  '2003.12.1'也可以查,不过跟你那个也没多大区别