How To Do Percent/Total in SQL?

前端 未结 4 1597
梦如初夏
梦如初夏 2021-01-07 17:12

I have an typical CUSTOMER/ORDERS set of tables and I want to display the total percentage of sales a particular customer is responsible for. I can get the

4条回答
  •  渐次进展
    2021-01-07 17:15

    select max([order].customerid) customer_id, count(orderid) customer_orders, (select count(orderid) from [order]) as total_orders,
    100.0 * (count(orderid))/(select count(orderid) from [order])
    from [order] inner join customer
    on [order].customerid = customer.customerid
    group by [order].customerid
    

    To illustrate the flow, I have included more columns than you need to see in the final result set. Holding the count(order_id) in a temporary variable will be more efficient. I'm not used to postgres, I hope this works with minimal modification.

提交回复
热议问题