如何计算一个值在多个字段中出现的次数
How to get a count of how many times a value is present in multiple fields
我正在构建一个跟踪系统,其中最多三名团队成员可以负责每个项目。我正在尝试构建一个查询来计算团队成员的姓名 ([FullName]) 在任何负责字段 ([Responsible]、[Responsible2]、[Responsible3]) 中出现的次数。
所以对于这个例子,我想要一个查询,它会在我的团队 table 和我负责的 table 中查找并输出类似这样的内容。
我试过了
SELECT tblTeam.FullName, Count([FullName]=[Responsible] Or [Fullname]=[Responsible2] Or [FullName]=[Responsible3]) AS CountResp
FROM tblTeam, qryItems
GROUP BY tblTeam.FullName;
但我得到的结果是,应该有零值的人没有,一些应该有正值的人的数字太高,而一个有很多负责任的人的数字太低。谁能指出我的菜鸟错误?
一种简单的方法是合并三列,然后使用 count(*) 进行分组。类似于以下内容:
select name, count(*) from(
select resp1 as name from table1
union all
select resp2 as name from table1
union all
select resp3 as name from table1)
group by name
一种方法:
SELECT FullName,
SUM(
(select count(*) from qryItems where Responsible = FullName) +
(select count(*) from qryItems where Responsible2 = FullName) +
(select count(*) from qryItems where Responsible3 = FullName))
FROM tblTeam
GROUP BY FullName
我正在构建一个跟踪系统,其中最多三名团队成员可以负责每个项目。我正在尝试构建一个查询来计算团队成员的姓名 ([FullName]) 在任何负责字段 ([Responsible]、[Responsible2]、[Responsible3]) 中出现的次数。
所以对于这个例子,我想要一个查询,它会在我的团队 table 和我负责的 table 中查找并输出类似这样的内容。
我试过了
SELECT tblTeam.FullName, Count([FullName]=[Responsible] Or [Fullname]=[Responsible2] Or [FullName]=[Responsible3]) AS CountResp
FROM tblTeam, qryItems
GROUP BY tblTeam.FullName;
但我得到的结果是,应该有零值的人没有,一些应该有正值的人的数字太高,而一个有很多负责任的人的数字太低。谁能指出我的菜鸟错误?
一种简单的方法是合并三列,然后使用 count(*) 进行分组。类似于以下内容:
select name, count(*) from(
select resp1 as name from table1
union all
select resp2 as name from table1
union all
select resp3 as name from table1)
group by name
一种方法:
SELECT FullName,
SUM(
(select count(*) from qryItems where Responsible = FullName) +
(select count(*) from qryItems where Responsible2 = FullName) +
(select count(*) from qryItems where Responsible3 = FullName))
FROM tblTeam
GROUP BY FullName