1661. Average Time of Process per Machine

Question:

https://leetcode.com/problems/average-time-of-process-per-machine/

Solution:

select 
a.machine_id, 
round(sum(a.duration)/count(distinct a.process_id),3) as processing_time
from(
    select 
    s.machine_id, 
    s.process_id, 
    (e.end_time- s.start_time) as duration
    from(
        select 
        machine_id, 
        process_id, 
        timestamp as start_time
        from Activity
        where activity_type = 'start'
        ) s
    left join (
        select 
        machine_id, 
        process_id, 
        timestamp as end_time
        from Activity
        where activity_type = 'end'
    ) e 
        on (s.machine_id = e.machine_id and s.process_id = e.process_id )
) a
group by 1
order by 1

1667. Fix Names in a Table

Question:

https://leetcode.com/problems/fix-names-in-a-table/

Solution:

select 
user_id, 
concat(upper(mid(name,1,1)),lower(mid(name,2))) as name
from Users
order by 1

1677. Product’s Worth Over Invoices

Question:

https://leetcode.com/problems/products-worth-over-invoices/

Solution:

select p.name, 
ifnull(sum(i.rest),0) as rest, 
ifnull(sum(i.paid),0) as paid,
ifnull(sum(i.canceled),0) as canceled,
ifnull(sum(i.refunded),0) as refunded
from Product p
    inner join Invoice i 
        on p.product_id = i.product_id
group by 1
order by 1

Leave a Reply