数据类型:
Code char(6)
CreateTime datetime
Price float
Status bit数据如下: Code       CreateTime     Price Status
031002 2008-10-17 15:00:15 3.58    1
031002 2008-10-17 15:00:16 5.53    1
031002 2008-10-17 15:00:17 4.54    1
031002 2008-10-17 15:00:18 4.44    1
031002 2008-10-17 15:00:19 4.34    1
031002 2008-10-17 15:00:20 4.24    1
031012 2008-10-17 15:00:15 3.58    0
031012 2008-10-17 15:00:16 5.53    0
031012 2008-10-17 15:00:17 4.54    0
031012 2008-10-17 15:00:18 4.44    0
031012 2008-10-17 15:00:19 4.34    1
031012 2008-10-17 15:00:20 4.24    1
031012 2008-10-17 15:00:18 3.51    0
031012 2008-10-17 15:00:19 3.52    0
031012 2008-10-17 15:00:20 2.50    0
031012 2008-10-17 15:00:21 3.51    0
031012 2008-10-17 15:00:22 3.52    0
031012 2008-10-17 15:00:23 2.50    0
031012 2008-10-17 15:00:24 3.51    0
031012 2008-10-17 15:00:25 3.52    0
031012 2008-10-17 15:00:26 2.50    0
031033 2008-10-17 15:00:21 4.53    0
031033 2008-10-17 15:00:22 3.55    0
031033 2008-10-17 15:00:23 3.55    0
031033 2008-10-17 15:00:24 5.57    0
031033 2008-10-17 15:00:25 5.57    1
031033 2008-10-17 15:00:26 5.57    1
031033 2008-10-17 15:00:27 5.57    1
031033 2008-10-17 15:00:28 5.57    1
我想要的结果是031012 2008-10-17 15:00:19 4.34    1
031033 2008-10-17 15:00:25 5.57    1
得出这个结果的要求是: 按照Code分组,按照时间asc排序 Status从0变为1的第一条,
如果该Code的Status全是0或者是1就不要,只有当Status为0,比他晚的时间突然变成1了就要该第一条。请问这句SQL要怎么写,谢谢!

