mssql sqlserver两条求和sql脚本相加的方法分享

来源:http://www.sh-fengwen.com 作者:气血双补 人气:152 发布时间:2019-11-19
摘要:摘自: 转自:http://www.maomao365.com/?p=7205 摘要: 下文介绍sql server中,sql脚本防止现身除零不当的主意分享 摘要: 下文分享两条sql求和本子,再一次求和的不二诀窍分享 在各样事务系统开拓中

摘自:

转自:http://www.maomao365.com/?p=7205

摘要:
下文介绍sql server中,sql脚本防止现身除零不当的主意分享

摘要:


下文分享两条sql求和本子,再一次求和的不二诀窍分享

在各样事务系统开拓中,平常会越过巳零的大错特错,下文分享了二种管理方法:
方法1: case when end 

 /*
     例:
       下文已知两条sql求和脚本,现需对两张不同表的求和记录再次求和   
     */  
     ---对两条求和sql脚本求和的方法
      select sum(q) from 
      (
       select sum(qty)  as q from tableNameA where ...
       union all 
        select sum(qty)  as q from tableNameB where ...
       ) as table

     ---对多条求和sql脚本,再次求和的方法分享
     select sum(q) from 
      (
       select sum(qty)  as q from tableNameA where ...
       union all 
        select sum(qty)  as q from tableNameB where ...
         union all 
        select sum(qty)  as q from tableNameC where ...
        union all 
        select sum(qty)  as q from tableNameD where ...
       ...  
    ) as table
declare @a int ---分子
declare @b int ---分母
select  case  when @b=0 then NULL else @a/@b end  as [a除以b]

 

本文由美高梅游戏平台网站发布于气血双补,转载请注明出处:mssql sqlserver两条求和sql脚本相加的方法分享

关键词:

最火资讯