gpt4 book ai didi

MySQL根据文本单词进行计数

转载 作者:行者123 更新时间:2023-11-29 06:49:25 26 4
gpt4 key购买 nike

因为我正在计算有多少个 SKU(基于每个 SKU),据我所知,SKU“H-ROOT-C”有 3 件商品已退款。

SELECT 
sku,
count(IF(transaction_type = 'Refund'AND amount_description = 'Principal' AND amount_type = 'ItemPrice', sku, 0)) AS 'refund_QTY_Order'
FROM
settlements_qty_test
WHERE
(sku NOT LIKE '%loc%'
AND sku NOT LIKE 'isc%'
AND sku NOT LIKE 'trek%')
GROUP BY SKU ='H-ROOT-C'
HAVING sku IS NOT NULL AND LENGTH(sku) > 0
ORDER BY refund_QTY_Order ASC

结果输出

'H-ROOT-C', '125'

我一直得到结果,因为所有退款计数为 125,因为我希望此搜索重点关注每组 SKU,而 h-root-C 应该是 3 件商品返回,而不是 125 件商品返回。我不需要所有 SKU 退款,我只想要这个 SKU='H-root-c'。

最佳答案

您可以将其简化为:

SELECT sku
sum( (transaction_type = 'Refund' AND
amount_description = 'Principal' AND
amount_type = 'ItemPrice'
)
) AS refund_QTY_Order
FROM settlements_qty_test
WHERE sku NOT LIKE '%loc%' AND
sku NOT LIKE 'isc%' AND
sku NOT LIKE 'trek%' AND
LENGTH(sku) > 0
GROUP BY SKU
ORDER BY refund_QTY_Order ASC;

NULL的比较是多余的。我将条件逻辑留在了 SELECT 中,因此您将获得 0 值。

关于MySQL根据文本单词进行计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48081282/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com