优化 SQL 查询以与 Access 一起使用(直通)

问题描述 投票:0回答:1

我正在尝试优化我构建的 SQL 查询,我将其用作 Access 中的直通查询。目前,此查询最多可能需要 10 分钟才能完成,我真的很想缩短这个时间。我意识到问题的很大一部分是我在其他内部查询中有很多内部查询。这是因为我的公司不会授予我创建任何永久表的权限。

我有一个此查询的版本,其中我将大量“混乱”的内部查询转移到临时表并调用它,但这不能作为 Access 的直通查询。除非它在访问数据库打开时的每个会话期间运行。

我真的很想尝试找到一种方法,让这项工作就像 SQL 中的直接查询一样,我可以将其用作 Access 中的直通。

这是 SQL:

SET NOCOUNT ON;
IF OBJECT_ID('tempdb..#catcov1') IS NOT NULL
    DROP TABLE #catcov1;
SELECT a.mailyear,
       a.offer,
       a.description,
       a.InternetActiveDate,
       a.FirstReleaseMailed,
       a.season_id,
       a.offer_type,
       a.price_type,
       b.Prefix_Quarter,
       a.CompanyCode AS Brand_Code,
       CASE WHEN CompanyCode = '1' THEN 'Company 1'
           WHEN CompanyCode = '2' THEN 'Company 2'
           WHEN CompanyCode = '3' THEN 'Company 3'
           WHEN CompanyCode = '4' THEN 'Company 4'
           WHEN CompanyCode = '5' THEN 'Company 5'
           WHEN CompanyCode = '6' THEN 'Company 6'
           WHEN CompanyCode = '7' THEN 'Company 7'
           WHEN CompanyCode = '8' THEN 'Company 8'
           WHEN CompanyCode = '9' THEN 'Company 9'
           ELSE CompanyCode
       END AS Brand_Name
INTO #catcov1
FROM CatCov a
     JOIN offer_placement b ON (a.Offer = b.offer)
                           AND (a.MailYear = b.offeryear)
WHERE (CASE WHEN Description LIKE '%test%' THEN 0
           WHEN Description LIKE '%Price%A%' THEN 0
           WHEN Description LIKE '%Price%B%' THEN 0
           WHEN Description LIKE '%Amazon.com%' THEN 0
           WHEN Description LIKE '%EB Discount%' THEN 0
           ELSE 1
       END) = 1
  AND a.Offer_Type IN ('Catalog', 'Insert', 'Kicker', 'Statement Insert', 'Bangtail', 'Onsert', 'Outside Ad', 'Blow In');

SELECT DISTINCT
       a.PackNum,
       a.Description,
       a.CatID,
       CONCAT(a.PackNum, a.CatID) AS [Key],
       CONCAT(a.rd, a.sfc) AS RDSFC,
       b.Season_Id,
       b.Brand_Name,
       b.Description AS CatCovDesc,
       b.FirstReleaseMailed AS MailDate,
       b.Prefix_Quarter,
       a.retone AS Retail,
       a.ret2 AS EBRetail,
       a.OriginalRetail,
       a.DiscountReasonCode AS DRC,
       hr.LastHigh AS LastHigh,
       ct.New_Retail1 AS New_Retail,
       ct.EB_High,
       ct.Catalog,
       ct.Original_Retail,
       ct.DRC AS New_DRC,
       ct.Requested,
       ct.Request_Completed_Date,
       ct.Notes,
       ct.Season,
       m.Merch AS MerchMgr,
       t.MerchAssistants
