1

I have a query that I need to edit. Currently, it returns 2 columns of data, a case label and the count (or total number of cases) handled during the previous 7 day period starting yesterday. I need to change this output so that only 6 labels are in the output (i.e - always 6 rows of data). These rows need to be the top 5 labels and the sum of the remaining labels as the 6th label (called "Other"). This is because this output is fed to a PHP script that presents the data on a web-based platform.

Finally, to illustrate here is a table of the output I need as well as the query below.

+-----------+---------------+  
| CaseLabel | CasesResolved |  
+-----------+---------------+  
| Label1    |            20 |  
| Label2    |            18 |
| Label3    |            10 |
| Label4    |             9 |
| Label5    |             7 |
| Other     |            12 |
+-----------+---------------+

Thanks in advance for any help! :-)

Running MySQL 5.096

MySQL Code:

SELECT
    deskcases.Labels,
    COUNT(deskcases.Labels)AS CaseCount
FROM
    deskcases
WHERE
    deskcases.Labels NOT LIKE ''
AND deskcases.Labels NOT LIKE '%SPAM%'
AND deskcases.Labels NOT LIKE '%Online Orders%'
AND deskcases.Labels NOT LIKE '%Internal SPAM%'
AND deskcases.`Case Status` LIKE 'Resolved'
AND deskcases.`Resolved At` > CURDATE()- INTERVAL 7 DAY
GROUP BY
    deskcases.Labels
ORDER BY
    CaseCount DESC
4

2 回答 2

2

在 MySQL 中,表达这一点的最简单方法可能是使用临时表:

create temporary table temp as (
    id int not null auto_increment,
    CaseLabel varchar(255),
    CasesResolved int
);

insert into temp(CaseLabel, CasesResolved)
    SELECT deskcases.Labels, COUNT(deskcases.Labels)AS CaseCount
    FROM deskcases
    WHERE deskcases.Labels NOT LIKE ''
          AND deskcases.Labels NOT LIKE '%SPAM%'
          AND deskcases.Labels NOT LIKE '%Online Orders%'
          AND deskcases.Labels NOT LIKE '%Internal SPAM%'
          AND deskcases.`Case Status` LIKE 'Resolved'
          AND deskcases.`Resolved At` > CURDATE()- INTERVAL 7 DAY
    GROUP BY deskcases.Labels
    ORDER BY CaseCount DESC;

select (case when id <= 5 then caselabel else 'Other' end),
       SUM(casesResolved) as CasesResolved
from temp
group by (case when id <= 5 then caselabel else 'Other' end)
order by MAX(id) desc

临时表中的id列将行号添加到每一行。在任何其他真实数据库中,您都可以使用该row_number()功能,但 MySQL 不支持该功能。

于 2013-03-20T19:03:58.063 回答
-1

一种可能的选择是使用等级变量。

虚拟连接将初始化排名,if 将计数到 6。
所有这些东西都将首先在内部查询中得到解决,产生类似的东西

| Label1    |            20 |  
| Label2    |            18 |
| Label3    |            10 |
| Label4    |             9 |
| Label5    |             7 |
| Label6    |            12 |
| Label7    |               |
| ......

然后,另一个查询会将其折叠到所需的输出中。

select if(rank=6,"Other",sub.Labels) as Label, SUM(sub.CaseCount) from (
    SELECT
        if(@Rank < 6,@Rank:= @Rank + 1, @Rank) as Rank
        ,deskcases.Labels
        ,COUNT(deskcases.Labels) AS CaseCount
    FROM
        deskcases
    JOIN (@rank:= 0)
    WHERE
        deskcases.Labels NOT LIKE ''
    AND deskcases.Labels NOT LIKE '%SPAM%'
    AND deskcases.Labels NOT LIKE '%Online Orders%'
    AND deskcases.Labels NOT LIKE '%Internal SPAM%'
    AND deskcases.`Case Status` LIKE 'Resolved'
    AND deskcases.`Resolved At` > CURDATE()- INTERVAL 7 DAY
    GROUP BY
        deskcases.Labels
    ORDER BY
        CaseCount DESC
) sub
group by sub.rank ASC

将最后一行的“其他”替换labelx为“其他”。

于 2013-03-20T18:42:24.763 回答