无法摆脱重复和总和数量

时间:2015-04-30 13:34:10

标签: sql-server-2012

我正在研究一个SQL语句,以便创建一个cron作业来获取有关我们产品库存和退货的csv文件。然而,当我运行它时,即使我总结了数量,我仍然在sku代码上得到重复,看起来它不是总和返回数量。

请参阅下面的代码,非常感谢。

SELECT DISTINCT
    p.part
,   p.identifier
,   p.descr
,   supl.full_name AS supplier
,   pgrp.descr AS groupd
,   fam.descr AS family
,   pt.part_cost AS Cost_GBP
,   ISNULL((
            SELECT
                SUM(qty - ISNULL(qty_received, 0))
            FROM
                pord_line AS pl
            WHERE
                status < 31
                AND p.part = pl.part
                AND qty - ISNULL(qty_received, 0) > 0
           ), 0) AS OnPO
,   (
     SELECT TOP 1
        pb.bcode
     FROM
        part_barc pb WITH (NOLOCK)
     WHERE
        pb.part = p.part
        AND pb.bcode LIKE '5%'
        AND LEN(pb.bcode) = 13
     ORDER BY
        pb.last_updated DESC
    ) AS EAN
,   (
     SELECT TOP 1
        v.price
     FROM
        vprice_list v
     WHERE
        v.part = p.part
        AND v.volume = 1
        AND v.price_list = 'WEB UK'
    ) AS RRP_GBP
,   (
     SELECT TOP 1
        v.price
     FROM
        vprice_list v
     WHERE
        v.part = p.part
        AND v.volume = 1
        AND v.price_list = 'WEB US'
    ) AS RRP_USD
,   p.status
,   p.obs_code
,   (CASE WHEN p.obs_code = 0 THEN 0
          ELSE 100
     END) AS obslink
