我有一个简单的安装:
表如果install_slot为NULL,那么它是一个可用的安装槽。不为null - 然后,使用插槽。我需要返回给定产品和电子邮件的总安装结果,以及给定产品和电子邮件的已安装结果。我想我可以通过两个查询来做到这一点,但是想知道是否有一种SQL方法可以在一个中完成这一切?
我尝试了下面的猜测,但无效。
SELECT
i1.`prod_code`,
COUNT(i1.`email`) AS total_installs,
COUNT(ISNULL(i2.`install_slot`)) AS used_installs
FROM
`installs` AS i1
JOIN
`installs` AS i2
ON
i1.`prod_code` = i2.`prod_code`
WHERE
i1.`email` = 'example@example.com'
GROUP BY
i1.`prod_code`,i2.`prod_code`
答案 0 :(得分:30)
SELECT prod_code,
COUNT(email) AS total_installs,
COUNT(install_slot) AS used_installs
FROM installs
WHERE email='example@example.com'
GROUP BY prod_code
COUNT
仅计算NOT NULL
个值。
答案 1 :(得分:5)
提供的解决方案对我不起作用。我必须修改如下:
SELECT prod_code,
COUNT(NULLIF(email,'')) AS total_installs,
COUNT(NULLIF(install_slot,'')) AS used_installs
FROM installs
WHERE email='example@example.com'
GROUP BY prod_code