2012-10-11 2 views
0

이 코드는 작동하지만 매우 오래된 스타일입니다. 누구든지 단일 쿼리에서 이것을 선택하고 그룹화하는 더 좋은 방법이 있습니까? 기본적으로 이것은 단일 헤더 테이블, 픽업을위한 서브 헤더 테이블, 드롭 다운을위한 서브 헤더 테이블 및 각각에 대한 세부 테이블이있는 일반적인 설정입니다.t-sql : 학술 검색,보다 효율적으로 검색/더 좋은 방법

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 

답변

1

하나의 SQL 문에서 동일한 데이터를 가져 오는 방법이 없다고 생각합니다. 드롭 오프 데이터와 픽업 데이터의 두 가지 SQL 쿼리를 만들 수 있습니다.

+0

감사합니다. 나는 그렇게 생각했다. 그러나 나는 단지 그것이 습관의 힘이 아니라는 것을 확신하고 싶었다. :) – plditallo

관련 문제