我有以下查询按日期对“打开销售报价”记录进行分组,并且我添加了一些CASE
表达式以将总公开报价金额分成4个附加列:
SELECT
DATE(quotes.created_at) AS date,
COUNT(DISTINCT quotes.entity_id) AS quote_open,
SUM(quote_items.row_total) AS quote_open_mrc,
CASE WHEN quotes.percentage_to_close >= 0 < 50 THEN COUNT(DISTINCT quotes.entity_id) END AS quote_open_50,
CASE WHEN quotes.percentage_to_close >= 50 < 75 THEN COUNT(DISTINCT quotes.entity_id) END AS quote_open_5075,
CASE WHEN quotes.percentage_to_close >= 75 THEN COUNT(DISTINCT quotes.entity_id) END AS quote_open_75,
CASE WHEN quotes.percentage_to_close IS NULL THEN COUNT(DISTINCT quotes.entity_id) END AS quote_open_noclose
FROM `sales_quote_item` AS `quote_items`
INNER JOIN `sales_quote` AS `quotes`
ON quotes.entity_id = quote_items.quote_id
WHERE (quote_items.sku like '05-%')
AND (quotes.is_quotation = 1)
AND (quotes.quote_status NOT IN ('won', 'lost', 'expired'))
AND (quotes.quote_type = 'prod_serv_base_rec')
GROUP BY
`date`
;
但这并没有返回正确的结果:
+------------+------------+----------------+---------------+-----------------+---------------+--------------------+
| date | quote_open | quote_open_mrc | quote_open_50 | quote_open_5075 | quote_open_75 | quote_open_noclose |
+------------+------------+----------------+---------------+-----------------+---------------+--------------------+
| 2014-09-25 | 2 | 209.9000 | 2 | 2 | NULL | NULL |
| 2014-09-29 | 1 | 95.4300 | 1 | 1 | NULL | NULL |
| 2014-09-30 | 5 | 943.4500 | 5 | 5 | NULL | NULL |
| 2014-10-02 | 1 | 70.9700 | 1 | 1 | NULL | NULL |
| 2014-10-03 | 1 | 105.8300 | 1 | 1 | NULL | NULL |
| 2014-10-06 | 1 | 197.3800 | 1 | 1 | NULL | NULL |
| 2014-10-07 | 2 | 1430.1800 | 2 | 2 | NULL | NULL |
| 2014-10-08 | 4 | 1185.5100 | 4 | 4 | NULL | NULL |
| 2014-10-09 | 1 | 57.4800 | 1 | 1 | NULL | NULL |
| 2014-10-13 | 1 | 73.1000 | 1 | 1 | NULL | NULL |
| 2014-10-14 | 2 | 326.6800 | 2 | 2 | 2 | NULL |
| 2014-10-15 | 2 | 1294.8300 | 2 | 2 | NULL | NULL |
+------------+------------+----------------+---------------+-----------------+---------------+--------------------+
我不确定如何正确使用CASE
,但实际上我想要的是在同一结果集中看到quote_open
跨差异“桶”的传播。因此,4个存储桶列应在每条记录上加起来quote_open
。
答案 0 :(得分:1)
您的CASE WHEN
语法错误。尝试:
COUNT(DISTINCT CASE
WHEN quotes.percentage_to_close >= 0
AND quotes.percentage_to_close < 50
THEN quotes.entity_id
END) AS quote_open_50,
COUNT(DISTINCT CASE
WHEN quotes.percentage_to_close >= 0
AND quotes.percentage_to_close < 50
THEN quotes.entity_id
END) AS quote_open_50,
COUNT(DISTINCT CASE
WHEN quotes.percentage_to_close >= 50
AND quotes.percentage_to_close < 75
THEN quotes.entity_id
END) AS quote_open_5075,
COUNT(DISTINCT CASE
WHEN quotes.percentage_to_close >= 75
THEN quotes.entity_id
END) AS quote_open_75,
COUNT(DISTINCT CASE
WHEN quotes.percentage_to_close IS NULL
THEN quotes.entity_id
END) AS quote_open_noclose
答案 1 :(得分:0)
你的逻辑写得不好:
CASE WHEN quotes.percentage_to_close >= 0 < 50
这将被解析为更像
CASE WHEN quotes.percentage_to_close >= (0 < 50)
CASE WHEN quotes.percentage_to_close >= (true)
CASE WHEN quotes.percentage_to_close >= 1
你不能写这样的多重比较,尝试更像
的东西CASE WHEN (a >= 0) AND (a <= 50)
代替。