MYSQL结果中的重复行LEFT JOIN UNION RIGHT JOIN(FULL JOIN)

时间:2018-02-08 00:13:31

标签: mysql sql-server join duplicates

我有一个复杂的查询,我想完全加入。我正在将我的数据库从SQL Server迁移到MYSQL,并且在转换此SQL查询时遇到困难。任何帮助将受到高度赞赏。这是实际的SQL Server查询:

SELECT * FROM (
    SELECT
        tac.Name as 'Category',
        SUM(a.AlwaysFailingCount) as 'ConsistentlyFailingToday',
        SUM(a.StartedFailing) as 'StartedFailingToday',
        SUM(b.AlwaysFailingCount) as 'ConsistentlyFailingYesterday',
        SUM(b.StartedFailing) as 'StartedFailingYesterday'
    FROM (
        SELECT DISTINCT
            ta.TestAreaCategoryId,
            d.TestCaseId,
            d.AlwaysFailingCount,
            d.PassedNowFailedCount - d.FailedLastTestCount as 'StartedFailing'
        FROM DailyScenarioResults d
        JOIN TestAreaTestCases tatc ON (tatc.TestCase_Id = d.TestCaseId)
        JOIN TestAreas ta ON (tatc.TestArea_Id = ta.Id)
        WHERE
            d.BranchId = @BranchId AND
            d.CachedDate = @TodayDate AND
            (d.AlwaysFailingCount != 0 OR (d.PassedNowFailedCount - d.FailedLastTestCount) != 0)
    ) a
    FULL JOIN (
        SELECT DISTINCT
            ta.TestAreaCategoryId,
            d.TestCaseId,
            d.AlwaysFailingCount,
            d.PassedNowFailedCount - d.FailedLastTestCount as 'StartedFailing'
        FROM DailyScenarioResults d
        JOIN TestAreaTestCases tatc ON (tatc.TestCase_Id = d.TestCaseId)
        JOIN TestAreas ta ON (tatc.TestArea_Id = ta.Id)
        WHERE
            d.BranchId = @BranchId AND
            d.CachedDate = @YesterdayDate AND
            (d.AlwaysFailingCount != 0 OR (d.PassedNowFailedCount - d.FailedLastTestCount) != 0)
    ) b ON (a.TestCaseId = b.TestCaseId AND a.TestAreaCategoryId = b.TestAreaCategoryId)
    JOIN TestAreaCategories tac ON (a.TestAreaCategoryId = tac.Id)
    GROUP BY tac.Name
    UNION
    SELECT
        'Any Category' as 'Category',
        SUM(a.AlwaysFailingCount) as 'ConsistentlyFailingToday',
        SUM(a.StartedFailing) as 'StartedFailingToday',
        SUM(b.AlwaysFailingCount) as 'ConsistentlyFailingYesterday',
        SUM(b.StartedFailing) as 'StartedFailingYesterday'
    FROM (
        SELECT
            d.TestCaseId,
            d.AlwaysFailingCount,
            d.PassedNowFailedCount - d.FailedLastTestCount as 'StartedFailing'
        FROM DailyScenarioResults d
        WHERE
            d.BranchId = @BranchId AND
            d.CachedDate = @TodayDate AND
            (d.AlwaysFailingCount != 0 OR (d.PassedNowFailedCount - d.FailedLastTestCount) != 0)
    ) a
    FULL JOIN (
        SELECT
            d.TestCaseId,
            d.AlwaysFailingCount,
            d.PassedNowFailedCount - d.FailedLastTestCount as 'StartedFailing'
        FROM DailyScenarioResults d
        WHERE
            d.BranchId = @BranchId AND
            d.CachedDate = @YesterdayDate AND
            (d.AlwaysFailingCount != 0 OR (d.PassedNowFailedCount - d.FailedLastTestCount) != 0)
    ) b ON (a.TestCaseId = b.TestCaseId)
) r
ORDER BY
    r.ConsistentlyFailingToday DESC,
    r.StartedFailingToday DESC,
    r.ConsistentlyFailingYesterday DESC,
    r.StartedFailingYesterday DESC