FROM PIC704Current a
     JOIN #catcov1 b ON (a.CatID = b.Offer)
                    AND (a.year = b.mailyear)
     FULL JOIN CIDRetailChangeTracking ct ON (a.PackNum = ct.Pack)
                                         AND (a.CatID = ct.Catalog)
                                         AND (b.Season_Id = ct.Season)
     FULL JOIN MerchantCodes m ON (a.TwoDigitMerchCode = m.MerchCode)
     JOIN SupplyChain_Misc.flex_tbl_merchcode t ON (m.MerchCode = t.MerchCode)
     FULL JOIN (SELECT DISTINCT
                       a.PackNum,
                       MAX(q.IAD) AS [Last High Date],
                       MAX(q.MaxRetail) AS MaxRetail,
                       MIN(q.MinRetail) AS MinRetail,
                       q.LastHigh
                FROM PIC704Current a
                     JOIN #catcov1 b ON (a.CatID = b.Offer)
                                    AND (a.Year = b.MailYear)
                     JOIN (SELECT b.PackNum,
                                  MAX(CAST(InternetActiveDate AS date)) AS IAD,
                                  CASE WHEN retone >= ret2
                                        AND retone >= originalretail THEN Retone
                                      WHEN Ret2 >= RetOne
                                       AND Ret2 >= originalretail THEN Ret2
                                      WHEN Originalretail >= ret2
                                       AND Originalretail >= Retone THEN Originalretail
                                  END AS MaxRetail,
                                  CASE WHEN retone >= ret2
                                        AND retone >= originalretail THEN Retone
                                      WHEN Ret2 >= RetOne
                                       AND Ret2 >= originalretail THEN Ret2
                                      WHEN Originalretail >= ret2
                                       AND Originalretail >= Retone THEN Originalretail
                                  END AS MinRetail,
                                  LH.LastHigh
                           FROM #catcov1 a
                                JOIN PIC704Current b ON (a.offer = b.CatID)
                                                    AND (a.MailYear = b.year)
                                FULL JOIN (SELECT PackNum,
                                                  MAX(CAST(firstreleasemailed AS date)) AS Maildate,
                                                  CASE WHEN MAX(a.RetOne) >= MAX(a.ret2)
                                                        AND MAX(a.RetOne) >= MAX(a.ORIGINALRETAIL) THEN MAX(a.RetOne)
                                                      WHEN MAX(a.ret2) >= MAX(a.RetOne)
                                                       AND MAX(a.ret2) >= MAX(a.ORIGINALRETAIL) THEN MAX(a.ret2)
                                                      WHEN MAX(a.ORIGINALRETAIL) >= MAX(a.ret2)
                                                       AND MAX(a.ORIGINALRETAIL) >= MAX(a.RetOne) THEN MAX(a.ORIGINALRETAIL)
                                                  END AS LastHigh
                                           FROM pic704current a
                                                JOIN #catcov1 b ON (a.CatID = b.Offer)
                                                               AND (a.Year = b.MailYear)
                                                JOIN (SELECT DISTINCT
                                                             b.offer,
                                                             b.mailyear
                                                      FROM #catcov1 b) q ON (a.CatID = q.offer)
                                                                        AND (a.Year = q.MailYear)
                                           WHERE ( CAST(b.firstreleasemailed AS date) <= GETDATE()
                                                OR b.FirstReleaseMailed IS NULL)
                                           GROUP BY PackNum) LH ON (b.PackNum = LH.PackNum)
                           WHERE (CAST(a.InternetActiveDate AS date) = (SELECT MAX(CAST(InternetActiveDate AS date))
                                                                        FROM #catcov1 c2
                                                                        WHERE (a.offer = c2.Offer)
                                                                          AND (a.MailYear = c2.MailYear)))
                             AND a.Season_Id = 'F24'
                           GROUP BY b.PackNum,
                                    RetOne,
                                    Ret2,
                                    ORIGINALRETAIL,
                                    LH.LastHigh) q ON (a.PackNum = q.PackNum)
                                                  AND (b.InternetActiveDate = q.IAD)
                WHERE a.RD >= 50
                  AND (CAST(b.InternetActiveDate AS date) = (SELECT MAX(CAST(InternetActiveDate AS date))
                                                             FROM #catcov1 c2
                                                             WHERE (b.offer = c2.Offer)
                                                               AND (b.MailYear = c2.MailYear)))
                GROUP BY a.PackNum,
                         LastHigh) hr ON (a.PackNum = hr.PackNum)
WHERE RD >= 50
  AND b.Season_Id = 'F24'
  AND ( CAST(ct.Request_Completed_Date AS datetime) = (SELECT MAX(CAST(Request_Completed_Date AS datetime))
                                                       FROM CIDRetailChangeTracking c2
                                                       WHERE ct.[key] = c2.[key])
     OR ct.Request_Completed_Date IS NULL)
GROUP BY a.PackNum,
         a.Description,
         a.CatID,
         a.rd,
         a.sfc,
         b.Season_Id,
         b.Brand_Name,
         b.Description,
         b.FirstReleaseMailed,
         b.Prefix_Quarter,
         hr.MaxRetail,
         hr.MinRetail,
         hr.LastHigh,
         a.retone,
         a.ret2,
         a.OriginalRetail,
         a.DiscountReasonCode,
         ct.New_Retail1,
         ct.EB_High,
         ct.Catalog,
         ct.Original_Retail,
         ct.DRC,
         ct.Requested,
         ct.Request_Completed_Date,
         ct.Notes,
         ct.Season,
         m.Merch,
         t.MerchAssistants
HAVING hr.MaxRetail <> hr.MinRetail
ORDER BY Packnum,
         MailDate;
sql sql-server
1个回答
0
投票

下面应该稍微整理一下代码。

此外,所有日期的数据类型和格式是什么。迄今为止的所有转换和日期时间都没有帮助。

SET NOCOUNT ON;
IF OBJECT_ID('tempdb..#catcov1') IS NOT NULL
    DROP TABLE #catcov1;
SELECT a.mailyear,
       a.offer,
       a.description,
       a.InternetActiveDate,
       a.FirstReleaseMailed,
       a.season_id,
       a.offer_type,
       a.price_type,
       b.Prefix_Quarter,
       a.CompanyCode AS Brand_Code,
       CASE WHEN CompanyCode BETWEEN '1' AND '9' THEN CONCAT('Company ', CompanyCode)
           ELSE CompanyCode
       END AS Brand_Name
INTO #catcov1
FROM CatCov a
     JOIN offer_placement b ON (a.Offer = b.offer)
                           AND (a.MailYear = b.offeryear)
WHERE Description NOT LIKE '%test%'
  AND Description NOT LIKE '%Price%A%'
  AND Description NOT LIKE '%Price%B%'
  AND Description NOT LIKE '%Amazon.com%'
  AND Description NOT LIKE '%EB Discount%'
  AND a.Offer_Type IN ('Catalog', 'Insert', 'Kicker', 'Statement Insert', 'Bangtail', 'Onsert', 'Outside Ad', 'Blow In');

SELECT DISTINCT
       a.PackNum,
       a.Description,
       a.CatID,
       CONCAT(a.PackNum, a.CatID) AS [Key],
       CONCAT(a.rd, a.sfc) AS RDSFC,
       b.Season_Id,
       b.Brand_Name,
       b.Description AS CatCovDesc,
       b.FirstReleaseMailed AS MailDate,
       b.Prefix_Quarter,
       a.retone AS Retail,
       a.ret2 AS EBRetail,
       a.OriginalRetail,
       a.DiscountReasonCode AS DRC,
       hr.LastHigh AS LastHigh,
       ct.New_Retail1 AS New_Retail,
       ct.EB_High,
       ct.Catalog,
       ct.Original_Retail,
       ct.DRC AS New_DRC,
       ct.Requested,
       ct.Request_Completed_Date,
       ct.Notes,
       ct.Season,
       m.Merch AS MerchMgr,
       t.MerchAssistants
FROM PIC704Current a
     JOIN #catcov1 b ON (a.CatID = b.Offer)
                    AND (a.year = b.mailyear)
     FULL JOIN CIDRetailChangeTracking ct ON (a.PackNum = ct.Pack)
                                         AND (a.CatID = ct.Catalog)
                                         AND (b.Season_Id = ct.Season)
     FULL JOIN MerchantCodes m ON (a.TwoDigitMerchCode = m.MerchCode)
     JOIN SupplyChain_Misc.flex_tbl_merchcode t ON (m.MerchCode = t.MerchCode)
     FULL JOIN (SELECT DISTINCT
                       a.PackNum,
                       MAX(q.IAD) AS [Last High Date],
                       MAX(q.MaxRetail) AS MaxRetail,
                       MIN(q.MinRetail) AS MinRetail,
                       q.LastHigh
                FROM PIC704Current a
                     JOIN #catcov1 b ON (a.CatID = b.Offer)
                                    AND (a.Year = b.MailYear)
                     JOIN (SELECT b.PackNum,
                                  MAX(CAST(InternetActiveDate AS date)) AS IAD,
                                  CASE WHEN retone >= ret2
                                        AND retone >= originalretail THEN Retone
                                      WHEN Ret2 >= RetOne
                                       AND Ret2 >= originalretail THEN Ret2
                                      WHEN Originalretail >= ret2
                                       AND Originalretail >= Retone THEN Originalretail
                                  END AS MaxRetail,
                                  CASE WHEN retone >= ret2
                                        AND retone >= originalretail THEN Retone
                                      WHEN Ret2 >= RetOne
                                       AND Ret2 >= originalretail THEN Ret2
                                      WHEN Originalretail >= ret2
                                       AND Originalretail >= Retone THEN Originalretail
                                  END AS MinRetail,
                                  LH.LastHigh
                           FROM #catcov1 a
                                JOIN PIC704Current b ON (a.offer = b.CatID)
                                                    AND (a.MailYear = b.year)
                                FULL JOIN (SELECT PackNum,
                                                  MAX(CAST(firstreleasemailed AS date)) AS Maildate,
                                                  CASE WHEN MAX(a.RetOne) >= MAX(a.ret2)
                                                        AND MAX(a.RetOne) >= MAX(a.ORIGINALRETAIL) THEN MAX(a.RetOne)
                                                      WHEN MAX(a.ret2) >= MAX(a.RetOne)
                                                       AND MAX(a.ret2) >= MAX(a.ORIGINALRETAIL) THEN MAX(a.ret2)
                                                      WHEN MAX(a.ORIGINALRETAIL) >= MAX(a.ret2)
                                                       AND MAX(a.ORIGINALRETAIL) >= MAX(a.RetOne) THEN MAX(a.ORIGINALRETAIL)
                                                  END AS LastHigh
                                           FROM pic704current a
                                                JOIN #catcov1 b ON (a.CatID = b.Offer)
                                                               AND (a.Year = b.MailYear)
                                                JOIN (SELECT DISTINCT
                                                             b.offer,
                                                             b.mailyear
                                                      FROM #catcov1 b) q ON (a.CatID = q.offer)
                                                                        AND (a.Year = q.MailYear)
                                           WHERE ( CAST(b.firstreleasemailed AS date) <= GETDATE()
                                                OR b.FirstReleaseMailed IS NULL)
                                           GROUP BY PackNum) LH ON (b.PackNum = LH.PackNum)
                           WHERE (CAST(a.InternetActiveDate AS date) = (SELECT MAX(CAST(InternetActiveDate AS date))
                                                                        FROM #catcov1 c2
                                                                        WHERE (a.offer = c2.Offer)
                                                                          AND (a.MailYear = c2.MailYear)))
                             AND a.Season_Id = 'F24'
                           GROUP BY b.PackNum,
                                    RetOne,
                                    Ret2,
                                    ORIGINALRETAIL,
                                    LH.LastHigh) q ON (a.PackNum = q.PackNum)
                                                  AND (b.InternetActiveDate = q.IAD)
                WHERE a.RD >= 50
                  AND (CAST(b.InternetActiveDate AS date) = (SELECT MAX(CAST(InternetActiveDate AS date))
                                                             FROM #catcov1 c2
                                                             WHERE (b.offer = c2.Offer)
                                                               AND (b.MailYear = c2.MailYear)))
                GROUP BY a.PackNum,
                         LastHigh) hr ON (a.PackNum = hr.PackNum)
WHERE RD >= 50
  AND b.Season_Id = 'F24'
  AND ( CAST(ct.Request_Completed_Date AS datetime) = (SELECT MAX(CAST(Request_Completed_Date AS datetime))
                                                       FROM CIDRetailChangeTracking c2
                                                       WHERE ct.[key] = c2.[key])
     OR ct.Request_Completed_Date IS NULL)
     AND hr.MaxRetail <> hr.MinRetail
ORDER BY Packnum,
         MailDate;
© www.soinside.com 2019 - 2024. All rights reserved.