,   ISNULL(RTRIM(dbo.[fn_get_attr_value]('WEB', 'TEXT2', p.part, 1)), '') AS long_descr
,   t.Main_Store_Total_Stock
,   t.Main_Store_Allocated
,   f.Assay_Office_Total_Stock
,   f.Assay_Office_Allocated
,   g.Consigment_Stock_Total_Stock
,   g.Consigment_Stock_Allocated
,   h.Customer_Returns_Total_Stock
,   h.Customer_Returns_Allocated
,   j.Despatch_Total_Stock
,   j.Despatch_Allocated
,   k.Goods_in_Total_Stock
,   k.Goods_in_Allocated
,   l.Harrods_Total_Stock
,   l.Harrods_Allocated
,   m.Inspection_Total_Stock
,   m.Inspection_Allocated
,   n.Liberty_Total_Stock
,   n.Liberty_Allocated
,   o.Repair_Store_Total_Stock
,   o.Repair_Store_Allocated
,   q.Return_from_Supplier_Total_Stock
,   q.Return_from_Supplier_Allocated
,   r.Selfridges_Trafford_Total_Stock
,   r.Selfridges_Trafford_Allocated
,   u.Selfridges_Oxford_Total_Stock
,   u.Selfridges_Oxford_Allocated
,   w.Supplier_Returns_Total_Stock
,   w.Supplier_Returns_Allocated
,   x.UK_Press_Total_Stock
,   x.UK_Press_Allocated
,   y.US_Press_Total_Stock
,   y.US_Press_Allocated
FROM
    part p
    LEFT JOIN part_plan pp1
    ON pp1.part = p.part
    LEFT JOIN part_pnar pnar
    ON pnar.part = p.part
    LEFT JOIN pgrp
    ON pgrp.prod_group = p.prod_group
    LEFT JOIN faml_pgrp fam
    ON fam.prod_family = pgrp.prod_family
    LEFT JOIN sgrp
    ON sgrp.sales_group = p.sales_group
    LEFT JOIN supl
    ON supl.supplier = pp1.prefer_supplier
    LEFT JOIN part_trad pt
    ON p.part = pt.part
    LEFT JOIN part_bins pp
    ON p.part = pp.part
    LEFT JOIN (
               SELECT
                dbo.part.part AS Part
               ,SUM(dbo.part_bins.qty) AS Main_Store_Total_Stock
               ,dbo.stor.descr AS Stor
               ,dbo.part_bins.allocated_qty AS Main_Store_Allocated
               FROM
                dbo.part
                INNER JOIN dbo.part_bins
                ON dbo.part.part = dbo.part_bins.part
                INNER JOIN dbo.stor
                ON dbo.part_bins.store = dbo.stor.store
                   AND dbo.stor.descr = 'Main Store'
               GROUP BY
                dbo.part.part
               ,dbo.stor.descr
               ,dbo.part_bins.allocated_qty
              ) AS t
    ON p.part = t.Part
    LEFT JOIN (
               SELECT
                dbo.part.part AS Part
               ,SUM(dbo.part_bins.qty) AS Assay_Office_Total_Stock
               ,dbo.stor.descr AS Stor
               ,dbo.part_bins.allocated_qty AS Assay_Office_Allocated
               FROM
                dbo.part
                INNER JOIN dbo.part_bins
                ON dbo.part.part = dbo.part_bins.part
                INNER JOIN dbo.stor
                ON dbo.part_bins.store = dbo.stor.store
                   AND dbo.stor.descr = 'Assay Office'
               GROUP BY
                dbo.part.part
               ,dbo.stor.descr
               ,dbo.part_bins.allocated_qty
              ) AS f
    ON p.part = f.Part
    LEFT JOIN (
               SELECT
                dbo.part.part AS Part
               ,SUM(dbo.part_bins.qty) AS Consigment_Stock_Total_Stock
               ,dbo.stor.descr AS Stor
               ,dbo.part_bins.allocated_qty AS Consigment_Stock_Allocated
               FROM
                dbo.part
                INNER JOIN dbo.part_bins
                ON dbo.part.part = dbo.part_bins.part
                INNER JOIN dbo.stor
                ON dbo.part_bins.store = dbo.stor.store
                   AND dbo.stor.descr = 'Consigment Stock'
               GROUP BY
                dbo.part.part
               ,dbo.stor.descr
               ,dbo.part_bins.allocated_qty
              ) AS g
    ON p.part = g.Part
    LEFT JOIN (
               SELECT
                dbo.part.part AS Part
               ,SUM(dbo.part_bins.qty) AS Customer_Returns_Total_Stock
               ,dbo.stor.descr AS Stor
               ,dbo.part_bins.allocated_qty AS Customer_Returns_Allocated
               FROM
                dbo.part
                INNER JOIN dbo.part_bins
                ON dbo.part.part = dbo.part_bins.part
                INNER JOIN dbo.stor
                ON dbo.part_bins.store = dbo.stor.store
                   AND dbo.stor.descr = 'Customer Returns'
               GROUP BY
                dbo.part.part
               ,dbo.stor.descr
               ,dbo.part_bins.allocated_qty
              ) AS h
    ON p.part = h.Part
    LEFT JOIN (
               SELECT
                dbo.part.part AS Part
               ,SUM(dbo.part_bins.qty) AS Despatch_Total_Stock
               ,dbo.stor.descr AS Stor
               ,dbo.part_bins.allocated_qty AS Despatch_Allocated
               FROM
                dbo.part
                INNER JOIN dbo.part_bins
                ON dbo.part.part = dbo.part_bins.part
                INNER JOIN dbo.stor
                ON dbo.part_bins.store = dbo.stor.store
                   AND dbo.stor.descr = 'Despatch'
               GROUP BY
                dbo.part.part
               ,dbo.stor.descr
               ,dbo.part_bins.allocated_qty
              ) AS j
    ON p.part = j.Part
    LEFT JOIN (
               SELECT
                dbo.part.part AS Part
               ,SUM(dbo.part_bins.qty) AS Goods_in_Total_Stock
               ,dbo.stor.descr AS Stor
               ,dbo.part_bins.allocated_qty AS Goods_in_Allocated
               FROM
                dbo.part
                INNER JOIN dbo.part_bins
                ON dbo.part.part = dbo.part_bins.part
                INNER JOIN dbo.stor
                ON dbo.part_bins.store = dbo.stor.store
                   AND dbo.stor.descr = 'Goods In'
               GROUP BY
                dbo.part.part
               ,dbo.stor.descr
               ,dbo.part_bins.allocated_qty
              ) AS k
    ON p.part = k.Part
    LEFT JOIN (
               SELECT
                dbo.part.part AS Part
               ,SUM(dbo.part_bins.qty) AS Harrods_Total_Stock
               ,dbo.stor.descr AS Stor
               ,dbo.part_bins.allocated_qty AS Harrods_Allocated
               FROM
                dbo.part
                INNER JOIN dbo.part_bins
                ON dbo.part.part = dbo.part_bins.part
                INNER JOIN dbo.stor
                ON dbo.part_bins.store = dbo.stor.store
                   AND dbo.stor.descr = 'Harrods'
               GROUP BY
                dbo.part.part
               ,dbo.stor.descr
               ,dbo.part_bins.allocated_qty
              ) AS l
    ON p.part = l.Part
    LEFT JOIN (
               SELECT
                dbo.part.part AS Part
               ,SUM(dbo.part_bins.qty) AS Inspection_Total_Stock
               ,dbo.stor.descr AS Stor
               ,dbo.part_bins.allocated_qty AS Inspection_Allocated
               FROM
                dbo.part
                INNER JOIN dbo.part_bins
                ON dbo.part.part = dbo.part_bins.part
                INNER JOIN dbo.stor
                ON dbo.part_bins.store = dbo.stor.store
                   AND dbo.stor.descr = 'Inspection'
               GROUP BY
                dbo.part.part
               ,dbo.stor.descr
               ,dbo.part_bins.allocated_qty
              ) AS m
    ON p.part = m.Part
    LEFT JOIN (
               SELECT
                dbo.part.part AS Part
               ,SUM(dbo.part_bins.qty) AS Liberty_Total_Stock
               ,dbo.stor.descr AS Stor
               ,dbo.part_bins.allocated_qty AS Liberty_Allocated
               FROM
                dbo.part
                INNER JOIN dbo.part_bins
                ON dbo.part.part = dbo.part_bins.part
                INNER JOIN dbo.stor
                ON dbo.part_bins.store = dbo.stor.store
                   AND dbo.stor.descr = 'Liberty'
               GROUP BY
                dbo.part.part
               ,dbo.stor.descr
               ,dbo.part_bins.allocated_qty
              ) AS n
    ON p.part = n.Part
    LEFT JOIN (
               SELECT
                dbo.part.part AS Part
               ,SUM(dbo.part_bins.qty) AS Repair_Store_Total_Stock
               ,dbo.stor.descr AS Stor
               ,dbo.part_bins.allocated_qty AS Repair_Store_Allocated
               FROM
                dbo.part
                INNER JOIN dbo.part_bins
                ON dbo.part.part = dbo.part_bins.part
                INNER JOIN dbo.stor
                ON dbo.part_bins.store = dbo.stor.store
                   AND dbo.stor.descr = 'Repair Store'
               GROUP BY
                dbo.part.part
               ,dbo.stor.descr
               ,dbo.part_bins.allocated_qty
              ) AS o
    ON p.part = o.Part
    LEFT JOIN (
               SELECT
                dbo.part.part AS Part
               ,SUM(dbo.part_bins.qty) AS Return_from_Supplier_Total_Stock
               ,dbo.stor.descr AS Stor
               ,dbo.part_bins.allocated_qty AS Return_from_Supplier_Allocated
               FROM
                dbo.part
                INNER JOIN dbo.part_bins
                ON dbo.part.part = dbo.part_bins.part
                INNER JOIN dbo.stor
                ON dbo.part_bins.store = dbo.stor.store
                   AND dbo.stor.descr = 'Return from Supplier'
               GROUP BY
                dbo.part.part
               ,dbo.stor.descr
               ,dbo.part_bins.allocated_qty
              ) AS q
    ON p.part = q.Part
    LEFT JOIN (
               SELECT
                dbo.part.part AS Part
               ,SUM(dbo.part_bins.qty) AS Selfridges_Trafford_Total_Stock
               ,dbo.stor.descr AS Stor
               ,dbo.part_bins.allocated_qty AS Selfridges_Trafford_Allocated
               FROM
                dbo.part
                INNER JOIN dbo.part_bins
                ON dbo.part.part = dbo.part_bins.part
                INNER JOIN dbo.stor
                ON dbo.part_bins.store = dbo.stor.store
                   AND dbo.stor.descr = 'Selfidges Trafford'
               GROUP BY
                dbo.part.part
               ,dbo.stor.descr
               ,dbo.part_bins.allocated_qty
              ) AS r
    ON p.part = r.Part
    LEFT JOIN (
               SELECT
                dbo.part.part AS Part
               ,SUM(dbo.part_bins.qty) AS Selfridges_Oxford_Total_Stock
               ,dbo.stor.descr AS Stor
               ,dbo.part_bins.allocated_qty AS Selfridges_Oxford_Allocated
               FROM
                dbo.part
                INNER JOIN dbo.part_bins
                ON dbo.part.part = dbo.part_bins.part
                INNER JOIN dbo.stor
                ON dbo.part_bins.store = dbo.stor.store
                   AND dbo.stor.descr = 'Selfridges Oxford'
               GROUP BY
                dbo.part.part
               ,dbo.stor.descr
               ,dbo.part_bins.allocated_qty
              ) AS u
    ON p.part = u.Part
    LEFT JOIN (
               SELECT
                dbo.part.part AS Part
               ,SUM(dbo.part_bins.qty) AS Supplier_Returns_Total_Stock
               ,dbo.stor.descr AS Stor
               ,dbo.part_bins.allocated_qty AS Supplier_Returns_Allocated
               FROM
                dbo.part
                INNER JOIN dbo.part_bins
                ON dbo.part.part = dbo.part_bins.part
                INNER JOIN dbo.stor
                ON dbo.part_bins.store = dbo.stor.store
                   AND dbo.stor.descr = 'Supplier Returns'
               GROUP BY
                dbo.part.part
               ,dbo.stor.descr
               ,dbo.part_bins.allocated_qty
              ) AS w
    ON p.part = w.Part
    LEFT JOIN (
               SELECT
                dbo.part.part AS Part
               ,SUM(dbo.part_bins.qty) AS UK_Press_Total_Stock
               ,dbo.stor.descr AS Stor
               ,dbo.part_bins.allocated_qty AS UK_Press_Allocated
               FROM
                dbo.part
                INNER JOIN dbo.part_bins
                ON dbo.part.part = dbo.part_bins.part
                INNER JOIN dbo.stor
                ON dbo.part_bins.store = dbo.stor.store
                   AND dbo.stor.descr = 'UK Press'
               GROUP BY
                dbo.part.part
               ,dbo.stor.descr
               ,dbo.part_bins.allocated_qty
              ) AS x
    ON p.part = x.Part
    LEFT JOIN (
               SELECT
                dbo.part.part AS Part
               ,SUM(dbo.part_bins.qty) AS US_Press_Total_Stock
               ,dbo.stor.descr AS Stor
               ,dbo.part_bins.allocated_qty AS US_Press_Allocated
               FROM
                dbo.part
                INNER JOIN dbo.part_bins
                ON dbo.part.part = dbo.part_bins.part
                INNER JOIN dbo.stor
                ON dbo.part_bins.store = dbo.stor.store
                   AND dbo.stor.descr = 'US Press'
               GROUP BY
                dbo.part.part
               ,dbo.stor.descr
               ,dbo.part_bins.allocated_qty
              ) AS y
    ON p.part = y.Part
