Mysql如何在一个select语句中运行每个计数的总和

时间:2011-08-16 07:47:20

标签: mysql

我需要知道如何总结所有记录的数量。我有表master_tracking(master_vw_tracking_name_grp),其中有像常量值的优先级字段,如果5表示非常高,如果4高和3中等。我还有open_date字段名,其中在故障单打开的日期。现在我可以这样算。

V-HIGH                    
SELECT substr(open_date,1,10) as time, COUNT(*) as rows FROM 
`master_vw_tracking_name_grp` 
where assigned_to in ('name1','name2','name3') and substr(open_date,1,16) 
between '2011-07-25 00:00' and '2011-07-25 23:59' and priority='5' 
group by substr(open_date,1,10) order by 1;
+------------+------+
| time       | rows |
+------------+------+
| 2011-07-25 |    9 |
+------------+------+
1 row in set (0.00 sec)

HIGH
SELECT substr(open_date,1,10) as time, COUNT(*) as rows 
FROM `master_vw_tracking_name_grp` 
where assigned_to in ('name1','name2','name3') and substr(open_date,1,16) 
between '2011-07-25 00:00' and '2011-07-25 23:59' and priority='4' 
group by substr(open_date,1,10) order by 1;
+------------+------+
| time       | rows |
+------------+------+
| 2011-07-25 |   20 |
+------------+------+
1 row in set (0.10 sec)

MEDIUM
 SELECT substr(open_date,1,10) as time, COUNT(*) as rows 
FROM `master_vw_tracking_name_grp` 
where assigned_to in ('name1','name2','name3') and substr(open_date,1,16) 
between '2011-07-25 00:00' and '2011-07-25 23:59' and priority='3' 
group by substr(open_date,1,10) order by 1;
+------------+------+
| time       | rows |
+------------+------+
| 2011-07-25 |   20 |
+------------+------+
1 row in set (0.09 sec)    

我需要将它放在这样的pentaho仪表板中。

Agent:                 No. Of Open Tickets              Total
ServiceDeskGroup   Very High 5| High 4 | Medium 3   

                       9         18         19              97

                       9           20       20              49

如何在一个查询中对打开的票证进行总结。

3 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

因为你的问题是如此严重,所以很难破译你究竟需要什么,但我会尽我所能。我想你想要的东西如下:

SELECT date(open_date) AS time, priority, COUNT(*) as rows 
FROM `master_vw_tracking_name_grp` 
WHERE assigned_to in ('bryan.latoza', 'edison.alegre', 'maja.pasamba',
    'gabriel.manlapig','grace.sanchez','francis.abellera','chris.dimaliuat',
    'rayniel.delosreyes','michael.yu','jeffrey.bucaneg','emman.soriano',
    'carmina.fernandez','jet.binamira','katrinekae.soriano','rogelio.nolasco',
    'chard.batisatic') 
  AND open_date BETWEEN '2011-07-25 00:00' AND '2011-07-25 23:59'
GROUP BY date(open_date), priority
ORDER BY priority DESC;

这将计算每个不同日期和优先级的行数。

答案 2 :(得分:0)

尝试使用类似的东西

select time, sum(level_5), sum(level_4), sum(level_3)
from (
      select substr(open_date,1,10) as time,
       case when priority='3' then 1 else 0 end as level_3,
       case when priority='4' then 1 else 0 end as level_4,
       case when priority='5' then 1 else 0 end as level_5
      from `master_vw_tracking_name_grp`
      where 
          assigned_to in ('bryan.latoza', 'edison.alegre', 'maja.pasamba',
          'gabriel.manlapig','grace.sanchez','francis.abellera','chris.dimaliuat',
          'rayniel.delosreyes','michael.yu','jeffrey.bucaneg','emman.soriano',
          'carmina.fernandez','jet.binamira','katrinekae.soriano','rogelio.nolasco',
          'chard.batisatic') and substr(open_date,1,16) 
          between '2011-07-25 00:00' and '2011-07-25 23:59'
) as temp group by time