查询以获取同一表中字段的比较

时间:2017-04-21 06:23:08

标签: sql sql-server sql-server-2008

我正在尝试构建一个逻辑来获取报告中的匹配字段,以便我能够并排比较报告,如"预期查询输出"屏幕截图。

现有表格中的行: -

enter image description here

预期的查询输出: -

enter image description here

为此,我尝试使用SQL Query构建INNER JOIN。 但似乎在这种情况下这不起作用。现在我的脑子一片空白。 有人可以指导我正确的方向吗?

以下是我尝试构建的示例代码。

; 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

也欢迎没有代码的逻辑:)

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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