2017-04-21 58 views
0

我想建立一個邏輯來獲取報表中的匹配字段,我應該能夠並排比較報表,如「預期查詢輸出「截圖。查詢得到同一個表中的字段

行現有表格: -

enter image description here

預計查詢輸出: -

enter image description here

爲了得到這個,我試圖建立與INNER JOIN一個SQL Query。 但似乎這不會在這種情況下工作。現在我的大腦空白。 有人可以引導我在正確的方向嗎?

下面是我試圖構建的示例代碼。

; WITH CTE AS (
      SELECT 'R1' AS ReportName, 'A' FieldName, 'M' FieldType 
UNION ALL SELECT 'R1' AS ReportName, 'B' FieldName, 'D' FieldType 
UNION ALL SELECT 'R1' AS ReportName, 'C' FieldName, 'D' FieldType 
UNION ALL SELECT 'R1' AS ReportName, 'D' FieldName, 'D' FieldType 
UNION ALL SELECT 'R2' AS ReportName, 'A' FieldName, 'M' FieldType 
UNION ALL SELECT 'R2' AS ReportName, 'B' FieldName, 'D' FieldType 
UNION ALL SELECT 'R2' AS ReportName, 'D' FieldName, 'D' FieldType 
UNION ALL SELECT 'R3' AS ReportName, 'K' FieldName, 'M' FieldType 
UNION ALL SELECT 'R3' AS ReportName, 'C' FieldName, 'D' FieldType 
UNION ALL SELECT 'R4' AS ReportName, 'P' FieldName, 'D' FieldType 
UNION ALL SELECT 'R4' AS ReportName, 'Q' FieldName, 'D' FieldType 
UNION ALL SELECT 'R4' AS ReportName, 'R' FieldName, 'M' FieldType 
UNION ALL SELECT 'R5' AS ReportName, 'A' FieldName, 'M' FieldType 
UNION ALL SELECT 'R5' AS ReportName, 'B' FieldName, 'D' FieldType 
UNION ALL SELECT 'R5' AS ReportName, 'L' FieldName, 'M' FieldType 
) 

SELECT C1.ReportName, count(C1.FieldName), C2.ReportName, COUNT(C2.FieldName) FROM CTE C1 
inner join CTE C2 
on C1.FieldName = C2.FieldType 
and C1.FieldType = C2.FieldType 
group by C1.ReportName, C2.ReportName 

無碼A邏輯也歡迎:)

+0

您需要解釋可以生成您的預期輸出的數據中的關係。在匹配報告中,R1(和R2,R3,R4在matchreport中)有3行,但只有一行R2(在匹配報告中有R1)的關係如何? –

+0

單個表具有報告名稱和它包含的字段。我想比較那些具有共同字段(度量和維度)的報告,如預期輸出中所示。我也想要總數,以便我可以詳細查看。所以我們可以說關係是多對多的? – Aditya

回答

0

嘗試這種情況:

 ; WITH CTE AS (
        SELECT 'R1' AS ReportName, 'A' FieldName, 'M' FieldType 
     UNION ALL SELECT 'R1' AS ReportName, 'B' FieldName, 'D' FieldType 
     UNION ALL SELECT 'R1' AS ReportName, 'C' FieldName, 'D' FieldType 
     UNION ALL SELECT 'R1' AS ReportName, 'D' FieldName, 'D' FieldType 
     UNION ALL SELECT 'R2' AS ReportName, 'A' FieldName, 'M' FieldType 
     UNION ALL SELECT 'R2' AS ReportName, 'B' FieldName, 'D' FieldType 
     UNION ALL SELECT 'R2' AS ReportName, 'D' FieldName, 'D' FieldType 
     UNION ALL SELECT 'R3' AS ReportName, 'K' FieldName, 'M' FieldType 
     UNION ALL SELECT 'R3' AS ReportName, 'C' FieldName, 'D' FieldType 
     UNION ALL SELECT 'R4' AS ReportName, 'P' FieldName, 'D' FieldType 
     UNION ALL SELECT 'R4' AS ReportName, 'Q' FieldName, 'D' FieldType 
     UNION ALL SELECT 'R4' AS ReportName, 'R' FieldName, 'M' FieldType 
     UNION ALL SELECT 'R5' AS ReportName, 'A' FieldName, 'M' FieldType 
     UNION ALL SELECT 'R5' AS ReportName, 'B' FieldName, 'D' FieldType 
     UNION ALL SELECT 'R5' AS ReportName, 'L' FieldName, 'M' FieldType 
     ) 
