This is one of those cases where a subquery would be very usefull.
A JOIN just doesn't do it, not even with IF/CASE statements, (You'd have to mess around with internal variables to make sure you get only one count for contract no matter how many orders there are)
Either use subqueries (if your database allows that) or use temporary tables to narrow down your results.
company; total_ordertime; total_contracttime
First insert the companies:
(I did not test these queries, so you may have to fiddle with them)
SELECT companyname, id FROM companies;
then insert the sum of the time-allowed by each contract:
SELECT sum(time) FROM contract, temp_table
WHERE contract.companyid = temp_table.companyid
GROUP BY temp_table.companyid
the inser the total time for all the orders for all the contracts for each company:
SELECT sum(time) FROM orders, contract, temp_table
WHERE contract.companyid = temp_table.companyid
AND orders.contractid = contracts.contractid
GROUP BY temp_table.companyid
Then you should end up with a table that contains the company name, the total time for all contracts, and the total time for all the orders.