COUNT(DISTINCT(在('A','B')的情况下

时间:2017-04-27 12:30:45

标签: mysql

嗨大家都想知道是否有人可以提供以下帮助:

SELECT
  HOST_ORDER_NO,
  EARLIEST_PICK_DATE,
  PRODUCT_NO,
  PRODUCT_NUM_PARTS,
  PRODUCT_WHS_LOCATION,
  COUNT(DISTINCT(CASE 
   WHEN LEFT(PRODUCT_WHS_LOCATION,1) IN ('A','B','C','K','M','N') THEN 'BULK'
   WHEN LEFT(PRODUCT_WHS_LOCATION,1) IN ('D','E','F') THEN 'TEN'
   WHEN LEFT(PRODUCT_WHS_LOCATION,1) IN ('G','H','I','J','L') THEN 'TENSMALL'
   ELSE 'SIP' END) AS 'ZONE',
  MAX(COLLATION_INDICATOR),
  COLLATION_GROUP_ID,
  CASE WHEN COUNT(HOST_ORDER_NO)=1 THEN 'SingleOrder' ELSE 'MultiItem' END AS 'OrderType',
  COUNT(*) AS 'ItemsInOrder',
  YEARWEEK
FROM wk19
  WHERE YearWeek = '201717'
  GROUP BY HOST_ORDER_NO
  HAVING OrderType = 'MultiItem'
  ORDER BY HOST_ORDER_NO, COLLATION_GROUP_ID, MAX(COLLATION_INDICATOR);

我的错误是在案例中我希望能够看到订单在仓库中有多少不同的区域。声明的其余部分工作正常,但如果我需要更多地将其打破,我可以做。

1 个答案:

答案 0 :(得分:0)

您的case 表达式只是缺少括号。 distinct之后不需要paren,所以:

COUNT(DISTINCT CASE WHEN LEFT(PRODUCT_WHS_LOCATION,1) IN ('A','B','C','K','M','N') THEN 'BULK'
                    WHEN LEFT(PRODUCT_WHS_LOCATION,1) IN ('D','E','F') THEN 'TEN'
                    WHEN LEFT(PRODUCT_WHS_LOCATION,1) IN ('G','H','I','J','L') THEN 'TENSMALL'
                     ELSE 'SIP'
               END) AS ZONE,

建议:只对字符串和日期使用单引号。不要将它们用于列别名。