0

这段代码有效,但看起来非常“老派”。有没有人有更好的方法在单个查询中选择和分组?基本上,这只是一个通用设置,只有一个标题表、一个用于接送的子标题表、一个用于下车的子标题表和每个的详细信息表。

 select h.id
  ,h.col1 as Customer
  ,h.col2 as Req
  ,d.subid as DropId
  ,d.col1 as DropCity
  ,d.col2 as DropLoc
  ,dd.detid as DropDetailId
  ,dd.col1 as DropSpot
  ,dd.col2 as DropAWBorPackage
  ,NULL as PickupId
  ,NULL as PickUpCity
  ,NULL as PickUpLoc
  ,NULL as PickupDetailId
  ,NULL as PickupLoc
  ,NULL as PickupAWBorPackage
 from [scratch].GenericHeader h
   inner join [scratch].GenericSubHeader d on d.id = h.id
   inner join [scratch].GenericDetail dd on dd.id = h.id
     and dd.id = d.id
     and dd.subid = d.subid
 UNION ALL   
  select h.id
   ,h.col1 as Customer
   ,h.col2 as Req
   ,NULL as DropId
   ,NULL as DropCity
   ,NULL as DropLoc
   ,NULL as DropDetailId
   ,NULL as DropSpot
   ,NULL as AWBorPackage
   ,p.sub2id as PickupId
   ,p.col1 as PickUpCity
   ,p.col2 as PickUpLoc
   ,pp.det2id as PickupDetailId
   ,pp.col1 as PickupLoc
   ,pp.col2 as PickupAWBorPackage
  from  [scratch].GenericHeader h
    inner join [scratch].GenericSubHeader2 p on p.id = h.id
    inner join [scratch].GenericDetail2 pp on pp.id = h.id
      and pp.id = p.id
      and pp.sub2id = p.sub2id
  order by Req,DropId,DropDetailId,PickupId,PickupDetailId
4

1 回答 1

1

我认为没有办法在一条 SQL 语句中获取相同的数据。您可能想要进行两个 SQL 查询,一个用于下车数据,另一个用于取货数据。

于 2012-10-11T17:40:55.027 回答