Skip to content

Instantly share code, notes, and snippets.

@agawronski
Created January 14, 2018 20:08
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save agawronski/69e2bb8ae241ddca86b7011d4b4bdf95 to your computer and use it in GitHub Desktop.
What is the average number of orders purchased across all vendors? What is the average cost across all vendors?
select
avg(i.num_orders) as average_num_orders,
avg(i.subtotal) as average_subtotal
from (
select
y.name,
count(distinct x.purchaseorderid) as num_orders,
sum(x.subtotal) as subtotal
from purchasing.purchaseorderheader x
left join purchasing.vendor y
on x.vendorid = y.businessentityid
group by y.name
) i;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment