开发者

Mysql how to run a sum of every counts in one select statement

I need to know how to sum up all the counts of records. I have table master_tracking (master_vw_tracking_name_grp), where in there are fields like priority with constant value if 5 meaning a very high,if 4 high and 3 medium. I also have the open_date fieldname where in the date when the ticket was open. Now I can count it like this.

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-0开发者_开发百科7-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)    

I need to place it in pentaho dashboard with like this.

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

                       9         18         19              97

                       9           20       20              49

How can I do the sum up of the open tickets in one query.


Try looking at SQL_CALC_FOUND_ROWS

http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_found-rows


Because your question is so mangled, it's hard to decipher what exactly you need, but I'll try my best. I think you want something like the following:

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;

This will count the number of rows for each distinct date and priority.


Try to use something like this

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 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