我有一个查询,它得到单位计数和它标记,如果完成与否。
SELECT distinct location,
case when id is NULL then 'Not Started'
when '1' then 'Completed'
else 'In Progress' end as Remarks,
count(name) as CountName
FROM table
group by location,
case when id is NULL then 'Not Started'
when '1' then 'Completed'
else 'In Progress' end;结果:

但我想将此概括为以下图片:

条件是当位置上有更多的两个(2)注释时,应该将其标记为“进行中”并对CountName进行求和。但是,当一个位置只有一个注释时,就会将该注释作为其标记。
发布于 2016-08-02 04:20:36
就像你说的,一个案子里的一个案子:
select location,
case when count(distinct case when id is null then 'Not Started'
when id = '1' then 'Completed'
else 'In Progress' end) > 1
then 'In Progress'
else max(case when id is null then 'Not Started'
when id = '1' then 'Completed'
else 'In Progress' end)
end as remarks,
count(*) as CountName
from tbl
group by locationSQLFiddle演示
发布于 2016-08-02 04:18:59
对此不确定(示例数据会有所帮助),但请尝试如下:
SELECT Location,
case when count(id) > 1 then 'In Progress'
when max(id) is null then 'Not Started'
when max(id) = 1 then 'Completed'
else 'In Progress' end As Remarks,
count(name) as CountName
FROM table
GROUP BY location发布于 2016-08-02 04:20:29
请尝试以下查询:-
SELECT location,
if(count(1) > 1, 'In Progress', Remarks) Remarks,
sum(countName) countName
FROM location
group by locationhttps://stackoverflow.com/questions/38711647
复制相似问题