【问题标题】:For vehicles with 2 or more suppliers, COUNT the number of parts (vehicle_part) and suppliers (supplier) per vehicleFor vehicles with 2 or more suppliers, COUNT the number of parts (vehicle_part) and suppliers (supplier) per vehicle
【发布时间】:2022-12-27 20:32:25
【问题描述】:
  • Data model

- Goal:Count the number of suppliers (supplier) and parts (vehicle_part) for each vehicle for which there are two or more suppliers.

- requirements:Expected columns: vehicle_name, vehicle_part_count, supplier_count in that order

【问题讨论】:

    标签: sql join group-by count


    【解决方案1】:

    Goal: Count the number of suppliers (supplier) and parts (vehicle_part) for each vehicle for which there aretwo or more suppliers<-- for each part ?

    There could be like 5 parts of a vehicle so 1 supplier per part = 5 different suppliers, does that count or not ?

    【讨论】:

    • How I could Count the parts and suppliers for each car. as you commented 5 parts could have 5 different suppliers
    • Could 2 different vehicle_part_ids have same supplier ?
    • Indeed, there may be a part supplied by more then One Supplier
    • That supplier should also appear once in the count right ?
    • let's assume that exact behavor I'm looking for
    【解决方案2】:

    select v.vehicle_name, count(distinct vp.vehicle_part_id) as vehicle_part_count, count(distinct vps.supplier_id) as supplier_count from "codinGame".vehicle_part vp join "codinGame".vehicle_part_supplier vps on vp.vehicle_part_id=vps.vehicle_part_id join "codinGame".vehicle v on vp.vehicle_id = v.vehicle_id group by v.vehicle_name having count(distinct vps.supplier_id)&gt;=2;

    【讨论】:

      猜你喜欢
      • 2020-06-24
      • 2022-12-01
      • 2020-07-11
      • 2020-11-12
      • 1970-01-01
      • 2023-02-03
      • 1970-01-01
      • 2022-12-02
      • 2022-12-27
      相关资源
      最近更新 更多