select d.end from 表 d where not exists
    (select 1 from (select a.* from 表 a ,表 b where a.end = b.start  and a.step +1 = b.step) c where c.step = d.step and c.start = d.start and c.end = d.end)

解决方案 »

  1.   

    select distinct  end  from table where end not in (select distinct start from table )
      

  2.   

    select distinct  end  from table where end not in (select start from table )
      

  3.   

    select 
        d.[end] 
    from 
        表名 d 
    where not exists
        (select 

         from 
    (select 
        a.* 
     from 
        表名 a ,表名 b 
     where 
        a.[end] = b.start  and a.step +1 = b.step) c 
         where 
    c.step = d.step and c.start = d.start and c.[end]= d.[end])
      

  4.   

    我是说:
    select distinct  end  from table where end not in (select distinct start from table )
      

  5.   

    to:libin_ftsafe(子陌红尘)  结果出不来
    to:wutao411(了缘),dulei115()   如果第四步麻六又把该物给张三了  则当前该物的位置应该在张三和王五手上,而用你们的方法查询出来的是不会显示张三的
      

  6.   

    原理:最后一次得到物品的step必须大于最后一次给出物品的step
    select a.end
    from (select end, max(step) as step
          from tablename
          group by end) a
    left join (select start, max(step) as step
          from tablename
          group by start) b on a.end = b.strart and a.step > isnull(b.step, 0)
      

  7.   

    to  dulei115() ( )   也不行   :(
      

  8.   

    例如:
    1    张三    李四
    1    张三    王五
    2    李四    张三应该出来的结果是  张三和王五
    但用你的sql语句出来的是   张三,李四,王五
      

  9.   

    select end
    from test t
    where not exists (select 1 from test where start = t.end)
      

  10.   

    select a.[end]
    from (select [end], max(step) as step
          from tablename
          group by [end]) a
    left join (select start, max(step) as step
          from tablename
          group by start) b on a.[end] = b.start 
    where a.step > isnull(b.step, 0)
      

  11.   

    再试试,原理是对的,刚才的语句写的又问题
    select a.[end]
    from (select [end], max(step) as step
          from tablename
          group by [end]) a
    left join (select start, max(step) as step
          from tablename
          group by start) b on a.[end] = b.start 
    where a.step > isnull(b.step, 0)
      

  12.   

    再试试,原理是对的,刚才的语句写的有问题
    select a.[end]
    from (select [end], max(step) as step
          from tablename
          group by [end]) a
    left join (select start, max(step) as step
          from tablename
          group by start) b on a.[end] = b.start 
    where a.step > isnull(b.step, 0)
      

  13.   

    怎么会了,这是我的语句和结果:
    if object_id('tablename') is not null
      drop table tablename
    select 1 as step, '张三' as start, '李四' as [end]
    into tablename
    union
    select 1, '张三', '王五'
    union
    select 2, '李四', '张三'
    union
    select 3, '张三', '麻六'select * from tablenameselect a.[end]
    from (select [end], max(step) as step
          from tablename
          group by [end]) a
    left join (select start, max(step) as step
          from tablename
          group by start) b on a.[end] = b.start 
    where a.step > isnull(b.step, 0)drop table tablename/*
    1 张三 李四
    1 张三 王五
    2 李四 张三
    3 张三 麻六
    *//*
    麻六
    王五
    */
      

  14.   

    另外的一种数据的情况,查询的结果也是正确的
    if object_id('tablename') is not null
      drop table tablename
    select 1 as step, '张三' as start, '李四' as [end]
    into tablename
    union
    select 1, '张三', '王五'
    union
    select 2, '李四', '张三'select * from tablenameselect a.[end]
    from (select [end], max(step) as step
          from tablename
          group by [end]) a
    left join (select start, max(step) as step
          from tablename
          group by start) b on a.[end] = b.start 
    where a.step > isnull(b.step, 0)drop table tablename/*
    1 张三 李四
    1 张三 王五
    2 李四 张三
    *//*
    王五
    张三
    */
      

  15.   

    真是奇怪   我完全按照你的方法   包括建表  插入数据  写sql 语句  但得出的结果就是/*
    李四
    麻六
    王五
    张三
    */
      

  16.   

    不好意思   现在是这样的
    你的代码放在查询分析器里没有任何问题  结果是对的
    但如果直接在sqlserver的sql窗口里做  查出来的结果是所有的人
      

  17.   

    对  在查询分析器中运行没有任何问题   跟你说的一样
    但放在sqlserver的sql窗口中  查出来的结果就是所有的人   真是奇怪
      

  18.   

    “放在sqlserver的sql窗口”
    ????????
    什么意思
      

  19.   

    对呀dulei115做的是正确的呀,我也实验了。只要end 的step大于sart的step就可以得到结果了。
      

  20.   

    to   dulei115() ( ) :sqlserver的企业管理器,打开表,返回所有行,显示/隐藏sql窗格  在这里面写就有问题,但在查询分析器里做是一点问题都没有
      

  21.   

    在企业管理其中,SQL语句会自动的优化成下面的样子,这个和我在前面给你的错误语句相同
    SELECT a.[end]
    FROM (SELECT [end], MAX(step) AS step
            FROM tablename
            GROUP BY [end]) a LEFT OUTER JOIN
              (SELECT start, MAX(step) AS step
             FROM tablename
             GROUP BY start) b ON a.step > ISNULL(b.step, 0) AND a.[end] = b.start至于为什么会变成这样,我就不知道了!
      

  22.   

    我也想知道为什么,又开了一贴
    http://community.csdn.net/Expert/topic/3669/3669085.xml?temp=.8003351