mysql计数

dxxyhpgq  于 2021-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(336)

因为我正在计算有多少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'。

ubof19bj

ubof19bj1#

试试这个:

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

如果只需要sku为“h-root-c”的记录,则:

SELECT 
        sku,
        count(sku) AS 'refund_QTY_Order'
    FROM
        settlements_qty_test
    WHERE (sku IS NOT NULL AND sku='H-ROOT-C' 
    AND transaction_type = 'Refund'AND amount_description = 'Principal' 
    AND amount_type = 'ItemPrice')
    GROUP BY SKU ORDER BY refund_QTY_Order ASC
klh5stk1

klh5stk12#

您可以将其简化为:

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 价值观。

相关问题