2

我无法让下面查询中的第二个 MAX 语句按我的意愿工作,举一个我目前得到的输出示例;

PART                    FREE_STOCK      BIN             ME_BIN
01TSC7EM                21.0000         A1E             NULL
01TSC7EM010MLG           7.0000         A1E             NULL
01TSC7EM010MME           7.0000         218             218         
01TSC7EM010MSM           3.0000         407             NULL
01TSC7EM010MXL           4.0000         407             NULL

BIN列的第一个 MAX 正在按我所期望的分组工作,这意味着短部分获得BIN最多的位置FREE_STOCK,我希望通过使用 case 语句并遵循类似的逻辑,我也可以返回BIN部分结尾的位置'%ME'并使用相同的 MAX 行为来为短部分设置它。

这是因为我使用 case 语句来提供值而不是针对列执行吗?关于如何让它发挥作用的任何建议?

SELECT dbo.part_bins.part, SUM(dbo.part_bins.free_stock) AS FREE_STOCK, MAX(PART_BINS.BIN) AS Bin, 
       MAX(CASE WHEN part_bins.part like '%ME' then PART_BINS.BIN END) AS ME_Bin
FROM   dbo.part_bins 
WHERE  dbo.part_bins.free_stock > 0 
GROUP BY dbo.part_bins.part
UNION
SELECT part.master_part AS part, SUM(dbo.part_bins.free_stock) AS FREE_STOCK,     MAX(PART_BINS.BIN) AS Bin, 
       MAX(CASE WHEN part.master_part like '%ME' THEN PART_BINS.BIN END) AS ME_Bin
FROM   dbo.part_bins 
WHERE  dbo.part_bins.free_stock > 0                   
GROUP BY part.master_part

我希望我能得到最终的结果是:

PART                    FREE_STOCK      BIN             ME_BIN
01TSC7EM                21.0000         A1E             218
01TSC7EM010MLG           7.0000         A1E             NULL
01TSC7EM010MME           7.0000         218             218         
01TSC7EM010MSM           3.0000         407             NULL
01TSC7EM010MXL           4.0000         407             NULL
4

1 回答 1

1

它不漂亮,但这是我的最终解决方案。我发现我需要在第一个选择中删除 MAX CASE 语句:

MAX(CASE WHEN part_bins.part like '%ME' then PART_BINS.BIN END) AS ME_Bin

而是将其保留为空白列:

'' AS ME_Bin

这使得 group by part.master_part 可以按照我的预期与第二个 SELECT 中的 MAX CASE 语句一起工作。其他变化是考虑到男性和女性产品尺寸之间的一些差异,以及考虑是否有免费库存,这些都与解决方案无关。

SELECT     dbo.part_bins.part, SUM(dbo.part_bins.free_stock) AS FREE_STOCK, MAX(PART_BINS.BIN) AS Bin, '' as ME_bin

FROM         dbo.part_bins LEFT OUTER JOIN
                      dbo.part ON dbo.part_bins.part = dbo.part.part LEFT OUTER JOIN
                      dbo.stor ON dbo.part_bins.store = dbo.stor.store LEFT OUTER JOIN
                      dbo.stor_bdtl ON dbo.part_bins.store = dbo.stor_bdtl.store AND dbo.part_bins.bin = dbo.stor_bdtl.bin
WHERE     (dbo.stor.on_hand = 1) AND (dbo.part_bins.free_stock > 0) AND (dbo.stor.sales = 1) OR
                      (dbo.stor.on_hand = 1) AND (dbo.part_bins.free_stock > 0) AND (dbo.stor.production = 1)
GROUP BY dbo.part_bins.part
UNION
SELECT     part.master_part AS part, SUM(dbo.part_bins.free_stock) AS FREE_STOCK, MAX(PART_BINS.BIN) AS Bin, 
        MAX (case 
            when part.prod_group like 'ME%' AND part_bins.part like '%ME' then part_bins.bin
            when part.prod_group like 'ME%' AND part_bins.part like '%32' then part_bins.bin
            when part.prod_group like 'LA%' AND part_bins.part like '%08' then part_bins.bin
            when part.prod_group like 'LA%' AND part_bins.part like '%XS' then part_bins.bin
        end) as ME_bin

FROM         dbo.part_bins LEFT OUTER JOIN
                      dbo.part ON dbo.part_bins.part = dbo.part.part LEFT OUTER JOIN
                      dbo.stor ON dbo.part_bins.store = dbo.stor.store LEFT OUTER JOIN
                      dbo.stor_bdtl ON dbo.part_bins.store = dbo.stor_bdtl.store AND dbo.part_bins.bin = dbo.stor_bdtl.bin
WHERE     (dbo.stor.on_hand = 1) AND (dbo.part_bins.free_stock > 0) AND (dbo.stor.sales = 1) OR
                      (dbo.stor.on_hand = 1) AND (dbo.part_bins.free_stock > 0) AND (dbo.stor.production = 1)
GROUP BY part.master_part
于 2014-04-08T09:04:15.043 回答