2012-02-06 71 views
0

我有一個Product表,其中有一列OrderStatusID查詢以獲取客戶下訂單的百分比

下面是一個例子查詢:

SELECT * FROM dbo.Product where CustomerID = 10; 

問題:找到Products的百分比,其中OrderStatusID不爲空。

+0

http://www.1keydata.com/sql/sql-percent-to -total.html,可以幫助你 – box86rowh 2012-02-06 13:54:39

回答

1
SELECT CAST(COUNT(OrderStatus) AS FLOAT)/COUNT(*)*100 Percentage 
FROM dbo.Product 
WHERE CustomerID = 10 
0
SELECT 
    SUM(CASE WHEN CustomerID=10 THEN 1 ELSE 0 END)/COUNT(CustomerID) 
    as Percentage 
FROM dbo.Product 
1

最簡單的方法是這樣的

SELECT 100*Count(OrderStatusID)/(SELECT count(*) FROM dbo.Product) FROM dbo.Product; 

希望它可以幫助

1
declare @total numeric(10,2) 
select @total =count(*) from orders 

select customerID, cast(100*count(*)/@total as numeric(10,2)) as Percentage 
from orders 
where customerID=10 
group by customerID