表table_A:
  路线代码 起点 终点 起点桩号 终点桩号   
  Y440608 大湾 石柱 0.56 2.35   
  Y440608 石柱 观音桥 2.35 8.52
  Y440608 观音桥 芦溪 8.52 11.98
  Y440605 龙桥 磙子河 36.58 56.32现在要写一条sql 让结果这样
  路线代码 起点 终点 起点桩号 终点桩号   
  Y440608 大湾 芦溪 0.56 11.98
  Y440605 龙桥 磙子河 36.58 56.32
功能
create table table_A(路线代码 varchar(10),起点 varchar(10),终点 varchar(10),起点桩号 decimal(18,2),终点桩号 decimal(18,2))
insert into table_a values('Y440608', '大湾'  , '石柱'  , 0.56  ,2.35)
insert into table_a values('Y440608', '石柱'  , '观音桥', 2.35  ,8.52)
insert into table_a values('Y440608', '观音桥', '芦溪'  , 8.52  ,11.98)
insert into table_a values('Y440605', '龙桥'  , '磙子河', 36.58 ,56.32)
goselect 路线代码,max(起点) 起点,max(终点) 终点,max(起点桩号) 起点桩号,max(终点桩号) 终点桩号 from
(
select t.路线代码,
       起点 = (case when not exists (select 1 from table_a where 路线代码 = t.路线代码 and 终点 = t.起点) then 起点 end),
       终点 = (case when not exists (select 1 from table_a where 路线代码 = t.路线代码 and 起点 = t.终点) then 终点 end),
       起点桩号 = (case when not exists (select 1 from table_a where 路线代码 = t.路线代码 and 终点 = t.起点) then 起点桩号 end),
       终点桩号 = (case when not exists (select 1 from table_a where 路线代码 = t.路线代码 and 起点 = t.终点) then 终点桩号 end)
from table_a t
) m
group by 路线代码drop table table_a/*
路线代码       起点         终点         起点桩号                 终点桩号                 
---------- ---------- ---------- -------------------- -------------------- 
Y440605    龙桥         磙子河        36.58                56.32
Y440608    大湾         芦溪         .56                  11.98(所影响的行数为 2 行)里面的这个子查询select t.路线代码,
       起点 = (case when not exists (select 1 from table_a where 路线代码 = t.路线代码 and 终点 = t.起点) then 起点 end),
       终点 = (case when not exists (select 1 from table_a where 路线代码 = t.路线代码 and 起点 = t.终点) then 终点 end),
       起点桩号 = (case when not exists (select 1 from table_a where 路线代码 = t.路线代码 and 终点 = t.起点) then 起点桩号 end),
       终点桩号 = (case when not exists (select 1 from table_a where 路线代码 = t.路线代码 and 起点 = t.终点) then 终点桩号 end)
from table_a t 怎么就能实现了 想不通。
原帖http://topic.csdn.net/u/20110122/16/368294ae-208a-4da2-a96e-fdb12bce9e0b.html?4977

解决方案 »

  1.   

    select 1 from table_a where 路线代码 = t.路线代码 and 终点 = t.起点 这里面的 我理解上应该是横成立吧 
      

  2.   

    not exists 
    你理解这个,就理解语句了.
      

  3.   

    select 1 from table_a where 路线代码 = t.路线代码 and 终点 = t.起点 
    我理解为 恒成立   not exists ()  不就是 一条数据没有嘛能多说点嘛  我比较菜鸟
      

  4.   

    not exists是不存在的意思  是不存在与外面数据 路线代码和终点都一样的 就...