在一個圓上有幾個標識符(A~G),標識符之間有它們的距離
已知開始,終止標識符及其方向,求它們之間的距離
如何寫SQL資料表結構及資料如下
BeginSite EndSite BELength
A B 10.5
B C 20
c D 15.8
D E 30
E F 28
F G 45.2
G A 18求AF順時針方向的距離
求AF逆時針方向的距離

解决方案 »

  1.   

    顺时针:
     select sum(BELength) from 表 where BeginSite between 'A' and 'F'逆时针:
      select sum(BELength) from 表 where BeginSite >='F'
      

  2.   

    楼上的方法有问题吧
    如果数据是:
    AB   10
    BC   10
    BD   10
    DF   10
    FC   10
    FA   10
    呢?这道题蛮有意思的.
    记得邹大哥新书里,有类似的题目.
    想想...
      

  3.   

    --生成测试数据
    create table T(BeginSite char(1),EndSite char(1),BELength numeric(5,1))
    insert into T select 'A','B',10.5
    insert into T select 'B','C',20
    insert into T select 'C','D',15.8
    insert into T select 'D','E',30
    insert into T select 'E','F',28
    insert into T select 'F','G',45.2
    insert into T select 'G','A',18
    go
    --创建用户定义函数
    create function f_getLine(@BeginSite char(1),@Deasil int)   --@Deasil:1、顺时针;else、逆时针
    returns @a table(Site char(1),BELength numeric(5,1))
    as
    begin
        
        insert into @a
        select 
            (case @Deasil when 1 then EndSite else BeginSite end),BELength
        from 
            t 
        where 
            (case @Deasil when 1 then BeginSite else EndSite end)='A'
            
        while not exists(select 1 from @a where Site=@BeginSite)
        begin
            insert into @a
            select
                (case @Deasil when 1 then a.EndSite else a.BeginSite end),a.BELength
            from
                t  a,
                @a b
            where
                (case @Deasil when 1 then BeginSite else EndSite end)=b.Site
                and
                not exists(select 1 from @a where Site = (case @Deasil when 1 then a.EndSite else a.BeginSite end))
        end
        
        return
    end
    go
    --以A为起点順時針方向的距離
    select sum(BELength) from dbo.f_getLine('A',1)--以A为起点逆時針方向的距離
    select sum(BELength) from dbo.f_getLine('A',0)
      

  4.   

    --declare @s table
    (k varchar(2),m varchar(2),Be numeric(5,1))
    --正时针情况
    select sum(Be) from @s where  k<'F' and k<m--逆时针情况declare @t varchar(4)
    select @t = m from @s where k='F'
    if @t>='F'
    select sum(Be) from @s where k>='F'
    ELSE
    select sum(Be) from @s where ASCII(k)-ASCII(m)>=0
      

  5.   

    测试数据,当出现FC时候
    k    m    Be      
    ---- ---- ------- 
    A    B    10.5
    B    C    20.0
    C    D    15.8
    D    E    30.0
    E    F    28.0
    F    C    45.2
    C    A    18.0(所影响的行数为 7 行)                                         
    ---------------------------------------- 
    104.3(所影响的行数为 1 行)                                         
    ---------------------------------------- 
    63.2当出现FG的时候k    m    Be      
    ---- ---- ------- 
    A    B    10.5
    B    C    20.0
    C    D    15.8
    D    E    30.0
    E    F    28.0
    F    G    45.2
    G    A    18.0(所影响的行数为 7 行)                                         
    ---------------------------------------- 
    104.3(所影响的行数为 1 行)                                         
    ---------------------------------------- 
    63.2(所影响的行数为 1 行)可以把自行更改为函数,就可以用一条语句执行了!
      

  6.   

    samfeng_2003(风云) 的"k<m"这个查询条件太妙了!!!
      

  7.   

    这个问题很有趣,我也来做做:
    create table Test(BeginSite char(1),EndSite char(1),BELength numeric(5,1))
    insert into Test select 'A','B',10.5
    insert into Test select 'B','C',20
    insert into Test select 'C','D',15.8
    insert into Test select 'D','E',30
    insert into Test select 'E','F',28
    insert into Test select 'F','G',45.2
    insert into Test select 'G','A',18
    go
    create procedure P_len
    @start varchar(5),--起点
    @end varchar(5),--终点
    @flag int--方向0:顺时,1:逆进
    as
    declare @sum numeric(5,1),@bh varchar(5)
    select @sum=0,@bh=@startif @flag=0 while @bh<>@end
    select @sum=@sum+belength,@bh=endsite from test where beginsite=@bhelsewhile @bh<>@end
    select @sum=@sum+belength,@bh=beginsite from test where endsite=@bhselect [起点]=@start,[终点]=@end,[方向]=(case @flag when 0 then '顺时' else '逆时' end),[总长]=@sumgo--测试
    p_len 'a','f',0
    /*
    起点    终点    方向   总长      
    ----- ----- ---- ------- 
    a     f     顺时   104.3(所影响的行数为 1 行)
    */
    p_len 'a','f',1
    /*
    起点    终点    方向   总长      
    ----- ----- ---- ------- 
    a     f     逆时   63.2(所影响的行数为 1 行)
    */
    p_len 'b','g',0
    /*
    起点    终点    方向   总长      
    ----- ----- ---- ------- 
    b     g     顺时   139.0(所影响的行数为 1 行)
    */
    p_len 'g','g',0
    /*
    起点    终点    方向   总长      
    ----- ----- ---- ------- 
    g     g     顺时   .0(所影响的行数为 1 行)
    */
    --删除测试数据
    drop table test
    drop procedure p_len
      

  8.   

    samfeng_2003(风云) 的"k<m"这个查询条件太妙了!!!
    ---------风云的代码是不对的,
    select sum(Be) from @s where  k<'F' and k<m
    --请问上面‘F’是什么回事,如果上面不是‘A’到‘G’,也就是说根本没有‘F’呢?
      

  9.   

    create table T(BeginSite char(1),EndSite char(1),BELength numeric(5,1))
    insert into T select 'A','B',10.5
    insert into T select 'B','C',20
    insert into T select 'C','D',15.8
    insert into T select 'D','E',30
    insert into T select 'E','F',28
    insert into T select 'F','G',45.2
    insert into T select 'G','A',18
    godeclare @begin char(1),@end char(1)select @begin = 'B',@end = 'F'select sum(belength) from T where beginsite >= @begin and beginSite < @end and beginSite = char(ascii(endSite) - 1)select sum(belength) from T where endsite = @begin or endSite > @end and beginSite = char(ascii(endSite) - 1) or endsite = 'A' and beginsite = 'G'drop table T
      

  10.   

    呵呵!不用夸我哈!~还是看了 vivianfdlpw()补充的:)
      

  11.   

    呵呵!select sum(Be) from @s where  k<'F' and k<m
    --请问上面‘F’是什么回事,如果上面不是‘A’到‘G’,也就是说根本没有‘F’呢?是的!我考虑的是特殊情况,完备性不够!:( 谢谢,再看看!
      

  12.   

    k    m    Be      
    ---- ---- ------- 
    A    B    10.5
    B    C    20.0
    C    D    15.8
    D    E    30.0
    E    A    28.0
    A    B    45.2
    B    A    18.0如果数据是这样的,那么如果数据是‘A’到‘D’就更难得出语句,我觉得这个时候只有用游标来做了!
      

  13.   

    那么我们可以这样考虑,一种是正常情况,也就是从A~Z正常排序的情况,这种情况下没有重复值,相对比较简单一些。
    declare @s table
    (k varchar(2),m varchar(2),Be numeric(5,1))
    insert into @s select 'A','B',10.5
    insert into @s select 'B','C',20
    insert into @s select 'C','D',15.8
    insert into @s select 'D','E',30
    insert into @s select 'E','F',28
    insert into @s select 'F','G',45.2
    insert into @s select 'G','A',18select * from @sdeclare @x varchar(4),@y varchar(4)set @x = 'F'
    set @y = 'G'--顺时针排序
     select sum(Be) from @s where  k<@y and k>=@x and k<=m 
    --逆时针情况
     select sum(Be) from @s where  k>m or m>@y or k<@x 至于有重复,还有非正常排序的方式,大家都想想吧!:( 我已经技穷了!
      

  14.   

    我有考虑了一下,其实有一种对于只要没有重复值的那么都可以实行的方式,
    比如不存在有k,m 重复的情况,如下
    k    m    Be      
    ---- ---- ------- 
    A    B    10.5
    B    C    20.0
    C    D    15.8
    D    E    30.0
    E    A    28.0
    A    B    45.2
    B    A    18.0
    那么就可以用declare @x  varchar(4),@y varchar(4),@sum1 int,@sum2  int
    set @x='B'
    set @y='D'
    ---顺时针情况
    select @sum1 = isnull(sum(Be),0) from @s where k>=@x and k<@y and (ascii(m)-ascii(k)=1)
    ---逆时针情况
    select @sum2 = sum(Be)-@sum1 from @sprint @sum1,@sum2其实逆时针和顺时针相加为总和