2017-11-13 9 views
4

세 가지 수준의 계층 적 데이터와 네 번째 수준의 선택적 데이터를 가진 데이터베이스 구조 (아래의 ER 다이어그램)가 있습니다. enter image description here왼쪽 외부 조인트를 통해 쿼리를 반복하지 않는 데이터 추가

나는 세 가지 수준의 드 정규화 된 데이터를 얻을 수있는 쿼리를 작성하는 경우 - 세 개의 테이블은 아래 그림을 통해 샘플 데이터로 레벨 3 수준 1 :

enter image description here

조회 이 데이터 레이아웃은 매우 간단하고 예상대로 아래와 같습니다.

enter image description here

아래 쿼리를 실행하면 다음과 같은 출력이 표시됩니다. 그리고 L1 세트를 L4로 이동시키고 다른 L4를 다른 쿼리로 이동 한 다음 L1 - L4 세트에 합류하여 다양한 조합을 시도했습니다. 다시 이것은 기대되는 라인에있다.

SELECT  [Group].GroupId, [Group].GroupName, Category.CategoryId, Category.CategoryName, RLI.RLIId, RLI.RLIText, Comment.CommentId, Comment.CommentText, ManagementResponse.ManagementResponseId, 
         ManagementResponse.ManagementResponseTest 
FROM   Category INNER JOIN 
         [Group] ON Category.GroupId = [Group].GroupId INNER JOIN 
         RLI ON Category.CategoryId = RLI.CategoryId LEFT OUTER JOIN 
         ManagementResponse ON RLI.RLIId = ManagementResponse.RLIId LEFT OUTER JOIN 
         Comment ON RLI.RLIId = Comment.RLIId 

enter image description here

는 그러나, 나는 다음과 같은 형식으로 데이터가 필요 -이 내가이 레벨 I로 반복 4 데이터를 원하지 않는 (활용하는 방법을 알아낼 수 없습니까 것입니다 왼쪽 외부를 통해 추가 레벨 4 데이터를 추가하는)을 조인 enter image description here

+0

는 u는 내부하려고 않았다 대신 왼쪽의 조인 조인 ? –

+0

레벨 4의 데이터가 선택 사항이므로 내부 조인이 솔루션이되는 방법을 잘 모르겠습니다. 내부 조인은 상위 레벨 데이터를 유실시킵니다. –

+0

'CommentId'와'ManagementResponseId'의 동등성은 의도 한 출력인지 의도 한 출력인가? – Caleth

답변

1

같은 더 RLIId 외에 CommentManagementResponse의 관계, 무엇인가가없는 경우 또는 : enter image description here

WITH CommentAndResponse AS (

SELECT Comment.CommentId, 
     Comment.CommentText, 
     ManagementResponse.ManagementResponseId, 
     ManagementResponse.ManagementResponseTest, 
    COALESCE(Comment.RLIId, ManagementResponse.RLIId) AS RLIId 
FROM (
    (SELECT Comment.CommentId, 
      Comment.CommentText, 
      Comment.RLIId, 
      ROW_NUMBER() OVER (PARTITION BY Comment.RLIId ORDER BY Comment.CommentId) AS CommentRowNumber 
    FROM Comment) AS Comment 
    FULL JOIN 
    (SELECT ManagementResponse.ManagementResponseId, 
      ManagementResponse.ManagementResponseTest, 
      ManagementResponse.RLIId, 
      ROW_NUMBER() OVER (PARTITION BY ManagementResponse.RLIId ORDER BY ManagementResponse.ManagementResponseId) AS ManagementResponseRowNumber 
    FROM ManagementResponse) AS ManagementResponse 
    ON Comment.CommentRowNumber = ManagementResponse.ManagementResponseRowNumber AND Comment.RLIId = ManagementResponse.RLIId) 
    ) 

SELECT   [Group].GroupId, [Group].GroupName, Category.CategoryId, Category.CategoryName, RLI.RLIId, RLI.RLIText, CommentAndResponse.CommentId, CommentAndResponse.CommentText, CommentAndResponse.ManagementResponseId, CommentAndResponse.ManagementResponseTest 
FROM   [Category] 
INNER JOIN  [Group] ON Category.GroupId = [Group].GroupId 
INNER JOIN  [RLI] ON Category.CategoryId = RLI.CategoryId 
LEFT OUTER JOIN [CommentAndResponse] ON RLI.RLIId = CommentAndResponse.RLIId 
+0

그것은 완벽했습니다. 좋은 분! –

1

당신은 Comment.CommentIdManagementResponse.ManagementResponseId에 같거나 중 하나가 null로 지정해야합니다. 그건 JOIN 또는이 그 ID를 동일하게 시작한다고 가정 별도의 WHERE

SELECT   [Group].GroupId, [Group].GroupName, Category.CategoryId, Category.CategoryName, RLI.RLIId, RLI.RLIText, Comment.CommentId, Comment.CommentText, ManagementResponse.ManagementResponseId, 
         ManagementResponse.ManagementResponseTest 
FROM   [Category] 
INNER JOIN  [Group] ON Category.GroupId = [Group].GroupId 
INNER JOIN  [RLI] ON Category.CategoryId = RLI.CategoryId 
LEFT OUTER JOIN [ManagementResponse] ON RLI.RLIId = ManagementResponse.RLIId 
LEFT OUTER JOIN [Comment] ON RLI.RLIId = Comment.RLIId 
WHERE   ManagementResponse.ManagementResponseId = Comment.CommentId OR ManagementResponse.ManagementResponseId IS NULL OR Comment.CommentId IS NULL 

의 일부가 될 수는 모델링하고자하는 관계이다. 예제 데이터는 이것을 보여주는 것처럼 보이지만 예제를 어떻게 조합했는지는 우연 일 수 있습니다. 이 쿼리는 당신에게 최종 출력 줄 것이다

WITH CommentAndResponse AS (
    SELECT Comment.CommentId, Comment.CommentText, ManagementResponse.ManagementResponseId, ManagementResponse.ManagementResponseTest, 
     COALESCE(Comment.RLIId, ManagementResponse.RLIId) AS RLIId, 
     ROW_NUMBER() OVER (ORDER BY Comment.CommentId, ManagementResponse.ManagementResponseId, PARTITION BY Comment.RLIId, ManagementResponse.RLIId) AS rn 
    FROM Comment 
    FULL JOIN ManagementResponse ON Comment.RLIId = ManagementResponse.RLIId) 
SELECT   [Group].GroupId, [Group].GroupName, Category.CategoryId, Category.CategoryName, RLI.RLIId, RLI.RLIText, CommentAndResponse.CommentId, CommentAndResponse.CommentText, CommentAndResponse.ManagementResponseId, CommentAndResponse.ManagementResponseTest 
FROM   [Category] 
INNER JOIN  [Group] ON Category.GroupId = [Group].GroupId 
INNER JOIN  [RLI] ON Category.CategoryId = RLI.CategoryId 
LEFT OUTER JOIN [CommentAndResponse] ON RLI.RLIId = CommentAndResponse.RLIId AND CommentAndResponse.rn = 1 
+0

답해 주셔서 감사합니다. 내 팀 구성원 중 하나가 솔루션 (구문 문제가 있음)을 기반으로 작업하고 있습니다. 답변으로 표시된 작업 솔루션을 참조하십시오. 귀하의 의견은 우리가 거기에 도달하는 데 도움이되었습니다. –