-- MS SQL Server 测试数据
use xtjc
CREATE TABLE do_test(
id int,
wchar NVARCHAR(50)
);insert into do_test select 1,'A';
insert into do_test select 2,'B';
insert into do_test select 3,'C';
insert into do_test select 4,'D';
insert into do_test select 5,'E';
insert into do_test select 6,'F';
/****** 
要想得到结果集是:  W1         W2
  A          B
  B          C
  C          D
  D          E
  E          F
  ******/DROP TABLE do_test;   忘记如何写。

解决方案 »

  1.   

    select a.wchar  
    ,b.wchar 
    from do_test a
    ,do_test b
    where a.id = b.id-1
      

  2.   

    if object_id('do_test')>0
    drop table do_test
    CREATE TABLE do_test(
    id int,
    wchar NVARCHAR(50)
    );insert into do_test select 1,'A';
    insert into do_test select 2,'B';
    insert into do_test select 3,'C';
    insert into do_test select 4,'D';
    insert into do_test select 5,'E';
    insert into do_test select 6,'F';select * from do_testselect a.wchar as w1,(select top 1 b.wchar from do_test as b where a.id <b.id) as w2
    from do_test as a 
    where a.wchar != (select top 1 wchar from do_test order by id desc)结果
    A B
    B C
    C D
    D E
    E F
      

  3.   

    是这个意思?
    CREATE TABLE do_test(
    id int,
    wchar NVARCHAR(50)
    );insert into do_test select 1,'A';
    insert into do_test select 2,'B';
    insert into do_test select 3,'C';
    insert into do_test select 4,'D';
    insert into do_test select 5,'E';
    insert into do_test select 6,'F';SELECT TOP 5 w1=wchar,w2=(SELECT wchar FROM do_test WHERE id=t.id+1) FROM do_test t/*
    A           B
    B           C
    C           D
    D           E
    E           F
        
    */
      

  4.   


    select a.wchar,b.wchar from do_test as a,do_test as b where b.id=a.id+1;
      

  5.   

    CREATE TABLE do_test(
    id int,
    wchar NVARCHAR(50)
    );insert into do_test select 1,'A';
    insert into do_test select 2,'B';
    insert into do_test select 3,'C';
    insert into do_test select 4,'D';
    insert into do_test select 5,'E';
    insert into do_test select 6,'F';select m.wchar , (select top 1 wchar from do_test n where n.id > m.id order by id) from do_test mdrop table do_test
    /*
    wchar                                                                                                 
    -------------------------------------------------- -------------------------------------------------- 
    A                                                  B
    B                                                  C
    C                                                  D
    D                                                  E
    E                                                  F
    F                                                  NULL(所影响的行数为 6 行)
    */