我试过这个,但我有重复的行:

DECLARE @TodayDate DATE = GETDATE() - 39, @YesterdayDate DATE = GETDATE() - 40, @BranchId INT = 56;
SELECT * FROM (
                SELECT
                    tac.Name as 'Category',
                    SUM(a.AlwaysFailingCount) as 'ConsistentlyFailingToday',
                    SUM(a.StartedFailing) as 'StartedFailingToday',
                    SUM(b.AlwaysFailingCount) as 'ConsistentlyFailingYesterday',
                    SUM(b.StartedFailing) as 'StartedFailingYesterday'
                FROM (
                        SELECT DISTINCT
                            ta.TestAreaCategoryId,
                            d.TestCaseId,
                            d.AlwaysFailingCount,
                            d.PassedNowFailedCount - d.FailedLastTestCount as 'StartedFailing'
                        FROM DailyScenarioResults d
                        JOIN TestAreaTestCases tatc ON (tatc.TestCase_Id = d.TestCaseId)
                        JOIN TestAreas ta ON (tatc.TestArea_Id = ta.Id)
                        WHERE
                            d.BranchId = @BranchId AND
                            d.CachedDate = @TodayDate AND
                            (d.AlwaysFailingCount != 0 OR (d.PassedNowFailedCount - d.FailedLastTestCount) != 0)
                    ) a
                LEFT JOIN (
                            SELECT DISTINCT
                                ta.TestAreaCategoryId,
                                d.TestCaseId,
                                d.AlwaysFailingCount,
                                d.PassedNowFailedCount - d.FailedLastTestCount as 'StartedFailing'
                            FROM DailyScenarioResults d
                            JOIN TestAreaTestCases tatc ON (tatc.TestCase_Id = d.TestCaseId)
                            JOIN TestAreas ta ON (tatc.TestArea_Id = ta.Id)
                            WHERE
                                d.BranchId = @BranchId AND
                                d.CachedDate = @YesterdayDate AND
                                (d.AlwaysFailingCount != 0 OR (d.PassedNowFailedCount - d.FailedLastTestCount) != 0)
                        ) b ON (a.TestCaseId = b.TestCaseId AND a.TestAreaCategoryId = b.TestAreaCategoryId)
                JOIN TestAreaCategories tac ON (a.TestAreaCategoryId = tac.Id)
                GROUP BY tac.Name
                UNION
                SELECT
                    tac.Name as 'Category',
                    SUM(a.AlwaysFailingCount) as 'ConsistentlyFailingToday',
                    SUM(a.StartedFailing) as 'StartedFailingToday',
                    SUM(b.AlwaysFailingCount) as 'ConsistentlyFailingYesterday',
                    SUM(b.StartedFailing) as 'StartedFailingYesterday'
                FROM (
                SELECT DISTINCT
                            ta.TestAreaCategoryId,
                            d.TestCaseId,
                            d.AlwaysFailingCount,
                            d.PassedNowFailedCount - d.FailedLastTestCount as 'StartedFailing'
                        FROM DailyScenarioResults d
                        JOIN TestAreaTestCases tatc ON (tatc.TestCase_Id = d.TestCaseId)
                        JOIN TestAreas ta ON (tatc.TestArea_Id = ta.Id)
                        WHERE
                            d.BranchId = @BranchId AND
                            d.CachedDate = @TodayDate AND
                            (d.AlwaysFailingCount != 0 OR (d.PassedNowFailedCount - d.FailedLastTestCount) != 0)
                    ) a
                RIGHT JOIN (
                            SELECT DISTINCT
                                ta.TestAreaCategoryId,
                                d.TestCaseId,
                                d.AlwaysFailingCount,
                                d.PassedNowFailedCount - d.FailedLastTestCount as 'StartedFailing'
                            FROM DailyScenarioResults d
                            JOIN TestAreaTestCases tatc ON (tatc.TestCase_Id = d.TestCaseId)
                            JOIN TestAreas ta ON (tatc.TestArea_Id = ta.Id)
                            WHERE
                                d.BranchId = @BranchId AND
                                d.CachedDate = @YesterdayDate AND
                                (d.AlwaysFailingCount != 0 OR (d.PassedNowFailedCount - d.FailedLastTestCount) != 0)
                        ) b ON (a.TestCaseId = b.TestCaseId AND a.TestAreaCategoryId = b.TestAreaCategoryId)
                JOIN TestAreaCategories tac ON (a.TestAreaCategoryId = tac.Id)
                GROUP BY tac.Name
                UNION
                SELECT
                    'Any Category' as 'Category',
                    SUM(a.AlwaysFailingCount) as 'ConsistentlyFailingToday',
                    SUM(a.StartedFailing) as 'StartedFailingToday',
                    SUM(b.AlwaysFailingCount) as 'ConsistentlyFailingYesterday',
                    SUM(b.StartedFailing) as 'StartedFailingYesterday'
                FROM (
                        SELECT
                            d.TestCaseId,
                            d.AlwaysFailingCount,
                            d.PassedNowFailedCount - d.FailedLastTestCount as 'StartedFailing'
                        FROM DailyScenarioResults d
                        WHERE
                            d.BranchId = @BranchId AND
                            d.CachedDate = @TodayDate AND
                            (d.AlwaysFailingCount != 0 OR (d.PassedNowFailedCount - d.FailedLastTestCount) != 0)
                    ) a
                LEFT JOIN (
                            SELECT
                                d.TestCaseId,
                                d.AlwaysFailingCount,
                                d.PassedNowFailedCount - d.FailedLastTestCount as 'StartedFailing'
                            FROM DailyScenarioResults d
                            WHERE
                                d.BranchId = @BranchId AND
                                d.CachedDate = @YesterdayDate AND
                                (d.AlwaysFailingCount != 0 OR (d.PassedNowFailedCount - d.FailedLastTestCount) != 0)
                        ) b ON (a.TestCaseId = b.TestCaseId)
                        UNION
                        SELECT
                    'Any Category' as 'Category',
                    SUM(a.AlwaysFailingCount) as 'ConsistentlyFailingToday',
                    SUM(a.StartedFailing) as 'StartedFailingToday',
                    SUM(b.AlwaysFailingCount) as 'ConsistentlyFailingYesterday',
                    SUM(b.StartedFailing) as 'StartedFailingYesterday'
                FROM (
                        SELECT
                            d.TestCaseId,
                            d.AlwaysFailingCount,
                            d.PassedNowFailedCount - d.FailedLastTestCount as 'StartedFailing'
                        FROM DailyScenarioResults d
                        WHERE
                            d.BranchId = @BranchId AND
                            d.CachedDate = @TodayDate AND
                            (d.AlwaysFailingCount != 0 OR (d.PassedNowFailedCount - d.FailedLastTestCount) != 0)
                    ) a
                RIGHT JOIN (
                            SELECT
                                d.TestCaseId,
                                d.AlwaysFailingCount,
                                d.PassedNowFailedCount - d.FailedLastTestCount as 'StartedFailing'
                            FROM DailyScenarioResults d
                            WHERE
                                d.BranchId = @BranchId AND
                                d.CachedDate = @YesterdayDate AND
                                (d.AlwaysFailingCount != 0 OR (d.PassedNowFailedCount - d.FailedLastTestCount) != 0)
                        ) b ON (a.TestCaseId = b.TestCaseId)
            ) r
ORDER BY
    r.ConsistentlyFailingToday DESC,
    r.StartedFailingToday DESC,
    r.ConsistentlyFailingYesterday DESC,
    r.StartedFailingYesterday DESC

我有这个结果:

Result Screenshot

这是我希望获得的实际结果:

Actual Result Screenshot

0 个答案:

没有答案