我有一個Product
表,其中有一列OrderStatusID
。查詢以獲取客戶下訂單的百分比
下面是一個例子查詢:
SELECT * FROM dbo.Product where CustomerID = 10;
問題:找到Products
的百分比,其中OrderStatusID
不爲空。
我有一個Product
表,其中有一列OrderStatusID
。查詢以獲取客戶下訂單的百分比
下面是一個例子查詢:
SELECT * FROM dbo.Product where CustomerID = 10;
問題:找到Products
的百分比,其中OrderStatusID
不爲空。
SELECT CAST(COUNT(OrderStatus) AS FLOAT)/COUNT(*)*100 Percentage
FROM dbo.Product
WHERE CustomerID = 10
SELECT
SUM(CASE WHEN CustomerID=10 THEN 1 ELSE 0 END)/COUNT(CustomerID)
as Percentage
FROM dbo.Product
最簡單的方法是這樣的
SELECT 100*Count(OrderStatusID)/(SELECT count(*) FROM dbo.Product) FROM dbo.Product;
希望它可以幫助
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
http://www.1keydata.com/sql/sql-percent-to -total.html,可以幫助你 – box86rowh 2012-02-06 13:54:39