MySQL - DISTINCT 和 HAVING SUM
MySQL - DISTINCT with HAVING SUM
我有一个包含以下列的 table:
customerID
- 具有相同 ID 的多行客户 ID
actionVal
- 0 或 1
我想计算有多少客户 (customerID
) 有超过一行 (actionVal
) 等于 1
我想出了这个 SQL 声明,但运气不佳...
SELECT customerID, SUM(actionVal), COUNT(DISTINCT customerID) as total_C
FROM table1
GROUP BY customerID
HAVING SUM(actionVal) > 1
我正在寻找的结果是 ['total_C']
关闭,您只需计算查询中的客户数:
SELECT COUNT(*) as total_C
FROM (
SELECT customerID, SUM(actionVal)
FROM table1
GROUP BY customerID
HAVING SUM(actionVal) > 1
) as q;
您必须计算查询返回的行数。
像这样。
SELECT COUNT(1) FROM (
SELECT customerID, SUM(actionVal) as act_sum
FROM table1
GROUP BY customerID
HAVING act_sum > 1
) AS tab
希望对您有所帮助。
我有一个包含以下列的 table:
customerID
- 具有相同 ID 的多行客户 IDactionVal
- 0 或 1
我想计算有多少客户 (customerID
) 有超过一行 (actionVal
) 等于 1
我想出了这个 SQL 声明,但运气不佳...
SELECT customerID, SUM(actionVal), COUNT(DISTINCT customerID) as total_C
FROM table1
GROUP BY customerID
HAVING SUM(actionVal) > 1
我正在寻找的结果是 ['total_C']
关闭,您只需计算查询中的客户数:
SELECT COUNT(*) as total_C
FROM (
SELECT customerID, SUM(actionVal)
FROM table1
GROUP BY customerID
HAVING SUM(actionVal) > 1
) as q;
您必须计算查询返回的行数。 像这样。
SELECT COUNT(1) FROM (
SELECT customerID, SUM(actionVal) as act_sum
FROM table1
GROUP BY customerID
HAVING act_sum > 1
) AS tab
希望对您有所帮助。