GROUP BY
    p.identifier
,   p.part
,   p.descr
,   supl.full_name
,   pgrp.descr
,   fam.descr
,   pp.qty
,   pt.part_cost
,   pp.allocated_qty
,   p.status
,   p.obs_code
,   t.Main_Store_Total_Stock
,   t.Main_Store_Allocated
,   f.Assay_Office_Total_Stock
,   f.Assay_Office_Allocated
,   g.Consigment_Stock_Total_Stock
,   g.Consigment_Stock_Allocated
,   h.Customer_Returns_Total_Stock
,   h.Customer_Returns_Allocated
,   j.Despatch_Total_Stock
,   j.Despatch_Allocated
,   k.Goods_in_Total_Stock
,   k.Goods_in_Allocated
,   l.Harrods_Total_Stock
,   l.Harrods_Allocated
,   m.Inspection_Total_Stock
,   m.Inspection_Allocated
,   n.Liberty_Total_Stock
,   n.Liberty_Allocated
,   o.Repair_Store_Total_Stock
,   o.Repair_Store_Allocated
,   q.Return_from_Supplier_Total_Stock
,   q.Return_from_Supplier_Allocated
,   r.Selfridges_Trafford_Total_Stock
,   r.Selfridges_Trafford_Allocated
,   u.Selfridges_Oxford_Total_Stock
,   u.Selfridges_Oxford_Allocated
,   w.Supplier_Returns_Total_Stock
,   w.Supplier_Returns_Allocated
,   x.UK_Press_Total_Stock
,   x.UK_Press_Allocated
,   y.US_Press_Total_Stock
,   y.US_Press_Allocated;    

