表1:
表2:
sql語句:
select a.日期,a.姓名,sum(a.賣出和) as 賣出彙總,sum(b.買入和) as 買入彙總
from
(select 日期,姓名,sum(賣出) as 賣出和 from 表1 group by 日期,姓名) as a
left join
(select 日期,姓名,sum(買入) as 買入和 from 表2 group by 日期,姓名) as b
on a.日期=b.日期 and a.姓名=b.姓名
group by a.日期,a.姓名
union
select b.日期,b.姓名,sum(a.賣出和) as 賣出彙總,sum(b.買入和) as 買入彙總
right join
group by b.日期,b.姓名
結果:
另外,你圖3結果不對,B明明9月1日買了70
表1:
表2:
sql語句:
select a.日期,a.姓名,sum(a.賣出和) as 賣出彙總,sum(b.買入和) as 買入彙總
from
(select 日期,姓名,sum(賣出) as 賣出和 from 表1 group by 日期,姓名) as a
left join
(select 日期,姓名,sum(買入) as 買入和 from 表2 group by 日期,姓名) as b
on a.日期=b.日期 and a.姓名=b.姓名
group by a.日期,a.姓名
union
select b.日期,b.姓名,sum(a.賣出和) as 賣出彙總,sum(b.買入和) as 買入彙總
from
(select 日期,姓名,sum(賣出) as 賣出和 from 表1 group by 日期,姓名) as a
right join
(select 日期,姓名,sum(買入) as 買入和 from 表2 group by 日期,姓名) as b
on a.日期=b.日期 and a.姓名=b.姓名
group by b.日期,b.姓名
結果:
另外,你圖3結果不對,B明明9月1日買了70