Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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