我有一個要求來計算客戶在第一季度內第一季度內客戶的百分比,並且狀態爲'可用'。我正在做這樣的事情,但沒有給出正確的結果。Mysql使用group by子句計算百分比?
SELECT DISTINCT Customer, COUNT(1)/(select count(1) from table where QUARTER(`Creation_Date`) = 1)
FROM table
WHERE Customer is not null and QUARTER(`Creation_Date`)=1 and `Status` = 'Available'
GROUP BY Customer
我想通過「客戶」在第一內選擇即什麼要補充的組像下面但這顯然返回多個行。
select count(1)/select count(1) from table where QUARTER(
CREATION_DATE ) = 1 and Customer = 'ABC'
目前其計算像例如二百二十五分之四* 100。
需要個像這樣使用GROUPBY:
爲每一個客戶:
X = select count(1) from table where QUARTER(`Creation_Date`) = 1 and Customer = 'X' and Status = 'Available';
Y = select count(1) from table where QUARTER(`Creation_Date`) = 1 and Customer = 'X';
Z = X/Y*100
可有人請引導?
不要混合'distinct'和'group by'。你可能會迷惑自己。 – GolezTrol