我获得了一个使用从excel电子表格导入的表格。表是CustomerData表,其中包含以下属性:年龄,收入,性别等。
我被告知要在Access中编写这些查询: 收入查询:
一个。对于每5年递增的客户年龄组1至5; 6到10; 11-15等产生了平均收入并显示N(该年龄组的数量)
对于这个查询,我写了这个,但我知道必须有更好的方法:
SELECT Avg(CustomerData.Income) AS Average_Income_of_Age_Groups, Count(CustomerData.Age) AS N_Count
FROM CustomerData
Where (((CustomerData.Age)>5 And (CustomerData.Age)<11))
UNION
SELECT Avg(CustomerData.Income) AS Avg_Income_Of_Ages_10_to_15, Count(CustomerData.Age) AS N_Count
FROM CustomerData
Where (((CustomerData.Age)>10 And (CustomerData.Age)<16))
我一直在重复,直到我年满85岁。但是这个查询缺乏对结果集中年龄组的正确标记。
湾对于每5年递增的客户年龄组和性别M,F,产生平均收入并显示N(该年龄性别组的计数)
我刚刚开始写这篇文章,但我迷路了...到目前为止,我有类似的东西会返回某个年龄段男性或女性的平均收入:
SELECT Avg(Income) AS Average_Income,
(Select Count (*)
From CustomerData
Where Gender = "M" And Age Between 5 and 20) AS Gender_Male
FROM CustomerData
WHERE Age Between 5 and 20 And Gender = "M";
℃。对于每5年递增的客户年龄组和性别1至5; 6到10; 11-15等产生每个家庭成员的平均收入(家庭/收入内的成员)
如果有人能提供帮助,我们将不胜感激。
答案 0 :(得分:0)
A)
SELECT
CStr(([CustomerData].[Age]-[CustomerData].[Age] Mod 5) + 1) & "-" & CStr([CustomerData].[Age]+(5-([CustomerData].[Age] Mod 5))) AS AgeRange
,Avg(CustomerData.Income) AS AvgOfIncome
,Count(*) AS NumberOfCustomers
FROM
CustomerData
GROUP BY
CStr(([CustomerData].[Age]-[CustomerData].[Age] Mod 5) + 1) & "-" & CStr([CustomerData].[Age]+(5-([CustomerData].[Age] Mod 5)))
,([CustomerData].[Age]-[CustomerData].[Age] Mod 5) + 1
ORDER BY
([CustomerData].[Age]-[CustomerData].[Age] Mod 5) + 1
;
B)
SELECT
CStr(([CustomerData].[Age]-[CustomerData].[Age] Mod 5) + 1) & "-" & CStr([CustomerData].[Age]+(5-([CustomerData].[Age] Mod 5))) AS AgeRange
,CustomerData.Gender
,Avg(CustomerData.Income) AS AverageIncome
,Count(*) AS NumberOfCustomers
FROM
CustomerData
GROUP BY
CStr(([CustomerData].[Age]-[CustomerData].[Age] Mod 5) + 1) & "-" & CStr([CustomerData].[Age]+(5-([CustomerData].[Age] Mod 5)))
,CustomerData.Gender
,([CustomerData].[Age]-[CustomerData].[Age] Mod 5) + 1
ORDER BY
([CustomerData].[Age]-[CustomerData].[Age] Mod 5) + 1
;
C)
SELECT
CStr((CustomerData.Age-CustomerData.Age Mod 5) + 1) & "-" & CStr(CustomerData.Age+(5-(CustomerData.Age Mod 5))) AS AgeRange
,CustomerData.MembersWithinHousehold
,Avg(CustomerData.Income) AS AverageIncome
,Count(*) AS NumberOfCustomers
FROM
CustomerData
GROUP BY
CStr((CustomerData.Age-CustomerData.Age Mod 5) + 1) & "-" & CStr(CustomerData.Age+(5-(CustomerData.Age Mod 5)))
,CustomerData.MembersWithinHousehold
,(CustomerData.Age-CustomerData.Age Mod 5) + 1
ORDER BY
(CustomerData.Age-CustomerData.Age Mod 5) + 1
,CustomerData.MembersWithinHousehold
;
[CustomerData].[Age] Mod 5
- &gt;正在使用模块划分(即将年龄除以5并将余数作为答案)。我们将客户年龄除以5,因为这是您希望年龄范围“桶”的大小。 如果某人是21 ,则此计算结果为:21 mod 5 = 1
。我们稍后从年龄中减去这个数量,以找到年龄范围桶的开始。([CustomerData].[Age] - [CustomerData].[Age] Mod 5) + 1
- &gt;这会找到年龄范围桶的开头。因此,如果某人是21,则此计算为:(21 - 1) + 1 = 20
5 - ([CustomerData].[Age] Mod 5)
- &gt;这用于查找年龄范围桶的结束。再次使用我们21岁的客户,计算结果为:5 - (1) = 4
[CustomerData].[Age] + (5 - ([CustomerData].[Age] Mod 5)
- &gt;找到年龄范围桶的结尾。计算结果为:21 + 4 = 25
CStr(([CustomerData].[Age]-[CustomerData].[Age] Mod 5) + 1) & "-" & CStr([CustomerData].[Age]+(5-([CustomerData].[Age] Mod 5))) AS AgeRange
- &gt;这只是建立一个字符串,遵循21岁的结构,即:"21 - 25"
GROUP BY
允许我们对这些组执行聚合(计数,总和,平均等),因此年龄范围计算是第一项在GROUP BY
子句之后列出。 GROUP BY
项只是为了允许我在ORDER BY
子句中对其上的数据集进行排序。这只是年龄范围桶的开始,并且在计算上也比整个年龄范围的桶字符串“更便宜”。