select from,to,min(cost) as cost from 表一 group by from,to
是第一问答案

解决方案 »

  1.   

    --(a) 请给出一个SQL查询语句,检索出每对城市间最便宜的直飞航班价格,例如针对上述表数据,应返回表二数据;select a.* f
    rom Flight a join(
    select [from],[to],cost=min(cost)
    from Flight
    group by [from],[to]
    )b on a.[from]=b.[from] and a.[to]=b.[to] and a.cost=b.cost
      

  2.   

    --(a) 请给出一个SQL查询语句,检索出每对城市间最便宜的直飞航班价格,例如针对上述表数据,应返回表二数据;select a.* 
    from Flight a join(
    select [from],[to],cost=min(cost)
    from Flight
    group by [from],[to]
    )b on a.[from]=b.[from] and a.[to]=b.[to] and a.cost=b.cost
      

  3.   

    select a.from,b.to,min(a.cost+b.cost) from 表一 a join 表二 b on a.to=b.from and b.to<>a.from group by a.from,b.to
      

  4.   

    --(b)
    select a.[From],b.[to],cost=min(a.cost+b.cost)
    from Flight a join Flight b on a.[to]=b.[from]
    where a.[From]<>b.[to]
    group by a.[From],b.[to]
      

  5.   

    --(c)
    select [from],[to],cost=min(cost)
    from(
    select [from],[to],cost=min(cost)
    from Flight
    group by [from],[to]
    union all 
    select a.[From],b.[to],cost=min(a.cost+b.cost)
    from Flight a join Flight b on a.[to]=b.[from]
    where a.[From]<>b.[to]
    group by a.[From],b.[to]
    )a group by [from],[to]
      

  6.   

    --测试--测试数据
    create table Flight([from] varchar(10),[to] varchar(10),cost int,airline varchar(10))
    insert into Flight
    select 'SF','Denver',300,'Frontier'
    union all select 'SF','Denver',350,'United'
    union all select 'Denver','SF',250,'United'
    union all select 'Denver','SF',250,'Frontier'
    union all select 'Denver','Chicago',250,'American'
    union all select 'Chicago','NY',250,'Delta'
    union all select 'Denver','NY',500,'American'
    union all select 'Denver','NY',400,'TWA'
    union all select 'SF','NY',750,'United'
    go--(a)
    select a.[From],a.[to],a.cost
    from Flight a join(
    select [from],[to],cost=min(cost)
    from Flight
    group by [from],[to]
    )b on a.[from]=b.[from] and a.[to]=b.[to] and a.cost=b.cost
    go--(b)
    select a.[From],b.[to],cost=min(a.cost+b.cost)
    from Flight a join Flight b on a.[to]=b.[from]
    where a.[From]<>b.[to]
    group by a.[From],b.[to]
    go--(c)
    select [from],[to],cost=min(cost)
    from(
    select [from],[to],cost=min(cost)
    from Flight
    group by [from],[to]
    union all 
    select a.[From],b.[to],cost=min(a.cost+b.cost)
    from Flight a join Flight b on a.[to]=b.[from]
    where a.[From]<>b.[to]
    group by a.[From],b.[to]
    )a group by [from],[to]
    go--删除测试环境
    drop table Flight/*--测试结果
    From       to         cost        
    ---------- ---------- ----------- 
    Chicago    NY         250
    Denver     Chicago    250
    Denver     NY         400
    Denver     SF         250
    Denver     SF         250
    SF         Denver     300
    SF         NY         750(所影响的行数为 7 行)From       to         cost        
    ---------- ---------- ----------- 
    SF         Chicago    550
    Denver     NY         500
    SF         NY         700(所影响的行数为 3 行)from       to         cost        
    ---------- ---------- ----------- 
    Denver     Chicago    250
    SF         Chicago    550
    SF         Denver     300
    Chicago    NY         250
    Denver     NY         400
    SF         NY         700
    Denver     SF         250(所影响的行数为 7 行)
    --*/
      

  7.   

    a)select from,to,min(cost) as cost from Flight group by from,to
    b)select a.from,b.to,min(a.cost+b.cost) from Flight a,Flight b where a.to = b.from AND a.from <> b.to  group by a.from,b.to