MS SQL中CASE列上的MAX语句问题

时间:2013-03-14 10:39:29

标签: sql sql-server

我无法在下面的查询中获取第二个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

1 个答案:

答案 0 :(得分:1)

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

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

而是将其留作空白栏:

'' AS ME_Bin

这允许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