尝试创建其他唯一键以查询

时间:2019-03-25 15:35:53

标签: sql-server

我有一个表,该表包含两个字段,这些字段是唯一键,但是我不需要此表中的键,因为我需要该表中不包含的其他两个字段(Jda_sourcing)。我创建了一个查询,认为该查询可以满足我的需求,但是我不确定。我跑了,没有任何错误,但是想知道这是否可行。

这是一个很长的查询,所以我不会包括所有内容。我希望组合键是BrandNum和站点供应商。这些列不在Jda_sourcing表中。该项目是两个表的共同点。我不确定这是否可行,这就是为什么我要发布此问题。

SELECT COUNT(*),
       item,
       source,
       dest
FROM [dq].[jda_sourcing] AS js
WHERE [item] IN (SELECT [item]
                 FROM [dq].[jda_udt_item_local]
                 WHERE
                     --EXISTS  
                     [u_brand] IN (SELECT TOP 100
                                          itm.u_brand AS BrandNum
                                   FROM (SELECT lnk.material,
                                                loc.plant,
                                                loc.storagelocation,
                                                lnk.purchasingstatus,
                                                lnk.sellingstatus,
                                                lnk.sitevendor,
                                                CASE WHEN ssc.material IS NOT NULL THEN 'SSC' ELSE 'DIRECT' END AS source
                                         FROM scm.sap_marc AS lnk
                                              JOIN (SELECT DISTINCT
                                                           loc AS plant,
                                                           RIGHT(location_id, 4) AS storagelocation,
                                                           loc_type
                                                    FROM dq.static_location
                                                    WHERE loc IS NOT NULL
                                                      AND loc_status = 'A'
                                                      AND loc_type = 'FWD') AS loc ON loc.plant = lnk.plant
                                              LEFT JOIN scm.sap_mard AS ssc ON ssc.plant = loc.plant
                                                                           AND ssc.material = lnk.material
                                                                           AND ssc.sscvalidfrom <= CONVERT(varchar(8), GETDATE(), 112)
                                                                           AND ssc.sscvalidto >= CONVERT(varchar(8), GETDATE(), 112)
                                              LEFT JOIN scm.sap_zdt_mm_source AS src ON src.plant = lnk.plant
                                                                                    AND src.material = lnk.material
                                                                                    AND (lnk.sitevendor = src.vendor
                                                                                      OR ssc.material IS NOT NULL
                                                                                      OR LEN(src.vendor) = 4)
                                                                                    AND src.validfromdate <= CONVERT(varchar(8), GETDATE(), 112)
                                                                                    AND src.validtodate >= CONVERT(varchar(8), GETDATE(), 112)
                                         WHERE lnk.purchasingstatus IN ('P', 'U', 'W')
                                           AND src.material IS NULL) AS msrc
                                        LEFT JOIN dq.jda_udt_item_local AS itm ON itm.item = msrc.material
                                        LEFT JOIN scm.sap_mara AS ssn ON ssn.material = msrc.material
                                                                     AND ssn.supercedeeffdate <= GETDATE()
                                        LEFT JOIN (SELECT item,
                                                          dest,
                                                          tier1,
                                                          gs_source,
                                                          supplier,
                                                          otc_status,
                                                          u_inco_term
                                                   FROM [flmir-jdasqldv1].scdq.dq.jda_sku_sourcing AS SKUSRC
                                                        JOIN [flmir-jdasqldv1].scdq.dq.jda_udt_loc AS LOC ON LOC.loc = SKUSRC.tier1
                                                   WHERE tier1 <> ''
                                        --and otc_status in ('AA','AH','IN','IF','IH') 
                                        ) AS lss ON lss.item = msrc.material
                                                AND lss.dest = CAST(msrc.plant AS varchar) + '-' + CAST(msrc.storagelocation AS varchar)
                                        LEFT JOIN scm.ci_corpitem AS ci ON ci.item = msrc.material
                                        --left join [dq].[jda_sourcing]js on js.[item]=msrc.material 
                                        --and js.[dest]= cast(msrc.plant as varchar) + '-' + cast(msrc.storagelocation as varchar) 
                                        LEFT JOIN temp.lg_sourcing_xref AS lgs ON lgs.item = msrc.material
                                                                              AND lgs.plant = CAST(msrc.plant AS varchar)
                                                                              AND lgs.sloc = CAST(msrc.storagelocation AS varchar)
                                   WHERE ISNULL(itm.u_prod_class, 0) NOT IN (40, 85, 86, 94, 96, 99)
                                     AND ISNULL(itm.u_brand, 0) <> '9999'))
  AND LEN(source) = 13
GROUP BY item,
         source,
         dest
UNION ALL
SELECT COUNT(*),
       item,
       source,
       dest
