Wednesday, September 5, 2012

query for sub query grouping example

select (select name from source where id = src.parent) as 'Plant',  abbreviation, src.name, IFNULL((select sec_to_time(sum(time_to_sec(Duration))) from downtime where category=1 and source =down.source ),sec_to_time(0))  as  'Planned',
IFNULL((select sec_to_time(sum(time_to_sec(Duration))) from downtime where category=2 and source =down.source),sec_to_time(0)) as  'Unplanned',
IFNULL((select sec_to_time(sum(time_to_sec(Duration))) from downtime where category=3 and source =down.source),sec_to_time(0)) as  'StandBy',
IFNULL((select sec_to_time(sum(time_to_sec(Duration))) from downtime where source =down.source),sec_to_time(0)) as  'Total'
from downtime down   
inner join category c on down.category = c.id
inner join source src on down.source = src.id
group by abbreviation;

No comments:

Post a Comment