access 三表查询求和问题
表结构如下:a表b表:C表:想要按name分组进行求和,id、name类型为文本,aa、bb类型为数字想要得到的结果如下:试过如下语句一直提示标准表达式中数据类型不匹配:...
表结构如下:a表
b表: C表:
想要按name分组进行求和,id、name类型为文本,aa、bb类型为数字
想要得到的结果如下:
试过如下语句一直提示标准表达式中数据类型不匹配:
SELECT name, sum(a1)as 和1, sum(b1)as 和2
FROM (select name,sum(aa) as a1,''as b1 from(
select a.* ,aa from a left join b on a.id=b.id )a
group by name
union all
select name,''as a1,sum(bb) as b1 from(
select a.* ,bb from a left join c on a.id=c.id )a
group by name) AS a
GROUP BY name;
求高手解惑~谢谢! 展开
b表: C表:
想要按name分组进行求和,id、name类型为文本,aa、bb类型为数字
想要得到的结果如下:
试过如下语句一直提示标准表达式中数据类型不匹配:
SELECT name, sum(a1)as 和1, sum(b1)as 和2
FROM (select name,sum(aa) as a1,''as b1 from(
select a.* ,aa from a left join b on a.id=b.id )a
group by name
union all
select name,''as a1,sum(bb) as b1 from(
select a.* ,bb from a left join c on a.id=c.id )a
group by name) AS a
GROUP BY name;
求高手解惑~谢谢! 展开
1个回答
展开全部
这里假设a表的id字段是唯一的且分别与b表和c表的id字段建立了一对多参照完整性。
实现代码如下:实现代码如下:
select a.[name],t.a as 和1,t.b as 和2 from a,
(select t1.id,sum(t1.aa) as a,sum(t1.bb) as b from
(select id,aa,0 as bb from b union all
select id,0,bb from c)t1 group by t1.id)t
where a.id=t.id;
如有疑问请发追问
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询