解决方案 »

  1.   

    ----------------------------------------------------------------
    -- Author  :SQL77(只为思齐老)
    -- Date    :2010-04-17 19:49:43
    -- Version:
    --      Microsoft SQL Server 2005 - 9.00.1399.06 (Intel X86) 
    -- Oct 14 2005 00:33:37 
    -- Copyright (c) 1988-2005 Microsoft Corporation
    -- Developer Edition on Windows NT 5.1 (Build 2600: Service Pack 3)
    --
    ----------------------------------------------------------------
    --> 测试数据:#tb
    if object_id('tempdb.dbo.#tb') is not null drop table #tb
    go 
    create table #tb([Code] varchar(6),[CreateTime] datetime,[Price] numeric(3,2),[Status] int)
    insert #tb
    select '031002','2008-10-17 15:00:15',3.58,1 union all
    select '031002','2008-10-17 15:00:16',5.53,1 union all
    select '031002','2008-10-17 15:00:17',4.54,1 union all
    select '031002','2008-10-17 15:00:18',4.44,1 union all
    select '031002','2008-10-17 15:00:19',4.34,1 union all
    select '031002','2008-10-17 15:00:20',4.24,1 union all
    select '031012','2008-10-17 15:00:15',3.58,0 union all
    select '031012','2008-10-17 15:00:16',5.53,0 union all
    select '031012','2008-10-17 15:00:17',4.54,0 union all
    select '031012','2008-10-17 15:00:18',4.44,0 union all
    select '031012','2008-10-17 15:00:19',4.34,1 union all
    select '031012','2008-10-17 15:00:20',4.24,1 union all
    select '031012','2008-10-17 15:00:18',3.51,0 union all
    select '031012','2008-10-17 15:00:19',3.52,0 union all
    select '031012','2008-10-17 15:00:20',2.50,0 union all
    select '031012','2008-10-17 15:00:21',3.51,0 union all
    select '031012','2008-10-17 15:00:22',3.52,0 union all
    select '031012','2008-10-17 15:00:23',2.50,0 union all
    select '031012','2008-10-17 15:00:24',3.51,0 union all
    select '031012','2008-10-17 15:00:25',3.52,0 union all
    select '031012','2008-10-17 15:00:26',2.50,0 union all
    select '031033','2008-10-17 15:00:21',4.53,0 union all
    select '031033','2008-10-17 15:00:22',3.55,0 union all
    select '031033','2008-10-17 15:00:23',3.55,0 union all
    select '031033','2008-10-17 15:00:24',5.57,0 union all
    select '031033','2008-10-17 15:00:25',5.57,1 union all
    select '031033','2008-10-17 15:00:26',5.57,1 union all
    select '031033','2008-10-17 15:00:27',5.57,1 union all
    select '031033','2008-10-17 15:00:28',5.57,1
    --------------开始查询--------------------------select * from #tb t 
    where exists(select 1 from #tb where [Code]=t.[Code] and [Status]<>t.[Status])
    and [Status]=1
    and not exists(select 1 from #tb where [Code]=t.[Code] and [Status]=1 and createtime<t.createtime)
    ----------------结果----------------------------
    /* (29 行受影响)
    Code   CreateTime              Price                                   Status
    ------ ----------------------- --------------------------------------- -----------
    031012 2008-10-17 15:00:19.000 4.34                                    1
    031033 2008-10-17 15:00:25.000 5.57                                    1(2 行受影响)
    */
      

  2.   

    --> 测试数据:[tb]
    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb]([Code] varchar(6),[CreateTime] datetime,[Price] numeric(3,2),[Status] int)
    insert [tb]
    select '031002','2008-10-17 15:00:15',3.58,1 union all
    select '031002','2008-10-17 15:00:16',5.53,1 union all
    select '031002','2008-10-17 15:00:17',4.54,1 union all
    select '031002','2008-10-17 15:00:18',4.44,1 union all
    select '031002','2008-10-17 15:00:19',4.34,1 union all
    select '031002','2008-10-17 15:00:20',4.24,1 union all
    select '031012','2008-10-17 15:00:15',3.58,0 union all
    select '031012','2008-10-17 15:00:16',5.53,0 union all
    select '031012','2008-10-17 15:00:17',4.54,0 union all
    select '031012','2008-10-17 15:00:18',4.44,0 union all
    select '031012','2008-10-17 15:00:19',4.34,1 union all
    select '031012','2008-10-17 15:00:20',4.24,1 union all
    select '031012','2008-10-17 15:00:18',3.51,0 union all
    select '031012','2008-10-17 15:00:19',3.52,0 union all
    select '031012','2008-10-17 15:00:20',2.50,0 union all
    select '031012','2008-10-17 15:00:21',3.51,0 union all
    select '031012','2008-10-17 15:00:22',3.52,0 union all
    select '031012','2008-10-17 15:00:23',2.50,0 union all
    select '031012','2008-10-17 15:00:24',3.51,0 union all
    select '031012','2008-10-17 15:00:25',3.52,0 union all
    select '031012','2008-10-17 15:00:26',2.50,0 union all
    select '031033','2008-10-17 15:00:21',4.53,0 union all
    select '031033','2008-10-17 15:00:22',3.55,0 union all
    select '031033','2008-10-17 15:00:23',3.55,0 union all
    select '031033','2008-10-17 15:00:24',5.57,0 union all
    select '031033','2008-10-17 15:00:25',5.57,1 union all
    select '031033','2008-10-17 15:00:26',5.57,1 union all
    select '031033','2008-10-17 15:00:27',5.57,1 union all
    select '031033','2008-10-17 15:00:28',5.57,1--------------------------------查询开始------------------------------select * from tb a where
    exists(select 1 from tb where Code=a.Code and Status<>a.Status)
    and
    not exists(select 1 from tb where Code=a.Code and CreateTime<a.CreateTime and Status=1)
    and  Status=1
    /*
    Code   CreateTime              Price                                   Status
    ------ ----------------------- --------------------------------------- -----------
    031012 2008-10-17 15:00:19.000 4.34                                    1
    031033 2008-10-17 15:00:25.000 5.57                                    1(2 行受影响)
    */
      

  3.   


    /*------------------------------------------------------------------
    --  Author : htl258(Tony)
    --  Date   : 2010-04-17 19:56:07
    --  Version: Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86) 
    Jul  9 2008 14:43:34 
    Copyright (c) 1988-2008 Microsoft Corporation
    Developer Edition on Windows NT 5.1 <X86> (Build 2600: Service Pack 3)------------------------------------------------------------------*/
    --> 生成测试数据表:tbIF OBJECT_ID('[tb]') IS NOT NULL
    DROP TABLE [tb]
    GO
    CREATE TABLE [tb]([Code] NVARCHAR(10),[CreateTime] DATETIME,[Price] DECIMAL(18,2),[Status] NVARCHAR(10))
    INSERT [tb]
    SELECT '031002',N'2008-10-17 15:00:15',3.58,'1' UNION ALL
    SELECT '031002',N'2008-10-17 15:00:16',5.53,'1' UNION ALL
    SELECT '031002',N'2008-10-17 15:00:17',4.54,'1' UNION ALL
    SELECT '031002',N'2008-10-17 15:00:18',4.44,'1' UNION ALL
    SELECT '031002',N'2008-10-17 15:00:19',4.34,'1' UNION ALL
    SELECT '031002',N'2008-10-17 15:00:20',4.24,'1' UNION ALL
    SELECT '031012',N'2008-10-17 15:00:15',3.58,'0' UNION ALL
    SELECT '031012',N'2008-10-17 15:00:16',5.53,'0' UNION ALL
    SELECT '031012',N'2008-10-17 15:00:17',4.54,'0' UNION ALL
    SELECT '031012',N'2008-10-17 15:00:18',4.44,'0' UNION ALL
    SELECT '031012',N'2008-10-17 15:00:19',4.34,'1' UNION ALL
    SELECT '031012',N'2008-10-17 15:00:20',4.24,'1' UNION ALL
    SELECT '031012',N'2008-10-17 15:00:18',3.51,'0' UNION ALL
    SELECT '031012',N'2008-10-17 15:00:19',3.52,'0' UNION ALL
    SELECT '031012',N'2008-10-17 15:00:20',2.50,'0' UNION ALL
    SELECT '031012',N'2008-10-17 15:00:21',3.51,'0' UNION ALL
    SELECT '031012',N'2008-10-17 15:00:22',3.52,'0' UNION ALL
    SELECT '031012',N'2008-10-17 15:00:23',2.50,'0' UNION ALL
    SELECT '031012',N'2008-10-17 15:00:24',3.51,'0' UNION ALL
    SELECT '031012',N'2008-10-17 15:00:25',3.52,'0' UNION ALL
    SELECT '031012',N'2008-10-17 15:00:26',2.50,'0' UNION ALL
    SELECT '031033',N'2008-10-17 15:00:21',4.53,'0' UNION ALL
    SELECT '031033',N'2008-10-17 15:00:22',3.55,'0' UNION ALL
    SELECT '031033',N'2008-10-17 15:00:23',3.55,'0' UNION ALL
    SELECT '031033',N'2008-10-17 15:00:24',5.57,'0' UNION ALL
    SELECT '031033',N'2008-10-17 15:00:25',5.57,'1' UNION ALL
    SELECT '031033',N'2008-10-17 15:00:26',5.57,'1' UNION ALL
    SELECT '031033',N'2008-10-17 15:00:27',5.57,'1' UNION ALL
    SELECT '031033',N'2008-10-17 15:00:28',5.57,'1'
    GO
    --SELECT * FROM [tb]-->SQL查询如下:
    select * from tb t 
    where exists(select * from tb a where Code=t.Code and Status<>t.Status)
    and CreateTime=(select top 1 CreateTime from tb where Code=t.Code and Status=1 order by CreateTime)
    and Status=1
    /*
    Code       CreateTime              Price                                   Status
    ---------- ----------------------- --------------------------------------- ----------
    031012     2008-10-17 15:00:19.000 4.34                                    1
    031033     2008-10-17 15:00:25.000 5.57                                    1(2 行受影响)
    */
      

  4.   

    ----------------------------------------------------------------
    -- Author  :fredrickhu(小F,向高手学习)
    -- Date    :2010-04-17 20:07:40
    -- Version:
    --      Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86) 
    -- Nov 24 2008 13:01:59 
    -- Copyright (c) 1988-2005 Microsoft Corporation
    -- Developer Edition on Windows NT 5.1 (Build 2600: Service Pack 3)
    --
    ----------------------------------------------------------------
    --> 测试数据:[tb]
    if object_id('[tb]') is not null drop table [tb]
    go 
    create table [tb]([Code] varchar(6),[CreateTime] datetime,[Price] numeric(3,2),[Status] int)
    insert [tb]
    select '031002','2008-10-17 15:00:15',3.58,1 union all
    select '031002','2008-10-17 15:00:16',5.53,1 union all
    select '031002','2008-10-17 15:00:17',4.54,1 union all
    select '031002','2008-10-17 15:00:18',4.44,1 union all
    select '031002','2008-10-17 15:00:19',4.34,1 union all
    select '031002','2008-10-17 15:00:20',4.24,1 union all
    select '031012','2008-10-17 15:00:15',3.58,0 union all
    select '031012','2008-10-17 15:00:16',5.53,0 union all
    select '031012','2008-10-17 15:00:17',4.54,0 union all
    select '031012','2008-10-17 15:00:18',4.44,0 union all
    select '031012','2008-10-17 15:00:19',4.34,1 union all
    select '031012','2008-10-17 15:00:20',4.24,1 union all
    select '031012','2008-10-17 15:00:18',3.51,0 union all
    select '031012','2008-10-17 15:00:19',3.52,0 union all
    select '031012','2008-10-17 15:00:20',2.50,0 union all
    select '031012','2008-10-17 15:00:21',3.51,0 union all
    select '031012','2008-10-17 15:00:22',3.52,0 union all
    select '031012','2008-10-17 15:00:23',2.50,0 union all
    select '031012','2008-10-17 15:00:24',3.51,0 union all
    select '031012','2008-10-17 15:00:25',3.52,0 union all
    select '031012','2008-10-17 15:00:26',2.50,0 union all
    select '031033','2008-10-17 15:00:21',4.53,0 union all
    select '031033','2008-10-17 15:00:22',3.55,0 union all
    select '031033','2008-10-17 15:00:23',3.55,0 union all
    select '031033','2008-10-17 15:00:24',5.57,0 union all
    select '031033','2008-10-17 15:00:25',5.57,1 union all
    select '031033','2008-10-17 15:00:26',5.57,1 union all
    select '031033','2008-10-17 15:00:27',5.57,1 union all
    select '031033','2008-10-17 15:00:28',5.57,1
    --------------开始查询--------------------------
    select
     * 
    from
     tb t 
    where
     exists(select 1 from tb where [Code]=t.[Code] and [Status]<>t.[Status])
    and 
     [Status]=1
    and
     createtime=(select max(createtime) from tb where [Code]=t.[Code] and [Status]=1)----------------结果----------------------------
    /* Code   CreateTime              Price                                   Status
    ------ ----------------------- --------------------------------------- -----------
    031033 2008-10-17 15:00:28.000 5.57                                    1
    031012 2008-10-17 15:00:20.000 4.24                                    1(2 行受影响)
    */
      

  5.   

    各位,忘记说一个问题了,就是如果该code第一天就是1而前面没有0的话就不能算,现在按照你们的写法都把第一条Status为1前面没有Status为0的结果查询出来了
      

  6.   

    /*------------------------------------------------------------------
    --  Author : htl258(Tony)
    --  Date   : 2010-04-17 19:56:07
    --  Version: Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86) 
        Jul  9 2008 14:43:34 
        Copyright (c) 1988-2008 Microsoft Corporation
        Developer Edition on Windows NT 5.1 <X86> (Build 2600: Service Pack 3)------------------------------------------------------------------*/
    --> 生成测试数据表:tbIF OBJECT_ID('[tb]') IS NOT NULL
        DROP TABLE [tb]
    GO
    CREATE TABLE [tb]([Code] NVARCHAR(10),[CreateTime] DATETIME,[Price] DECIMAL(18,2),[Status] NVARCHAR(10))
    INSERT [tb]
    SELECT '031002',N'2008-10-17 15:00:15',3.58,'1' UNION ALL
    SELECT '031002',N'2008-10-17 15:00:16',5.53,'1' UNION ALL
    SELECT '031002',N'2008-10-17 15:00:17',4.54,'1' UNION ALL
    SELECT '031002',N'2008-10-17 15:00:18',4.44,'1' UNION ALL
    SELECT '031002',N'2008-10-17 15:00:19',4.34,'1' UNION ALL
    SELECT '031002',N'2008-10-17 15:00:20',4.24,'1' UNION ALL
    SELECT '031012',N'2008-10-17 15:00:15',3.58,'0' UNION ALL
    SELECT '031012',N'2008-10-17 15:00:16',5.53,'0' UNION ALL
    SELECT '031012',N'2008-10-17 15:00:17',4.54,'0' UNION ALL
    SELECT '031012',N'2008-10-17 15:00:18',4.44,'0' UNION ALL
    SELECT '031012',N'2008-10-17 15:00:19',4.34,'1' UNION ALL
    SELECT '031012',N'2008-10-17 15:00:20',4.24,'1' UNION ALL
    SELECT '031012',N'2008-10-17 15:00:18',3.51,'0' UNION ALL
    SELECT '031012',N'2008-10-17 15:00:19',3.52,'0' UNION ALL
    SELECT '031012',N'2008-10-17 15:00:20',2.50,'0' UNION ALL
    SELECT '031012',N'2008-10-17 15:00:21',3.51,'0' UNION ALL
    SELECT '031012',N'2008-10-17 15:00:22',3.52,'0' UNION ALL
    SELECT '031012',N'2008-10-17 15:00:23',2.50,'0' UNION ALL
    SELECT '031012',N'2008-10-17 15:00:24',3.51,'0' UNION ALL
    SELECT '031012',N'2008-10-17 15:00:25',3.52,'0' UNION ALL
    SELECT '031012',N'2008-10-17 15:00:26',2.50,'0' UNION ALL
    SELECT '031033',N'2008-10-17 15:00:21',4.53,'0' UNION ALL
    SELECT '031033',N'2008-10-17 15:00:22',3.55,'0' UNION ALL
    SELECT '031033',N'2008-10-17 15:00:23',3.55,'0' UNION ALL
    SELECT '031033',N'2008-10-17 15:00:24',5.57,'0' UNION ALL
    SELECT '031033',N'2008-10-17 15:00:25',5.57,'1' UNION ALL
    SELECT '031033',N'2008-10-17 15:00:26',5.57,'1' UNION ALL
    SELECT '031033',N'2008-10-17 15:00:27',5.57,'1' UNION ALL
    SELECT '031033',N'2008-10-17 15:00:28',5.57,'1' UNION ALL
    SELECT '031034',N'2008-10-17 15:00:26',5.57,'1' UNION ALL
    SELECT '031034',N'2008-10-17 15:00:27',5.57,'0' UNION ALL
    SELECT '031034',N'2008-10-17 15:00:28',5.57,'1'
    GO
    --SELECT * FROM [tb]-->SQL查询如下:
    select * from tb t 
    where exists(select * from tb a where Code=t.Code and Status<>t.Status)
        and CreateTime=(select min(CreateTime) from tb a where Code=t.Code and Status=1 
         and CreateTime>(select top 1 CreateTime from tb where Code=a.Code and Status=0 order by CreateTime))
        and Status=1
    /*
    Code       CreateTime              Price                                   Status
    ---------- ----------------------- --------------------------------------- ----------
    031012     2008-10-17 15:00:19.000 4.34                                    1
    031033     2008-10-17 15:00:25.000 5.57                                    1
    031034     2008-10-17 15:00:28.000 5.57                                    1(3 行受影响)
    */
      

  7.   

    /*------------------------------------------------------------------
    --  Author : htl258(Tony)
    --  Date   : 2010-04-17 19:56:07
    --  Version: Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86) 
        Jul  9 2008 14:43:34 
        Copyright (c) 1988-2008 Microsoft Corporation
        Developer Edition on Windows NT 5.1 <X86> (Build 2600: Service Pack 3)------------------------------------------------------------------*/
    --> 生成测试数据表:tbIF OBJECT_ID('[tb]') IS NOT NULL
        DROP TABLE [tb]
    GO
    CREATE TABLE [tb]([Code] NVARCHAR(10),[CreateTime] DATETIME,[Price] DECIMAL(18,2),[Status] NVARCHAR(10))
    INSERT [tb]
    SELECT '031002',N'2008-10-17 15:00:15',3.58,'1' UNION ALL
    SELECT '031002',N'2008-10-17 15:00:16',5.53,'1' UNION ALL
    SELECT '031002',N'2008-10-17 15:00:17',4.54,'1' UNION ALL
    SELECT '031002',N'2008-10-17 15:00:18',4.44,'1' UNION ALL
    SELECT '031002',N'2008-10-17 15:00:19',4.34,'1' UNION ALL
    SELECT '031002',N'2008-10-17 15:00:20',4.24,'1' UNION ALL
    SELECT '031012',N'2008-10-17 15:00:15',3.58,'0' UNION ALL
    SELECT '031012',N'2008-10-17 15:00:16',5.53,'0' UNION ALL
    SELECT '031012',N'2008-10-17 15:00:17',4.54,'0' UNION ALL
    SELECT '031012',N'2008-10-17 15:00:18',4.44,'0' UNION ALL
    SELECT '031012',N'2008-10-17 15:00:19',4.34,'1' UNION ALL
    SELECT '031012',N'2008-10-17 15:00:20',4.24,'1' UNION ALL
    SELECT '031012',N'2008-10-17 15:00:18',3.51,'0' UNION ALL
    SELECT '031012',N'2008-10-17 15:00:19',3.52,'0' UNION ALL
    SELECT '031012',N'2008-10-17 15:00:20',2.50,'0' UNION ALL
    SELECT '031012',N'2008-10-17 15:00:21',3.51,'0' UNION ALL
    SELECT '031012',N'2008-10-17 15:00:22',3.52,'0' UNION ALL
    SELECT '031012',N'2008-10-17 15:00:23',2.50,'0' UNION ALL
    SELECT '031012',N'2008-10-17 15:00:24',3.51,'0' UNION ALL
    SELECT '031012',N'2008-10-17 15:00:25',3.52,'0' UNION ALL
    SELECT '031012',N'2008-10-17 15:00:26',2.50,'0' UNION ALL
    SELECT '031033',N'2008-10-17 15:00:21',4.53,'0' UNION ALL
    SELECT '031033',N'2008-10-17 15:00:22',3.55,'0' UNION ALL
    SELECT '031033',N'2008-10-17 15:00:23',3.55,'0' UNION ALL
    SELECT '031033',N'2008-10-17 15:00:24',5.57,'0' UNION ALL
    SELECT '031033',N'2008-10-17 15:00:25',5.57,'1' UNION ALL
    SELECT '031033',N'2008-10-17 15:00:26',5.57,'1' UNION ALL
    SELECT '031033',N'2008-10-17 15:00:27',5.57,'1' UNION ALL
    SELECT '031033',N'2008-10-17 15:00:28',5.57,'1' UNION ALL
    SELECT '031034',N'2008-10-17 15:00:26',5.57,'1' UNION ALL
    SELECT '031034',N'2008-10-17 15:00:27',5.57,'0' UNION ALL
    SELECT '031034',N'2008-10-17 15:00:28',5.57,'1'
    GO
    --SELECT * FROM [tb]-->SQL查询如下:
    select * from tb t 
    where exists(select * from tb where Code=t.Code and Status<>t.Status)
        and CreateTime=(select min(CreateTime) from tb a where Code=t.Code and Status=1 
         and CreateTime>(select min(CreateTime) from tb where Code=a.Code and Status=0))
        and Status=1
    /*
    Code       CreateTime              Price                                   Status
    ---------- ----------------------- --------------------------------------- ----------
    031012     2008-10-17 15:00:19.000 4.34                                    1
    031033     2008-10-17 15:00:25.000 5.57                                    1
    031034     2008-10-17 15:00:28.000 5.57                                    1(3 行受影响)
    */这样会快点
      

  8.   


    --try--> 测试数据:[tb]
    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb]([Code] varchar(6),[CreateTime] datetime,[Price] numeric(3,2),[Status] int)
    insert [tb]
    select '031002','2008-10-17 15:00:15',3.58,1 union all
    select '031002','2008-10-17 15:00:16',5.53,1 union all
    select '031002','2008-10-17 15:00:17',4.54,1 union all
    select '031002','2008-10-17 15:00:18',4.44,1 union all
    select '031002','2008-10-17 15:00:19',4.34,1 union all
    select '031002','2008-10-17 15:00:20',4.24,1 union all
    select '031012','2008-10-17 15:00:15',3.58,0 union all
    select '031012','2008-10-17 15:00:16',5.53,0 union all
    select '031012','2008-10-17 15:00:17',4.54,0 union all
    select '031012','2008-10-17 15:00:18',4.44,0 union all
    select '031012','2008-10-17 15:00:19',4.34,1 union all
    select '031012','2008-10-17 15:00:20',4.24,1 union all
    select '031012','2008-10-17 15:00:18',3.51,0 union all
    select '031012','2008-10-17 15:00:19',3.52,0 union all
    select '031012','2008-10-17 15:00:20',2.50,0 union all
    select '031012','2008-10-17 15:00:21',3.51,0 union all
    select '031012','2008-10-17 15:00:22',3.52,0 union all
    select '031012','2008-10-17 15:00:23',2.50,0 union all
    select '031012','2008-10-17 15:00:24',3.51,0 union all
    select '031012','2008-10-17 15:00:25',3.52,0 union all
    select '031012','2008-10-17 15:00:26',2.50,0 union all
    select '031033','2008-10-17 15:00:21',4.53,0 union all
    select '031033','2008-10-17 15:00:22',3.55,0 union all
    select '031033','2008-10-17 15:00:23',3.55,0 union all
    select '031033','2008-10-17 15:00:24',5.57,0 union all
    select '031033','2008-10-17 15:00:25',5.57,1 union all
    select '031033','2008-10-17 15:00:26',5.57,1 union all
    select '031033','2008-10-17 15:00:27',5.57,1 union all
    select '031033','2008-10-17 15:00:28',5.57,1 union all
    select '121033','2008-10-17 15:00:31',5.57,1 union all
    select '121033','2008-10-17 15:00:33',5.57,0 --------------------------------查询开始------------------------------select * from tb a where
     exists(select 1 from tb where Code=a.Code and Status<>a.Status)
    and  not exists(select 1 from tb where Code=a.Code and CreateTime<a.CreateTime and Status=1)
    and  exists(select 1 from tb where Code=a.Code and CreateTime>a.CreateTime and Status=1)
    and  Status=1
    /*
    Code   CreateTime              Price                                   Status
    ------ ----------------------- --------------------------------------- -----------
    031012 2008-10-17 15:00:19.000 4.34                                    1
    031033 2008-10-17 15:00:25.000 5.57                                    1(2 行受影响)
    */
      

  9.   

    ----------------------------------------------------------------
    -- Author  :SQL77(只为思齐老)
    -- Date    :2010-04-17 19:49:43
    -- Version:
    --      Microsoft SQL Server 2005 - 9.00.1399.06 (Intel X86) 
    -- Oct 14 2005 00:33:37 
    -- Copyright (c) 1988-2005 Microsoft Corporation
    -- Developer Edition on Windows NT 5.1 (Build 2600: Service Pack 3)
    --
    ----------------------------------------------------------------
    --> 测试数据:#tb
    if object_id('tempdb.dbo.#tb') is not null drop table #tb
    go 
    create table #tb([Code] varchar(6),[CreateTime] datetime,[Price] numeric(3,2),[Status] int)
    insert #tb
    select '031002','2008-10-17 15:00:15',3.58,1 union all
    select '031002','2008-10-17 15:00:16',5.53,1 union all
    select '031002','2008-10-17 15:00:17',4.54,1 union all
    select '031002','2008-10-17 15:00:18',4.44,1 union all
    select '031002','2008-10-17 15:00:19',4.34,1 union all
    select '031002','2008-10-17 15:00:20',4.24,1 union all
    select '031012','2008-10-17 15:00:15',3.58,0 union all
    select '031012','2008-10-17 15:00:16',5.53,0 union all
    select '031012','2008-10-17 15:00:17',4.54,0 union all
    select '031012','2008-10-17 15:00:18',4.44,0 union all
    select '031012','2008-10-17 15:00:19',4.34,1 union all
    select '031012','2008-10-17 15:00:20',4.24,1 union all
    select '031012','2008-10-17 15:00:18',3.51,0 union all
    select '031012','2008-10-17 15:00:19',3.52,0 union all
    select '031012','2008-10-17 15:00:20',2.50,0 union all
    select '031012','2008-10-17 15:00:21',3.51,0 union all
    select '031012','2008-10-17 15:00:22',3.52,0 union all
    select '031012','2008-10-17 15:00:23',2.50,0 union all
    select '031012','2008-10-17 15:00:24',3.51,0 union all
    select '031012','2008-10-17 15:00:25',3.52,0 union all
    select '031012','2008-10-17 15:00:26',2.50,0 union all
    select '031033','2008-10-17 15:00:21',4.53,0 union all
    select '031033','2008-10-17 15:00:22',3.55,0 union all
    select '031033','2008-10-17 15:00:23',3.55,0 union all
    select '031033','2008-10-17 15:00:24',5.57,0 union all
    select '031033','2008-10-01 15:00:25',5.57,1 union all
    select '031033','2008-10-17 15:00:25',5.57,1 union all
    select '031033','2008-10-17 15:00:26',5.57,1 union all
    select '031033','2008-10-17 15:00:27',5.57,1 union all
    select '031033','2008-10-17 15:00:28',5.57,1
    --------------开始查询--------------------------select * from #tb t 
    where exists(select 1 from #tb where [Code]=t.[Code] and [Status]<>t.[Status]  )
    and not exists(select 1 from #tb where [Code]=t.[Code] and [Status]=1 and createtime<t.createtime)
    and [Status]=1 
    and not exists(select 1 from #tb where [Code]=t.[Code] and (day(createtime)=1 and [Status]=1 ))
    ----------------结果----------------------------
    /* (30 行受影响)
    (1 行受影响)
    Code   CreateTime              Price                                   Status
    ------ ----------------------- --------------------------------------- -----------
    031012 2008-10-17 15:00:19.000 4.34                                    1(1 行受影响)(1 行受影响)*/................记得把条件说清楚
      

  10.   

    是这样吗?
    --> 测试数据:[tb]
    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb]([Code] varchar(6),[CreateTime] datetime,[Price] numeric(3,2),[Status] int)
    insert [tb]
    select '031002','2008-10-17 15:00:15',3.58,1 union all
    select '031002','2008-10-17 15:00:16',5.53,1 union all
    select '031002','2008-10-17 15:00:17',4.54,1 union all
    select '031002','2008-10-17 15:00:18',4.44,1 union all
    select '031002','2008-10-17 15:00:19',4.34,1 union all
    select '031002','2008-10-17 15:00:20',4.24,1 union all
    select '031012','2008-10-17 15:00:15',3.58,0 union all
    select '031012','2008-10-17 15:00:16',5.53,0 union all
    select '031012','2008-10-17 15:00:17',4.54,0 union all
    select '031012','2008-10-17 15:00:18',4.44,0 union all
    select '031012','2008-10-17 15:00:19',4.34,1 union all
    select '031012','2008-10-17 15:00:20',4.24,1 union all
    select '031012','2008-10-17 15:00:18',3.51,0 union all
    select '031012','2008-10-17 15:00:19',3.52,0 union all
    select '031012','2008-10-17 15:00:20',2.50,0 union all
    select '031012','2008-10-17 15:00:21',3.51,0 union all
    select '031012','2008-10-17 15:00:22',3.52,0 union all
    select '031012','2008-10-17 15:00:23',2.50,0 union all
    select '031012','2008-10-17 15:00:24',3.51,0 union all
    select '031012','2008-10-17 15:00:25',3.52,0 union all
    select '031012','2008-10-17 15:00:26',2.50,0 union all
    select '031033','2008-10-17 15:00:21',4.53,0 union all
    select '031033','2008-10-17 15:00:22',3.55,0 union all
    select '031033','2008-10-17 15:00:23',3.55,0 union all
    select '031033','2008-10-17 15:00:24',5.57,0 union all
    select '031033','2008-10-17 15:00:25',5.57,1 union all
    select '031033','2008-10-17 15:00:26',5.57,1 union all
    select '031033','2008-10-17 15:00:27',5.57,1 union all
    select '031033','2008-10-17 15:00:28',5.57,1 union all
    --新加的测试数据
    select '121033','2008-10-17 15:00:31',5.17,1 union all
    select '121033','2008-10-17 15:00:33',5.27,0 union all
    select '123033','2008-10-17 15:00:45',5.51,1 union all
    select '123033','2008-10-17 15:00:46',5.52,0 union all
    select '123033','2008-10-17 15:00:48',5.53,1 --------------------------------查询开始------------------------------select * from tb a where
     exists(select 1 from tb where Code=a.Code and Status<>a.Status)
    and  not exists(select 1 from tb where Code=a.Code and CreateTime<a.CreateTime and Status=1)
    and  Status=1
    and  not exists(select 1 from tb where Code=a.Code and CreateTime>a.CreateTime and Status=0)/*
    Code   CreateTime              Price                                   Status
    ------ ----------------------- --------------------------------------- -----------
    031033 2008-10-17 15:00:25.000 5.57                                    1(1 行受影响)                                1
    */
      

  11.   

    select '121033','2008-10-17 15:00:31',5.17,1 union all
    select '121033','2008-10-17 15:00:33',5.27,0 union all
    select '123033','2008-10-17 15:00:45',5.51,1 union all
    select '123033','2008-10-17 15:00:46',5.52,0 union all
    select '123033','2008-10-17 15:00:48',5.53,1 
    就那这写数据来书你是要
    select '123033','2008-10-17 15:00:45',5.51,1
    还是select '123033','2008-10-17 15:00:48',5.53,1 
    还是都不要
      

  12.   

    ----------------------------------------------------------------
    -- Author  :fredrickhu(小F,向高手学习)
    -- Date    :2010-04-17 20:07:40
    -- Version:
    --      Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86) 
    -- Nov 24 2008 13:01:59 
    -- Copyright (c) 1988-2005 Microsoft Corporation
    -- Developer Edition on Windows NT 5.1 (Build 2600: Service Pack 3)
    --
    ----------------------------------------------------------------
    --> 测试数据:[tb]
    if object_id('[tb]') is not null drop table [tb]
    go 
    create table [tb]([Code] varchar(6),[CreateTime] datetime,[Price] numeric(3,2),[Status] int)
    insert [tb]
    select '031002','2008-10-17 15:00:15',3.58,1 union all
    select '031002','2008-10-17 15:00:16',5.53,1 union all
    select '031002','2008-10-17 15:00:17',4.54,1 union all
    select '031002','2008-10-17 15:00:18',4.44,1 union all
    select '031002','2008-10-17 15:00:19',4.34,1 union all
    select '031002','2008-10-17 15:00:20',4.24,1 union all
    select '031012','2008-10-17 15:00:15',3.58,0 union all
    select '031012','2008-10-17 15:00:16',5.53,0 union all
    select '031012','2008-10-17 15:00:17',4.54,0 union all
    select '031012','2008-10-17 15:00:18',4.44,0 union all
    select '031012','2008-10-17 15:00:19',4.34,1 union all
    select '031012','2008-10-17 15:00:20',4.24,1 union all
    select '031012','2008-10-17 15:00:18',3.51,0 union all
    select '031012','2008-10-17 15:00:19',3.52,0 union all
    select '031012','2008-10-17 15:00:20',2.50,0 union all
    select '031012','2008-10-17 15:00:21',3.51,0 union all
    select '031012','2008-10-17 15:00:22',3.52,0 union all
    select '031012','2008-10-17 15:00:23',2.50,0 union all
    select '031012','2008-10-17 15:00:24',3.51,0 union all
    select '031012','2008-10-17 15:00:25',3.52,0 union all
    select '031012','2008-10-17 15:00:26',2.50,0 union all
    select '031033','2008-10-17 15:00:21',4.53,0 union all
    select '031033','2008-10-17 15:00:22',3.55,0 union all
    select '031033','2008-10-17 15:00:23',3.55,0 union all
    select '031033','2008-10-17 15:00:24',5.57,0 union all
    select '031033','2008-10-17 15:00:25',5.57,1 union all
    select '031033','2008-10-17 15:00:26',5.57,1 union all
    select '031033','2008-10-17 15:00:27',5.57,1 union all
    select '031033','2008-10-17 15:00:28',5.57,1
    --------------开始查询--------------------------
    select
     * 
    from
     tb t 
    where
     exists(select 1 from tb where [Code]=t.[Code] and [Status]<>t.[Status])
    and 
     [Status]=1
    and
     createtime=(select max(createtime) from tb where [Code]=t.[Code] and [Status]=1)
    and
     not exists(select 1 from tb where Code=t.Code and CreateTime>t.CreateTime and [Status]=0)
    ----------------结果----------------------------
    /* Code   CreateTime              Price                                   Status
    ------ ----------------------- --------------------------------------- -----------
    031033 2008-10-17 15:00:28.000 5.57                                    1(1 行受影响)*/
      

  13.   

    --此题代码再简化一下:/*------------------------------------------------------------------
    --  Author : htl258(Tony)
    --  Date   : 2010-04-17 19:56:07
    --  Version: Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86) 
        Jul  9 2008 14:43:34 
        Copyright (c) 1988-2008 Microsoft Corporation
        Developer Edition on Windows NT 5.1 <X86> (Build 2600: Service Pack 3)------------------------------------------------------------------*/
    --> 生成测试数据表:tbIF OBJECT_ID('[tb]') IS NOT NULL
        DROP TABLE [tb]
    GO
    CREATE TABLE [tb]([Code] NVARCHAR(10),[CreateTime] DATETIME,[Price] DECIMAL(18,2),[Status] NVARCHAR(10))
    INSERT [tb]
    SELECT '031002',N'2008-10-17 15:00:15',3.58,'1' UNION ALL
    SELECT '031002',N'2008-10-17 15:00:16',5.53,'1' UNION ALL
    SELECT '031002',N'2008-10-17 15:00:17',4.54,'1' UNION ALL
    SELECT '031002',N'2008-10-17 15:00:18',4.44,'1' UNION ALL
    SELECT '031002',N'2008-10-17 15:00:19',4.34,'1' UNION ALL
    SELECT '031002',N'2008-10-17 15:00:20',4.24,'1' UNION ALL
    SELECT '031012',N'2008-10-17 15:00:15',3.58,'0' UNION ALL
    SELECT '031012',N'2008-10-17 15:00:16',5.53,'0' UNION ALL
    SELECT '031012',N'2008-10-17 15:00:17',4.54,'0' UNION ALL
    SELECT '031012',N'2008-10-17 15:00:18',4.44,'0' UNION ALL
    SELECT '031012',N'2008-10-17 15:00:19',4.34,'1' UNION ALL
    SELECT '031012',N'2008-10-17 15:00:20',4.24,'1' UNION ALL
    SELECT '031012',N'2008-10-17 15:00:18',3.51,'0' UNION ALL
    SELECT '031012',N'2008-10-17 15:00:19',3.52,'0' UNION ALL
    SELECT '031012',N'2008-10-17 15:00:20',2.50,'0' UNION ALL
    SELECT '031012',N'2008-10-17 15:00:21',3.51,'0' UNION ALL
    SELECT '031012',N'2008-10-17 15:00:22',3.52,'0' UNION ALL
    SELECT '031012',N'2008-10-17 15:00:23',2.50,'0' UNION ALL
    SELECT '031012',N'2008-10-17 15:00:24',3.51,'0' UNION ALL
    SELECT '031012',N'2008-10-17 15:00:25',3.52,'0' UNION ALL
    SELECT '031012',N'2008-10-17 15:00:26',2.50,'0' UNION ALL
    SELECT '031033',N'2008-10-17 15:00:21',4.53,'0' UNION ALL
    SELECT '031033',N'2008-10-17 15:00:22',3.55,'0' UNION ALL
    SELECT '031033',N'2008-10-17 15:00:23',3.55,'0' UNION ALL
    SELECT '031033',N'2008-10-17 15:00:24',5.57,'0' UNION ALL
    SELECT '031033',N'2008-10-17 15:00:25',5.57,'1' UNION ALL
    SELECT '031033',N'2008-10-17 15:00:26',5.57,'1' UNION ALL
    SELECT '031033',N'2008-10-17 15:00:27',5.57,'1' UNION ALL
    SELECT '031033',N'2008-10-17 15:00:28',5.57,'1' UNION ALL
    --新增记录
    SELECT '031034',N'2008-10-17 15:00:26',5.57,'1' UNION ALL
    SELECT '031034',N'2008-10-17 15:00:27',5.57,'0' UNION ALL
    SELECT '031034',N'2008-10-17 15:00:28',5.57,'1'
    GO
    --SELECT * FROM [tb]-->SQL查询如下:
    select * from tb t 
    where CreateTime=(select min(CreateTime) from tb a where Code=t.Code and Status=1 
            and CreateTime>(select min(CreateTime) from tb where Code=a.Code and Status=0))
        and Status=1
    /*
    Code       CreateTime              Price                                   Status
    ---------- ----------------------- --------------------------------------- ----------
    031012     2008-10-17 15:00:19.000 4.34                                    1
    031033     2008-10-17 15:00:25.000 5.57                                    1
    031034     2008-10-17 15:00:28.000 5.57                                    1(3 行受影响)
    */
      

  14.   

    tony 哥辛苦了,估计楼主早睡了
      

  15.   

    呵呵,看了许多高手的回答
    一时兴趣盎然,也发现一些不完美的地方。比如当031002 ,从0-1,1-0,0-1,不停的切换状态的时候,那这种情况应该选择
    多条031002 在1 的情况。所以也凑一下热闹。
    /*------------------------------------------------------------------
    --  Author : 无名小卒
    --  Date   : 2010-04-18 00:02:04
    --  Version: Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86) 
       
    ------------------------------------------------------------------*/
    --> 生成测试数据表:tb
    IF OBJECT_ID('[tb]') IS NOT NULL
        DROP TABLE [tb]
    GO
    CREATE TABLE [tb]([Code] NVARCHAR(10),[CreateTime] DATETIME,[Price] DECIMAL(18,2),[Status] NVARCHAR(10))
    INSERT [tb]
    SELECT '031002',N'2008-10-17 15:00:15',3.58,'1' UNION ALL
    SELECT '031002',N'2008-10-17 15:00:16',5.53,'1' UNION ALL
    SELECT '031002',N'2008-10-17 15:00:17',4.54,'1' UNION ALL
    SELECT '031002',N'2008-10-17 15:00:18',4.44,'1' UNION ALL
    SELECT '031002',N'2008-10-17 15:00:19',4.34,'1' UNION ALL
    SELECT '031002',N'2008-10-17 15:00:20',4.24,'1' UNION ALL
    SELECT '031012',N'2008-10-17 15:00:15',3.58,'0' UNION ALL
    SELECT '031012',N'2008-10-17 15:00:16',5.53,'0' UNION ALL
    SELECT '031012',N'2008-10-17 15:00:17',4.54,'0' UNION ALL
    SELECT '031012',N'2008-10-17 15:00:18',4.44,'0' UNION ALL
    SELECT '031012',N'2008-10-17 15:00:19',4.34,'1' UNION ALL
    SELECT '031012',N'2008-10-17 15:00:20',4.24,'1' UNION ALL
    SELECT '031012',N'2008-10-17 15:00:18',3.51,'0' UNION ALL
    SELECT '031012',N'2008-10-17 15:00:19',3.52,'0' UNION ALL
    SELECT '031012',N'2008-10-17 15:00:20',2.50,'0' UNION ALL
    SELECT '031012',N'2008-10-17 15:00:21',3.51,'0' UNION ALL
    SELECT '031012',N'2008-10-17 15:00:22',3.52,'0' UNION ALL
    SELECT '031012',N'2008-10-17 15:00:23',2.50,'0' UNION ALL
    SELECT '031012',N'2008-10-17 15:00:24',3.51,'0' UNION ALL
    --变换数据 0-1
    SELECT '031012',N'2008-10-17 15:00:25',3.52,'1' UNION ALL
    SELECT '031012',N'2008-10-17 15:00:26',2.50,'1' UNION ALLSELECT '031033',N'2008-10-17 15:00:21',4.53,'0' UNION ALL
    SELECT '031033',N'2008-10-17 15:00:22',3.55,'0' UNION ALL
    SELECT '031033',N'2008-10-17 15:00:23',3.55,'0' UNION ALL
    SELECT '031033',N'2008-10-17 15:00:24',5.57,'0' UNION ALL
    SELECT '031033',N'2008-10-17 15:00:25',5.57,'1' UNION ALL
    SELECT '031033',N'2008-10-17 15:00:26',5.57,'1' UNION ALL
    SELECT '031033',N'2008-10-17 15:00:27',5.57,'1' UNION ALL
    SELECT '031033',N'2008-10-17 15:00:28',5.57,'1' UNION ALL
    --新增记录
    SELECT '031034',N'2008-10-17 15:00:26',5.57,'1' UNION ALL
    SELECT '031034',N'2008-10-17 15:00:27',5.57,'0' UNION ALL
    SELECT '031034',N'2008-10-17 15:00:28',5.57,'1'
    GOwith  wumin as
    (
    select *,ROW_NUMBER() over(order by code ,createtime ) as num from tb t 
    )
    select * from wumin wu
    where 
     exists ( select * from wumin where code=wu.code and wu.num=num+1 and wu.status>status)