LeetCode 1661: Average Time of Process per Machine [SQL]

แชร์
ฝัง
  • เผยแพร่เมื่อ 5 ก.ย. 2024

ความคิดเห็น • 15

  • @ayeoh47
    @ayeoh47 2 ปีที่แล้ว +11

    this one felt more challenging than the other easy questions, i didnt think to create a self join, this made the problem much easier to solve thank you

    • @frederikmuller
      @frederikmuller  2 ปีที่แล้ว +1

      glad it helped!

    • @illiat3164
      @illiat3164 6 หลายเดือนก่อน

      I solved it by using 2 additional CTEs and I still think using CTEs here is more cleaner and production like code:
      with start_activity_type as (
      select
      machine_id,
      process_id,
      timestamp as start_timestamp
      from Activity
      where activity_type = 'start'
      ),
      end_activity_type as (
      select
      machine_id,
      process_id,
      timestamp as end_timestamp
      from Activity
      where activity_type = 'end'
      )

  • @459B
    @459B 2 ปีที่แล้ว +7

    my solution without using join:
    select machine_id,
    avg(case when activity_type='end' then timestamp end)-avg(case when activity_type='start' then timestamp end) as processing_time
    from Activity
    group by machine_id

    • @mickyman753
      @mickyman753 2 ปีที่แล้ว +1

      that's intuitive , thanks for the solution

  • @UmaAndLak
    @UmaAndLak 2 ปีที่แล้ว +2

    This was what i typed in MySQL. Another solution.
    select machine_id, round(avg (end_timestamp - start_timestamp),3) as processing_time
    from
    (
    select
    machine_id,
    process_id,
    SUM(if(activity_type ='start', timestamp,null)) AS start_timestamp,
    SUM(if(activity_type ='end', timestamp,null)) AS end_timestamp
    from Activity
    group by machine_id, process_id
    ) as t
    group by machine_id

    • @ayeoh47
      @ayeoh47 2 ปีที่แล้ว

      this is what i sumbitted, this was my solution

  • @ThinkBigToday
    @ThinkBigToday 3 ปีที่แล้ว +1

    Excellent explanation. You’re the 🐐

  • @partidaa98
    @partidaa98 9 หลายเดือนก่อน

    As a beginner (and this being an "easy" LC problem), I think it would be very helpful if you could explain the reasoning for some of your decision making/thought process. Sometimes it feels like you just say and type what you are doing, but it isn't always intuitive to follow your logic (at least to a beginner like me). Great video though, very clear and concise overall.

    • @frederikmuller
      @frederikmuller  9 หลายเดือนก่อน +1

      That’s good feedback, thank you!

  • @vijaysakthivel4966
    @vijaysakthivel4966 11 หลายเดือนก่อน

    Thank you so much for this explanation. Really helpful.

  • @SB-ix7db
    @SB-ix7db 7 หลายเดือนก่อน

    usage of subquery in this question, will increase the time complexity!

  • @temenoscom3498
    @temenoscom3498 2 หลายเดือนก่อน

    great work, thanks.

  • @sanaayakurup5453
    @sanaayakurup5453 2 ปีที่แล้ว

    My Solution-select machine_id,AVG(diff) from
    (select machine_id,end_time-start_time as diff from (select machine_id,process_id,
    sum(if(activity_type='start',time,0)) as start_time,
    sum(if(activity_type='end',time,0)) as end_time
    from Activity
    group by machine_id,process_id) t1 )t2
    group by machine_id

  • @lonakshibhagat3116
    @lonakshibhagat3116 3 ปีที่แล้ว

    Hi, can we use case when in this problem or not?