如何优化此UNION SQL查询?

时间:2018-09-12 17:39:26

标签: sql sql-server

我有一个如下查询:

+11

如果可能,我想删除UNION ALL。有人知道如何对其进行优化吗?

1 个答案:

答案 0 :(得分:0)

删除了所有工会

SELECT DISTINCT * FROM ( 
                    SELECT  FundId AS Id,
                            PeriodYearMonth
                    FROM [Fund.Period] F
                    INNER JOIN (
                                SELECT * FROM (
                                                SELECT  FundId as Id, 
                                                MIN(PeriodYearMonth) AS MinPeriodYearMonth,MAX(PeriodYearMonth) + 1 as PeriodYearMonth
                                                FROM (
                                                    SELECT  FundId,
                                                            PeriodYearMonth,
                                                            PublishedOn
                                                    FROM    [Fund.Period] FP

                                                    ) FP WHERE PublishedOn IS NULL 
                                                    GROUP BY FundId
                                                ) MFP
                                )  FP ON F.FundId = FP.Id AND (F.PeriodYearMonth = FP.MinPeriodYearMonth OR (f.PeriodYearMonth +1) = FP.MinPeriodYearMonth) 
                       ) FP