FROM [dq].[jda_sourcing] AS js
WHERE [item] IN (SELECT [material]
                 FROM [scm].[sap_marc]
                 WHERE
                     --EXISTS  
                     [sitevendor] IN (SELECT TOP 100
                                             msrc.sitevendor AS SiteVendor
                                      FROM (SELECT lnk.material,
                                                   loc.plant,
                                                   loc.storagelocation,
                                                   lnk.purchasingstatus,
                                                   lnk.sellingstatus,
                                                   lnk.sitevendor,
                                                   CASE WHEN ssc.material IS NOT NULL THEN 'SSC' ELSE 'DIRECT' END AS source
                                            FROM scm.sap_marc AS lnk
                                                 JOIN (SELECT DISTINCT
                                                              loc AS plant,
                                                              RIGHT(location_id, 4) AS storagelocation,
                                                              loc_type
                                                       FROM dq.static_location
                                                       WHERE loc IS NOT NULL
                                                         AND loc_status = 'A'
                                                         AND loc_type = 'FWD') AS loc ON loc.plant = lnk.plant
                                                 LEFT JOIN scm.sap_mard AS ssc ON ssc.plant = loc.plant
                                                                              AND ssc.material = lnk.material
                                                                              AND ssc.sscvalidfrom <= CONVERT(varchar(8), GETDATE(), 112)
                                                                              AND ssc.sscvalidto >= CONVERT(varchar(8), GETDATE(), 112)
                                                 LEFT JOIN scm.sap_zdt_mm_source AS src ON src.plant = lnk.plant
                                                                                       AND src.material = lnk.material
                                                                                       AND (lnk.sitevendor = src.vendor
                                                                                         OR ssc.material IS NOT NULL
                                                                                         OR LEN(src.vendor) = 4)
                                                                                       AND src.validfromdate <= CONVERT(varchar(8), GETDATE(), 112)
                                                                                       AND src.validtodate >= CONVERT(varchar(8), GETDATE(), 112)
                                            WHERE lnk.purchasingstatus IN ('P', 'U', 'W')
                                              AND src.material IS NULL) AS msrc
                                           LEFT JOIN dq.jda_udt_item_local AS itm ON itm.item = msrc.material
                                           LEFT JOIN scm.sap_mara AS ssn ON ssn.material = msrc.material
                                                                        AND ssn.supercedeeffdate <= GETDATE()
                                           LEFT JOIN (SELECT item,
                                                             dest,
                                                             tier1,
                                                             gs_source,
                                                             supplier,
                                                             otc_status,
                                                             u_inco_term
                                                      FROM [flmir-jdasqldv1].scdq.dq.jda_sku_sourcing AS SKUSRC
                                                           JOIN [flmir-jdasqldv1].scdq.dq.jda_udt_loc AS LOC ON LOC.loc = SKUSRC.tier1
                                                      WHERE tier1 <> ''
                                           --and otc_status in ('AA','AH','IN','IF','IH') 
                                           ) AS lss ON lss.item = msrc.material
                                                   AND lss.dest = CAST(msrc.plant AS varchar) + '-' + CAST(msrc.storagelocation AS varchar)
                                           LEFT JOIN scm.ci_corpitem AS ci ON ci.item = msrc.material
                                           --left join [dq].[jda_sourcing]js on js.[item]=msrc.material 
                                           --and js.[dest]= cast(msrc.plant as varchar) + '-' + cast(msrc.storagelocation as varchar) 
                                           LEFT JOIN temp.lg_sourcing_xref AS lgs ON lgs.item = msrc.material
                                                                                 AND lgs.plant = CAST(msrc.plant AS varchar)
                                                                                 AND lgs.sloc = CAST(msrc.storagelocation AS varchar)
                                      WHERE ISNULL(itm.u_prod_class, 0) NOT IN (40, 85, 86, 94, 96, 99)
                                        AND ISNULL(itm.u_brand, 0) <> '9999'))
  AND LEN(source) = 13
GROUP BY item,
         source,
         dest;

1 个答案:

答案 0 :(得分:0)

稍作重构,您的代码就可以更清晰-像这样:

也许这也可以使您的问题更清晰,因为您可以按名称引用子查询?

