sqlsever如何将表1和表2 并表查询,得出某年某月某日的出库减退货的金额,按照组织分组
select
sum(case when c.类型='出库' then c.金额 else 0 end)
-
sum(case when c.类型='退货' then c.金额 else 0 end)
form (select * from a UNION ALL select * from b) as c group by c.组织,c.年, c.月, c.日
select a.年, a.月, a.日, a.类型, a.金额, a.组织,b.类型, b.金额, b.组织 from a left join b on a.年=b.年 and a.月=b.月 and a.日=b.日 group by a.组织
select t1.年,t1.月,t1.日,t1.组织,sum(t1.类型) - sum(t2.类型) from table1 t1,table2 t2 where t1.年= t2.年 and t1.月 = t2.月 and t1.日 = t2.日 and t1.组织 = t2.组织 group by t1.年,t1.月 ,t1.日 ,t1.组织;
两表合并查询?是将两张表合成一张表再进行查询吗?那可以将两张表连接成一张表,再将它当成数据源去查找不就行了。要某年某月某日的出库减掉退货的金额总和,可以组织,年月日分组,然后sum(表1.金额)-sum(表2.金额)。
你现在是遇到了什么问题?