这些是我得到的一些重复的不同数量的返回(SUM(dbo.part_bins.qty)作为Supplier_Returns_Total_Stock)

29620RMGR50 D29620SZ5   Morganite Tearoom Ring Rose Size UK J/US 5  Thien Po    Fine / Fao  Astley Clarke   299.6700    1.0000  NULL    1450.000000 2250.000000 20  0   0   14 carat rose gold morganite and diamond ring   NULL    NULL    NULL    NULL    NULL    NULL    2.0000  0.0000  NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    1.0000  1.0000  NULL    NULL    0.0000  0.0000  NULL    NULL    NULL    NULL    NULL    NULL

29620RMGR50 D29620SZ5   Morganite Tearoom Ring Rose Size UK J/US 5  Thien Po    Fine / Fao  Astley Clarke   299.6700    1.0000  NULL    1450.000000 2250.000000 20  0   0   14 carat rose gold morganite and diamond ring   NULL    NULL    NULL    NULL    NULL    NULL    2.0000  0.0000  NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    2.0000  0.0000  NULL    NULL    0.0000  0.0000  NULL    NULL    NULL    NULL    NULL    NULL

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。基本上我之所以得到重复的原因是因为返回来自不同的位置(store-bin)所以我所做的就是正确地关联商店和垃圾箱,以便摆脱重复并获得总和和代码现在运行得很好。我再次为此票可能造成的任何不便表示歉意。