伙计们我有这样的事情:
select t1.milestone_id, sum(t2.value) / count(t2.value) as value2, value3
from (
SELECT milestone_id, SUM(value) value , CASE WHEN (status !='done') THEN (value ='0') ELSE value END as value3
FROM project_has_tasks
WHERE project_id='52' and milestone_id !=0
GROUP BY milestone_id
) t1
join (
SELECT milestone_id, SUM(value) value
FROM project_has_tasks
WHERE project_id='52' and milestone_id !=0
GROUP BY milestone_id
) t2
on t1.milestone_id >= t2.milestone_id
group by t1.milestone_id
order by t1.milestone_id asc
得到了这个: enter image description here
请你帮我把value3作为value2除了0 像这样:
感谢您的任何建议。
答案 0 :(得分:0)
修改你的第一行:
SELECT t1.milestone_id, sum(t2.value) / count(t2.value) as value2, IF(value3=0, sum(t2.value) / count(t2.value), value3) as value3
IF语句适用于以下(see the doc):
IF(<expression>, <expression is true>, <expression is false>)
如果expression不是NULL而不是0则认为是真的。
答案 1 :(得分:0)
感谢您的建议。下面的代码很好用:)
select t1.milestone_id, sum(t2.value) / count(t2.value) as value2, IF(value3 !='0', sum(t2.value) / count(t2.value), value3)as value3
from (
SELECT milestone_id, SUM(value) value , CASE WHEN (status !='done') THEN (value ='0') ELSE value END as value3
FROM project_has_tasks
WHERE project_id='52' and milestone_id !=0
GROUP BY milestone_id
) t1
join (
SELECT milestone_id, SUM(value) value
FROM project_has_tasks
WHERE project_id='52' and milestone_id !=0
GROUP BY milestone_id
) t2
on t1.milestone_id >= t2.milestone_id
group by t1.milestone_id
order by t1.milestone_id asc