我环顾了四周,但我似乎无法理解逻辑。我认为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发布于 2015-12-13 01:59:51
如果你需要每个种族的信用卡交易百分比,你必须将每个种族的信用卡交易除以同一种族的交易总数。您不需要对此进行子查询:
SELECT Ethnicity, sum(IIF(CardID=1,0,1))/count(1) AS CardUsePercentage
FROM TransactionT
INNER JOIN CustomerT
ON TransactionT.CustomerID = CustomerT.CustomerID
GROUP BY Ethnicityhttps://stackoverflow.com/questions/34245536
复制相似问题