Select
	roundtime,
	Total_count,DivertFail_count,Diverted_count, DestFull_count,DestJam_count,DestDis_count,
	DestFault_count,Lost_count,Unsafe_count,Dim_count,GapErr_count,Unknown_count,
	
	Total_count/Total_count as total_perc,
	DivertFail_count/Total_count as DivertFail_perc,
	Diverted_count/Total_count as Diverted_perc,
	DestFull_count/Total_count as destfull_perc,
	DestJam_count/Total_count as destjam_perc,
	DestDis_count/Total_count as destdisabled_perc,
	DestFault_count/Total_count as destfault_perc,
	Lost_count/Total_count as lost_perc,
	Unsafe_count/Total_count as unsafe_perc,	
	Dim_count/Total_count as dim_perc,
	GapErr_count/Total_count as gaperr_perc,
	Unknown_count/Total_count as unknown_perc,		
		
	Total_count*3600/TIMESTAMPDIFF(second, :starttime, :endtime) as total_rate,
	DivertFail_count*3600/TIMESTAMPDIFF(second, :starttime, :endtime) as DivertFail_rate,
	Diverted_count*3600/TIMESTAMPDIFF(second, :starttime, :endtime) as Diverted_rate,
	DestFull_count*3600/TIMESTAMPDIFF(second, :starttime, :endtime) as destfull_rate,
	DestJam_count*3600/TIMESTAMPDIFF(second, :starttime, :endtime) as destjam_rate,
	DestDis_count*3600/TIMESTAMPDIFF(second, :starttime, :endtime) as destdisabled_rate,
	DestFault_count*3600/TIMESTAMPDIFF(second, :starttime, :endtime) as destfault_rate,
	Lost_count*3600/TIMESTAMPDIFF(second, :starttime, :endtime) as lost_rate,
	Unsafe_count*3600/TIMESTAMPDIFF(second, :starttime, :endtime) as unsafe_rate,
	Dim_count*3600/TIMESTAMPDIFF(second, :starttime, :endtime) as dim_rate,
	GapErr_count*3600/TIMESTAMPDIFF(second, :starttime, :endtime) as gaperr_rate,
	Unknown_count*3600/TIMESTAMPDIFF(second, :starttime, :endtime) as unknown_rate

From
	(Select
	COUNT(*) AS Total_count,
	SUM(alltable.ACTUAL_DEST <> 'S0199') AS Diverted_count,
	SUM(alltable.DivertStatus = 14) AS DivertFail_count,
	SUM(alltable.DivertStatus = 5) AS DestFull_count,
	SUM(alltable.DivertStatus = 6) AS DestJam_count,
	SUM(alltable.DivertStatus = 12) AS DestDis_count,
	MIN(alltable.t_stamp) AS Startstamp, 
    Max(alltable.t_stamp) AS Endtstamp,
	SUM(alltable.DivertStatus = 6) as DestFault_count,
	SUM(alltable.DivertStatus = 17) as Lost_count,
	SUM(alltable.DivertStatus = 21) as Unsafe_count,
	SUM(alltable.DivertStatus = 18) as Dim_count,
	SUM(alltable.DivertStatus = 4) as GapErr_count,
	SUM(alltable.DivertStatus = 1) as Unknown_count,		
    FROM_UNIXTIME(CEIL(UNIX_TIMESTAMP(alltable.t_stamp) / 60) * 60) roundtime
    
	FROM alltable
	Where (alltable.t_stamp BETWEEN :starttime AND :endtime) and (alltable.DEST_REQ = :Lane)
	Group BY FROM_UNIXTIME(CEIL(UNIX_TIMESTAMP(alltable.t_stamp) / 60) * 60)
	Order By alltable.t_stamp ASC
	) okey