Link to home
Create AccountLog in
Avatar of Morpheus7
Morpheus7

asked on

Calculations

I have a table that contains the the following fields:
Order number , product code and quantity. Most of the orders have multiple lines.
What would be the best way to determine the following:
Total number of orders
Number of orders that have
1-5 items
5-20 items
20-50 items
50 plus items

Also average number of products per order,
Average number of items per order
Any help would be appreciated
Thanks
Avatar of edtechdba
edtechdba
Flag of United States of America image

By "items" are you referring to the quantity? As in quantity of items?
Avatar of PortletPaul
here's a start
select
          num_of_orders
        , num_of_products
        , tot_rows / num_of_orders as avg_items_per_order
from (
        select
                  count(*) as tot_rows
                , count(distinct [Order number]) as num_of_orders
                , count(distinct [product code]) as num_of_products
        from sometable
     ) as derived

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of PortletPaul
PortletPaul
Flag of Australia image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer