首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MySQL:案例查询中的案例查询?

MySQL:案例查询中的案例查询?
EN

Stack Overflow用户
提问于 2016-08-02 04:13:17
回答 3查看 63关注 0票数 0

我有一个查询,它得到单位计数和它标记,如果完成与否。

代码语言:javascript
复制
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进行求和。但是,当一个位置只有一个注释时,就会将该注释作为其标记。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-08-02 04:20:36

就像你说的,一个案子里的一个案子:

代码语言:javascript
复制
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 location

SQLFiddle演示

票数 1
EN

Stack Overflow用户

发布于 2016-08-02 04:18:59

对此不确定(示例数据会有所帮助),但请尝试如下:

代码语言:javascript
复制
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
票数 3
EN

Stack Overflow用户

发布于 2016-08-02 04:20:29

请尝试以下查询:-

代码语言:javascript
复制
SELECT location, 
       if(count(1) > 1, 'In Progress', Remarks) Remarks, 
       sum(countName) countName 
   FROM location 
   group by location
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38711647

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档