SELECT DISTINCT A.REPO_NAME1, RC1,B.REPO_NAME2, RC2, MF 
FROM (SELECT C1.ReportName AS REPO_NAME1, FieldName AS FIELD_N1, FieldType AS FIELD_T1,COUNT(*) OVER (PARTITION BY C1.ReportName) AS RC1 
     FROM CTE C1 
     ) A 
LEFT JOIN (SELECT C2.ReportName AS REPO_NAME2, FieldName AS FIELD_N2, FieldType AS FIELD_T2,COUNT(*) OVER (PARTITION BY C2.ReportName) AS RC2 
     FROM CTE C2 
     ) B ON A.FIELD_N1 = B.FIELD_N2 
     and A.FIELD_T1 = B.FIELD_T2 
     AND A.REPO_NAME1 <> B.REPO_NAME2 
LEFT JOIN (SELECT C3.ReportName AS REPO_NAME1, C4.ReportName AS REPO_NAME2, COUNT(*) AS MF 
      FROM CTE C3 
      LEFT JOIN CTE C4 ON C3.FieldName = C4.FieldName 
          AND C3.FieldType = C4.FieldType 
          AND C3.ReportName <> C4.ReportName 
      GROUP BY C3.ReportName, C4.ReportName 
      ) D ON A.REPO_NAME1 = D.REPO_NAME1 AND B.REPO_NAME2= D.REPO_NAME2 
ORDER BY A.REPO_NAME1 

輸出:

REPO_NAME1 RC1   REPO_NAME2 RC2   MF 
---------- ----------- ---------- ----------- ----------- 
R1   4   R2   3   3 
R1   4   R3   2   1 
R1   4   R5   3   2 
R2   3   R1   4   3 
R2   3   R5   3   2 
R3   2   NULL  NULL  NULL 
R3   2   R1   4   1 
R4   3   NULL  NULL  NULL 
R5   3   NULL  NULL  NULL 
R5   3   R1   4   2 
R5   3   R2   3   2 
+0

我的SQL缺少MatchCount字段。所以我沒有得到匹配計數,因爲在上面的查詢以及預期輸出... :( – Aditya

+0

@Aditya:你可以嘗試新版本(2)張貼嗎?它是否可能你的預期結果是不完全一致的樣本數據? – etsa

0

嘗試:

DECLARE @table TABLE (ReportName NVARCHAR(2),Field NVARCHAR(2), FieldType NVARCHAR(2)) 
INSERT INTO @table VALUES 
('R1','A','M'),('R1','B','D'),('R1','C','D'),('R1','D','D'),('R2','A','M'),('R2','B','D'),('R2','D','D'), 
('R3','K','M'),('R3','C','D'),('R4','P','D'),('R4','Q','D'),('R5','A','M'),('R5','B','D'),('R5','L','M') 

;WITH AA AS (SELECT ReportName,Count(ReportName) TotalFields FROM @table GROUP BY ReportName) 

SELECT AA.ReportName,AA.TotalFields,BB.MatchReport,CC.TotalFields FROM AA LEFT JOIN (
SELECT DISTINCT A.ReportName,B.ReportName MatchReport FROM @table A 
LEFT JOIN @table B ON A.Field = B.Field AND A.FieldType = B.FieldType AND A.ReportName<>B.ReportName) BB ON AA.ReportName = BB.ReportName 
LEFT JOIN AA CC ON BB.MatchReport = CC.ReportName 
相關問題