有如下表
CREATE TABLE [dbo].[tests](
[tbid] [int] IDENTITY(1,1) NOT NULL,
[a] [bit] NULL,
[name] [varchar](50) NULL
) ON [PRIMARY]
数据样式如下:
查出字段a为Ture的下一条值为False结果,如何得到如下结果:2 False name1
6 False name2
9 False name3谢谢咯~~~~~~~~~~
顺便告诉我下,怎么把表里已经有的数据 导出成sql语句 insert into 那种样式,方便让别人测试问答你的问题。

解决方案 »

  1.   

    select a.* from tests a,tests b
    where b.tbid = a.tbid + 1
    and a.a = 'false'
    and b.a = 'true'
      

  2.   


    奇怪,csdn会篡改回复select a.* from tests a,tests b
    where b.tbid = a.tbid +1
    and a.a = 'false'
    and b.a = 'true'
      

  3.   

    完全晕了,以上都不是我的代码
    select a.* from tests a,tests b
    where b.tbid = a.tbid +1
    and a.a = 'false'
    and b.a = 'true'
      

  4.   

    select * from tb t where tbid=(select min(tbid) from tb where name=t.name)
      

  5.   

    select 
    b.* 
    from table1 as a 
    inner join table1 as b on a.ID=b.ID-1 and a.a=1 and a.a<>b.a and a.Name=b.Name--1為true
      

  6.   

    USE tempdb
    GO
    CREATE TABLE [dbo].[tests](
        [tbid] [int] IDENTITY(1,1) NOT NULL,
        [a] [bit] NULL,
        [name] [varchar](50) NULL
    ) ON [PRIMARY]
    GOINSERT [tests] SELECT 1,'Name1'
    INSERT [tests] SELECT 0,'Name1'
    INSERT [tests] SELECT 0,'Name1'
    INSERT [tests] SELECT 1,'Name2'
    INSERT [tests] SELECT 1,'Name2'
    INSERT [tests] SELECT 0,'Name2'
    INSERT [tests] SELECT 0,'Name2'
    INSERT [tests] SELECT 1,'Name3'
    INSERT [tests] SELECT 0,'Name3'
    INSERT [tests] SELECT 0,'Name3'
    GO
    select b.* 
    from [tests] as a 
    inner join [tests] as b on a.[tbid]=b.[tbid]-1 and a.a=1 and a.a<>b.a and a.Name=b.Name--1為true/*
    tbid a name
    2 0 Name1
    6 0 Name2
    9 0 Name3*/
      

  7.   

    是不是每次为true 的时候name就改变了??如果是的话 可以这样
    ;with f as
    (
    select px=row_number()over(partition by name order by getdate()),* from tb
    )select * from f where px=1 and a='false'
      

  8.   


    CREATE TABLE [dbo].[tests](
        [tbid] [int] IDENTITY(1,1) NOT NULL,
        [a] [bit] NULL,
        [name] [varchar](50) NULL
    ) ON [PRIMARY]
    go
    insert into tests (a,name)
    select 'True','name1' union all
    select 'False','name1' union all
    select 'False','name1' union all
    select 'True','name2' union all
    select 'True','name2' union all
    select 'False','name2' union all
    select 'False','name2' union all
    select 'True','name3' union all
    select 'False','name3' union all
    select 'False','name3'
    go
    select * from tests a where a='False' and exists(select 1 from tests where tbid=a.tbid-1 and a='True')
      

  9.   

    非常谢谢大家的问答,但是上面的所有语句都不能解决如下的问题:
    数据格式:
    得到如下结果:
    2      False     name1 
    6      False     name2
    10     False     name3注意name2里的True并不是连续的,只取第一个True之后的False
      

  10.   


    CREATE TABLE [dbo].[tests](
        [tbid] [int] IDENTITY(1,1) NOT NULL,
        [a] [bit] NULL,
        [name] [varchar](50) NULL
    ) ON [PRIMARY]
    insert into tests select 1,'name1'
    insert into tests select 0,'name1'
    insert into tests select 0,'name1'
    insert into tests select 1,'name2'
    insert into tests select 1,'name2'
    insert into tests select 0,'name2'
    insert into tests select 0,'name2'
    insert into tests select 1,'name3'
    insert into tests select 0,'name3'
    insert into tests select 0,'name3'
    go
    select * from tests a where a=0 and 
    exists(select 1 from tests where tbid=a.tbid-1 and a=1)
    --这个查询语句要在打开表界面中执行才会看到false
    /*
    tbid        a     name
    ----------- ----- --------------------------------------------------
    2           0     name1
    6           0     name2
    9           0     name3(3 行受影响)*/
    go
    drop table tests
      

  11.   


    大哥 要是插入这样的数据 得到的结果就不对了
    insert into tests select 1,'name1'
    insert into tests select 0,'name1'
    insert into tests select 0,'name1'
    insert into tests select 1,'name2'
    insert into tests select 0,'name2'
    insert into tests select 1,'name2'
    insert into tests select 0,'name2'
    insert into tests select 1,'name3'
    insert into tests select 0,'name3'
    insert into tests select 0,'name3'
    这个'name2'的True不是连续的只取第一个True之后的False
      

  12.   

    你这个表可以这样来生成数据:
    select 'insert into tests select '+ltrim(a)+','''+name+'''' from tests
    /*
    insert into tests select 1,'name1'
    insert into tests select 0,'name1'
    insert into tests select 0,'name1'
    insert into tests select 1,'name2'
    insert into tests select 1,'name2'
    insert into tests select 0,'name2'
    insert into tests select 0,'name2'
    insert into tests select 1,'name3'
    insert into tests select 0,'name3'
    insert into tests select 0,'name3'(10 行受影响)*/
      

  13.   


    受教了,请问这个界面是在哪里出现的?不是sql2005 “新建查询” 点出来的那个窗口吗?
      

  14.   

    那就加一个条件:
    CREATE TABLE [dbo].[tests](
        [tbid] [int] IDENTITY(1,1) NOT NULL,
        [a] [bit] NULL,
        [name] [varchar](50) NULL
    ) ON [PRIMARY]
    insert into tests select 1,'name1'
    insert into tests select 0,'name1'
    insert into tests select 0,'name1'
    insert into tests select 1,'name2'
    insert into tests select 0,'name2'
    insert into tests select 1,'name2'
    insert into tests select 0,'name2'
    insert into tests select 1,'name3'
    insert into tests select 0,'name3'
    insert into tests select 0,'name3'
    go
    select * from tests a where a=0 and 
    exists(select 1 from tests where tbid=a.tbid-1 and a=1)
    and not exists(select 1 from tests where name=a.name and a=0 and tbid<a.tbid)
    --这个查询语句要在打开表界面中执行才会看到false
    /*
    tbid        a     name
    ----------- ----- --------------------------------------------------
    2           0     name1
    5           0     name2
    9           0     name3(3 行受影响)*/
    go
    drop table tests
      

  15.   


    右击表-->打开表-->点击工具栏上的"显示SQL窗格",上面可以编辑语句.
      

  16.   


    看不见   如下结果啊?在那个界面可以看到。这样的输出形式/*
    tbid        a     name
    ----------- ----- --------------------------------------------------
    2           0     name1
    5           0     name2
    9           0     name3(3 行受影响)*/
      

  17.   


    create table t1
    (
    thid int identity(1,1) not null,
    a bit,
    name varchar(50)
    )
    insert into t1
    select 1,'name1' union all
    select 0,'name1' union all
    select 0,'name1' union all
    select 1,'name2' union all
    select 1,'name2' union all
    select 0,'name2' union all
    select 0,'name2' union all
    select 1,'name3' union all
    select 0,'name3' union all
    select 0,'name3' 
    select * from t1select b.* from t1 as a inner join t1 as b on a.thid=b.thid-1 and a.a=1 and b.a=0thid a name
    2 0 name1
    6 0 name2
    9 0 name3
      

  18.   


     create table tests 
    (
       id int identity(1,1),
       a bit,
       name varchar(50)
    )insert into tests values (0,'name1')
    insert into tests values (1,'name1')
    insert into tests values (1,'name1')
    insert into tests values (0,'name2')
    insert into tests values (0,'name2')
    insert into tests values (1,'name2')
    insert into tests values (1,'name2')
    insert into tests values (0,'name3')
    insert into tests values (0,'name3')
    insert into tests values (1,'name3')
    insert into tests values (1,'name3')select * from testsselect * from tests a inner join  (select * from tests a where a=0 ) b  on a.id=b.id+1 where a.a=1
    drop table tests