Skip to content

Instantly share code, notes, and snippets.

@Sedose
Last active December 16, 2021 12:06
Show Gist options
  • Select an option

  • Save Sedose/c13819ddd835fbaf9fac5864d183fff8 to your computer and use it in GitHub Desktop.

Select an option

Save Sedose/c13819ddd835fbaf9fac5864d183fff8 to your computer and use it in GitHub Desktop.
--1st solution
select warehouse, count(warehouse) as number_of_boxes
from boxes
group by warehouse
union (
select code, '0'
from warehouses
where code not in (
select warehouse
from boxes
)
);
--2nd solution
with warehouses_with_boxes (warehouse_code, number_of_boxes)
as (
select warehouse, count(warehouse) as number_of_boxes
from boxes
group by warehouse
)
select warehouse_code, number_of_boxes
from warehouses_with_boxes
union (
select code, '0'
from warehouses
where code not in (
select warehouse_code
from warehouses_with_boxes
)
);
Select the warehouse codes, along with the number of boxes in each warehouse.
!!!Take into account that some warehouses are empty
(i.e., the box count should show up as zero, instead of omitting the warehouse from the result).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment