SQL 在 Access 中查询以计算非主字段子集上的记录
SQL query in Access to count records on the subset of of non-primary fields
假设我有包含以下字段的 table T:
T(F1, F2, F3, F4, F5)
其中 F1 和 F2 的组合形成一个 主键 table。
访问 SQL 对 select 以下内容的查询是什么:
T(F1, F2, F3, F4, F5, F4_F5_Count)
其中 F4_F5_Count 是字段 F4 和 F5 组合的重复计数 仅(即并非所有字段正在 selected 但只有最后两个)。?
您可以为此使用子查询:
select t.*,
(select count(*) from t as t2 where t2.f4 = f.f4 and t2.f5 = f.f5
) as fr_f5_cnt
from t;
或者,将 join
与聚合一起使用:
select t.*, tt.f4_f5_count
from t join
(select f4, f5, count(*) as f4_f5_count
from t
group by f4, f5
) tt
on t2.f4 = f.f4 and t2.f5 = f.f5;
编辑:
您将使用第二种方法并添加:
where f4_f5_count = 1
假设我有包含以下字段的 table T:
T(F1, F2, F3, F4, F5)
其中 F1 和 F2 的组合形成一个 主键 table。
访问 SQL 对 select 以下内容的查询是什么: T(F1, F2, F3, F4, F5, F4_F5_Count)
其中 F4_F5_Count 是字段 F4 和 F5 组合的重复计数 仅(即并非所有字段正在 selected 但只有最后两个)。?
您可以为此使用子查询:
select t.*,
(select count(*) from t as t2 where t2.f4 = f.f4 and t2.f5 = f.f5
) as fr_f5_cnt
from t;
或者,将 join
与聚合一起使用:
select t.*, tt.f4_f5_count
from t join
(select f4, f5, count(*) as f4_f5_count
from t
group by f4, f5
) tt
on t2.f4 = f.f4 and t2.f5 = f.f5;
编辑:
您将使用第二种方法并添加:
where f4_f5_count = 1