【发布时间】:2023-03-09 16:35:01
【问题描述】:
我环顾四周,但似乎无法理解其中的逻辑。我认为一个好的回复是here,但就像我说的那样,它没有意义,所以更具体的解释将不胜感激。
所以我想展示每个种族的客户使用信用卡的频率。有不同类型的信用卡,但如果 CardID = 1,它们使用现金(因此不等于 1 语句)。
我想按种族分组并显示交易计数,但显示为百分比。
SELECT Ethnicity, COUNT(distinctCard.TransactionID) AS CardUseCount
FROM (SELECT DISTINCT TransactionID, CustomerID FROM TransactionT WHERE CardID <> 1)
AS distinctCard INNER JOIN CustomerT ON distinctCard.CustomerID = CustomerT.CustomerID
GROUP BY Ethnicity
ORDER BY COUNT(distinctCard.TransactionID) ASC
例如,这就是它提出的:
Ethnicity | CardUseCount
0 | 100
1 | 200
2 | 300
3 | 400
但我想要这个:
Ethnicity | CardUsePer
0 | 0.1
1 | 0.2
2 | 0.3
3 | 0.4
【问题讨论】:
-
看来您只是在计算交易数量。为了找到一个百分比,您需要获得交易总数并将每个计数除以该百分比。所以: CardUsePer = (CardUseCount / TotalTransactions)
-
是的,我知道这一点,但我不知道如何编程,因为我知道计算总交易的唯一方法是通过 SELECT,但它已经按种族分组。
-
在您链接的示例中,子查询用于返回记录总数。尝试类似: COUNT(distinctCard.TransactionID) / (select count(1) from TransactionT where CardID 1) as CardUsePer
标签: sql ms-access count percentage