如何在新列中显示总计数结果

时间:2016-07-27 11:14:45

标签: mysql count

我正在尝试从一个表和总状态计算中计算状态,但它在查询所在的同一列中显示

select Status ,count(Status) from assigntask group by status union all
SELECT count(Status) , count(Status) as total from assigntask ;

并且Reslut显示像这样

Status    Count
Complete    1
Delay       2
  3         3

是否有可能在另一列中不显示计数“3”?

例如

Status   Count   Total
Complete   1       3
Delay      2

5 个答案:

答案 0 :(得分:1)

试试这个:

select Status ,count(Status), null as totalCount from assigntask group by status union all
SELECT 'Total status' , null,  count(Status) from assigntask ;

这不完全是你想要的结果,但我认为这个结果比你理性展示的结果要好。

它将生成此结果:

Status         Count     Total
Complete       1         null
Delay          2         null  
Total status   null      3

答案 1 :(得分:0)

试试这个:

@sortedInternalLinksHash = @countHash.sort_by { |_,v| - v }

答案 2 :(得分:0)

你必须在两个选择中提供一个虚拟字段,以便工会匹配。

SELECT Status, COUNT(Status) as count, NULL as total FROM assigntask GROUP BY status 
UNION ALL
SELECT NULL, COUNT(Status), COUNT(Status) FROM assigntask;

答案 3 :(得分:0)

 
select Status, count(*) as status_total, q.total as all_total
from assigntask t
full join (select count(*) as total from assigntask) q
group by Status;

旁注:
在sql-server上你可以使用sum窗口函数 但它不是在MySQL上。

select Status, count(*) as status_total, sum(count(*)) over () as all_total
from assigntask
group by Status;

答案 4 :(得分:0)

如果您确实希望在第一行中拥有总值,则可以使用此查询:

select t1.* from (
    select Status, count(Status) as count, (select count(Status) from assigntask) as total
    from assigntask 
    group by Status limit 1) as t1
union all
select t2.* from (
    select Status, count(Status) as count, null as total
    from assigntask 
    group by Status limit 1,100000) as t2

结果是

enter image description here

另一种方法是这样的:

select Status, count(Status) as count, (select count(Status) from assigntask) as total
from assigntask 
group by Status

给出以下结果

enter image description here

我个人会使用两个单独的简单查询:

select Status, count(Status) as count
from assigntask 
group by Status;

select count(Status) as total from assigntask;