WITH lss AS
(
  SELECT item,
         dest,
         tier1,
         gs_source,
         supplier,
         otc_status,
         u_inco_term
  FROM [flmir-jdasqldv1].scdq.dq.jda_sku_sourcing AS SKUSRC
  JOIN [flmir-jdasqldv1].scdq.dq.jda_udt_loc AS LOC ON LOC.loc = SKUSRC.tier1
  WHERE tier1 <> ''
), loc as
(
  SELECT DISTINCT
    loc AS plant,
    RIGHT(location_id, 4) AS storagelocation,
    loc_type
  FROM dq.static_location
  WHERE loc IS NOT NULL AND loc_status = 'A' AND loc_type = 'FWD'
), msrc AS
(
  SELECT lnk.material,
           loc.plant,
           loc.storagelocation,
           lnk.purchasingstatus,
           lnk.sellingstatus,
           lnk.sitevendor,
           CASE WHEN ssc.material IS NOT NULL THEN 'SSC' ELSE 'DIRECT' END AS source
    FROM scm.sap_marc AS lnk
    JOIN loc ON loc.plant = lnk.plant
    LEFT JOIN scm.sap_mard AS ssc ON ssc.plant = loc.plant
                                      AND ssc.material = lnk.material
                                      AND ssc.sscvalidfrom <= CONVERT(varchar(8), GETDATE(), 112)
                                      AND ssc.sscvalidto >= CONVERT(varchar(8), GETDATE(), 112)
    LEFT JOIN scm.sap_zdt_mm_source AS src ON src.plant = lnk.plant
                                               AND src.material = lnk.material
                                               AND (lnk.sitevendor = src.vendor
                                                 OR ssc.material IS NOT NULL
                                                 OR LEN(src.vendor) = 4)
                                               AND src.validfromdate <= CONVERT(varchar(8), GETDATE(), 112)
                                               AND src.validtodate >= CONVERT(varchar(8), GETDATE(), 112)
    WHERE lnk.purchasingstatus IN ('P', 'U', 'W') AND src.material IS NULL
), t100brand AS
(
  SELECT TOP 100
           itm.u_brand AS BrandNum
  FROM  msrc
  LEFT JOIN dq.jda_udt_item_local AS itm ON itm.item = msrc.material
  LEFT JOIN scm.sap_mara AS ssn ON ssn.material = msrc.material
                                     AND ssn.supercedeeffdate <= GETDATE()
  LEFT JOIN  lss ON lss.item = msrc.material AND lss.dest = CAST(msrc.plant AS varchar) + '-' + CAST(msrc.storagelocation AS varchar)
  LEFT JOIN scm.ci_corpitem AS ci ON ci.item = msrc.material
         --left join [dq].[jda_sourcing]js on js.[item]=msrc.material 
         --and js.[dest]= cast(msrc.plant as varchar) + '-' + cast(msrc.storagelocation as varchar) 
  LEFT JOIN temp.lg_sourcing_xref AS lgs ON lgs.item = msrc.material
                                               AND lgs.plant = CAST(msrc.plant AS varchar)
                                               AND lgs.sloc = CAST(msrc.storagelocation AS varchar)
  WHERE ISNULL(itm.u_prod_class, 0) NOT IN (40, 85, 86, 94, 96, 99)
      AND ISNULL(itm.u_brand, 0) <> '9999')
), t100branditem AS
(
  SELECT [item]
  FROM [dq].[jda_udt_item_local]
  JOIN t100brand t100 on u_brand = t100.u_brand)
), t100vender AS
(
  SELECT TOP 100
           msrc.sitevendor AS SiteVendor
  FROM msrc
  LEFT JOIN dq.jda_udt_item_local AS itm ON itm.item = msrc.material
  LEFT JOIN scm.sap_mara AS ssn ON ssn.material = msrc.material
                               AND ssn.supercedeeffdate <= GETDATE()
  LEFT JOIN lss ON lss.item = msrc.material
          AND lss.dest = CAST(msrc.plant AS varchar) + '-' + CAST(msrc.storagelocation AS varchar)
  LEFT JOIN scm.ci_corpitem AS ci ON ci.item = msrc.material
  --left join [dq].[jda_sourcing]js on js.[item]=msrc.material 
  --and js.[dest]= cast(msrc.plant as varchar) + '-' + cast(msrc.storagelocation as varchar) 
  LEFT JOIN temp.lg_sourcing_xref AS lgs ON lgs.item = msrc.material
                                        AND lgs.plant = CAST(msrc.plant AS varchar)
                                        AND lgs.sloc = CAST(msrc.storagelocation AS varchar)
  WHERE ISNULL(itm.u_prod_class, 0) NOT IN (40, 85, 86, 94, 96, 99)
    AND ISNULL(itm.u_brand, 0) <> '9999')

), t100materialvendor as
(
  SELECT [material]
  FROM [scm].[sap_marc]
  join t100vender v on sitevendor = v.sitevendor  
  where LEN(source) = 13
)

    SELECT COUNT(*),
           item,
           source,
           dest
    FROM [dq].[jda_sourcing] AS js
    join t100branditem bi ON bi.item = js.item
    where LEN(source) = 13
    GROUP BY item, source, dest

    UNION ALL

    SELECT COUNT(*),
           item,
           source,
           dest
    FROM [dq].[jda_sourcing] AS js
    join t100materialvendor mv ON mv.material = js.item
    GROUP BY item, source, dest;