这段代码有效,但看起来非常“老派”。有没有人有更好的方法在单个查询中选择和分组?基本上,这只是一个通用设置,包含一个标题表,一个用于拾取的子标题表,一个用于删除的子标题表和每个表的详细信息表。
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
答案 0 :(得分:1)
我认为没有办法在一个SQL语句中获取相同的数据。您可能希望进行两个SQL查询,一个用于下拉数据,另一个用于拾取数据。