sql 有重复字段的多个表查询时,数据被覆盖怎么解决?

select a.no
,sum(case when a.index = 1 then a.value else 0 end)/count(distinct a.calcdate) as aa
,sum(case when c.index = 5 then c.value else 0 end)/count(distinct c.calcdate) as cc
from A a join C c on a.bid = c.bid
group by a.no

表A和表C字段相同,都有no,index,value,calcdate,现在运行后c的值会覆盖a的值运算
求不会覆盖的方法
最新回答
╭⌒浅浅笑

2024-05-06 06:43:12

select a.no
,sum(case when a.index = 1 then a.value else 0 end)/count(distinct a.calcdate) as aa
from A
group by a.no
union all
select c.no
,sum(case when c.index = 5 then c.value else 0 end)/count(distinct c.calcdate) as cc
from C
group by C.no
追问
运行后没有cc那列,只有no和aa
追答
select no
,sum(case when index = 1 then value else 0 end)/count(distinct calcdate) as aa
from A
group by no
union all
select no
,sum(case when index = 5 then value else 0 end)/count(distinct calcdate) as aa
from C
group by no
这样看下数据对不对,上下查询的结果都放同一个字段里面
追问
不行的
追答
我后面这个代码运行完是怎么样的,截图看下