我有一个Product
表,其中包含OrderStatusID
列。
以下是一个示例查询:
SELECT * FROM dbo.Product where CustomerID = 10;
问题:找到Products
的{{1}}不为空的百分比。
答案 0 :(得分:1)
SELECT CAST(COUNT(OrderStatus) AS FLOAT)/COUNT(*)*100 Percentage
FROM dbo.Product
WHERE CustomerID = 10
答案 1 :(得分:1)
最简单的方法就是这样
SELECT 100*Count(OrderStatusID)/(SELECT count(*) FROM dbo.Product) FROM dbo.Product;
希望有所帮助
答案 2 :(得分: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
答案 3 :(得分:0)
SELECT
SUM(CASE WHEN CustomerID=10 THEN 1 ELSE 0 END) / COUNT(CustomerID)
as Percentage
FROM dbo.Product