mysql select 计算多个条件
mysql select count for multiple conditions
我有一个包含元素的 table。
每个元素都有其父元素instance_id
,一个number_of_lines
的代码,
和 deletion_date
删除时。
我构建了一个查询,用于计算按实例分组的元素以及具有代码的元素(number_of_lines
非零)和已删除的元素(deletion_date
非空)
SELECT instance_id,
COUNT(id) AS elementsfound,
COUNT(NULLIF(number_of_lines,0)) AS havecode,
(COUNT(id)-COUNT(NULLIF(number_of_lines,0))) AS havenocode,
COUNT(deletion_date) AS beenremoved,
(COUNT(id)-COUNT(deletion_date)) AS stillexistent
FROM elements
GROUP BY instance_id
我得到了一个正确的结果(缩写)
+-------------+---------------+----------+------------+-------------+---------------+
| instance_id | elementsfound | havecode | havenocode | beenremoved | stillexistent |
+-------------+---------------+----------+------------+-------------+---------------+
| 59 | 93123 | 24109 | 69014 | 45397 | 47726 |
| 69 | 46399 | 7837 | 38562 | 23929 | 22470 |
| 71 | 41752 | 8746 | 33006 | 6960 | 34792 |
| 75 | 29097 | 4303 | 24794 | 13670 | 15427 |
| 77 | 41681 | 9858 | 31823 | 10540 | 31141 |
| 79 | 17800 | 695 | 17105 | 13391 | 4409 |
| 82 | 25323 | 2481 | 22842 | 20914 | 4409 |
| 83 | 12831 | 2544 | 10287 | 2988 | 9843 |
...
现在,我想添加一个列,其中包含每个实例的元素数量,两者 仍然存在(deletion_date
为空)and 有代码(number_of_lines
不同于零)
我找不到添加多列的 NULLIF
或 CASE WHEN
的方法。
如何包含多列条件计数器?
你想做什么?
sum( if(deletion_date is null and number_of_lines!=0, 1, 0) )
我假设你的意思是仍然存在的记录应该有 deletion_date NULL。
试试这个:
SELECT instance_id,
COUNT(id) AS elementsfound,
COUNT(NULLIF(number_of_lines,0)) AS havecode,
(COUNT(id)-COUNT(NULLIF(number_of_lines,0))) AS havenocode,
COUNT(deletion_date) AS beenremoved,
(COUNT(id)-COUNT(deletion_date)) AS stillexistent,
COUNT(IF(deletion_date IS NULL AND number_of_lines > 0, 1, NULL)) existentwithcode
FROM elements
GROUP BY instance_id
我有一个包含元素的 table。
每个元素都有其父元素instance_id
,一个number_of_lines
的代码,
和 deletion_date
删除时。
我构建了一个查询,用于计算按实例分组的元素以及具有代码的元素(number_of_lines
非零)和已删除的元素(deletion_date
非空)
SELECT instance_id,
COUNT(id) AS elementsfound,
COUNT(NULLIF(number_of_lines,0)) AS havecode,
(COUNT(id)-COUNT(NULLIF(number_of_lines,0))) AS havenocode,
COUNT(deletion_date) AS beenremoved,
(COUNT(id)-COUNT(deletion_date)) AS stillexistent
FROM elements
GROUP BY instance_id
我得到了一个正确的结果(缩写)
+-------------+---------------+----------+------------+-------------+---------------+
| instance_id | elementsfound | havecode | havenocode | beenremoved | stillexistent |
+-------------+---------------+----------+------------+-------------+---------------+
| 59 | 93123 | 24109 | 69014 | 45397 | 47726 |
| 69 | 46399 | 7837 | 38562 | 23929 | 22470 |
| 71 | 41752 | 8746 | 33006 | 6960 | 34792 |
| 75 | 29097 | 4303 | 24794 | 13670 | 15427 |
| 77 | 41681 | 9858 | 31823 | 10540 | 31141 |
| 79 | 17800 | 695 | 17105 | 13391 | 4409 |
| 82 | 25323 | 2481 | 22842 | 20914 | 4409 |
| 83 | 12831 | 2544 | 10287 | 2988 | 9843 |
...
现在,我想添加一个列,其中包含每个实例的元素数量,两者 仍然存在(deletion_date
为空)and 有代码(number_of_lines
不同于零)
我找不到添加多列的 NULLIF
或 CASE WHEN
的方法。
如何包含多列条件计数器?
你想做什么?
sum( if(deletion_date is null and number_of_lines!=0, 1, 0) )
我假设你的意思是仍然存在的记录应该有 deletion_date NULL。
试试这个:
SELECT instance_id,
COUNT(id) AS elementsfound,
COUNT(NULLIF(number_of_lines,0)) AS havecode,
(COUNT(id)-COUNT(NULLIF(number_of_lines,0))) AS havenocode,
COUNT(deletion_date) AS beenremoved,
(COUNT(id)-COUNT(deletion_date)) AS stillexistent,
COUNT(IF(deletion_date IS NULL AND number_of_lines > 0, 1, NULL)) existentwithcode
FROM elements
GROUP BY instance_id