如何对查询中的列求和以在文本框中显示结果

时间:2015-02-27 10:00:25

标签: vb.net sql-server-2008

我在查询中遇到问题,要求将总和显示在我的vb.net表单的文本框中

我有两个查询工作正常并显示结果现在我想总结两个查询的列,以在文本框中显示结果。

查询就像这样

Select 
    cl_ccode, co_scode, SUM(im_qty) as invtotal 
from 
    invmast 
where 
    cl_ccode = '3' and co_scode = '271210'
group by 
    cl_ccode, co_scode 

Select 
    cl_ccode, co_scode, SUM(st_qty) as stktotal 
from 
    stock  
where 
    cl_ccode = '3'and co_scode = '271210'
group by 
    cl_ccode, co_scode 

第一个查询的输出是

cl_ccode | co_scode | invtotal
       3 | 271210   | 132200

第二个查询的输出是

cl_ccode | co_scode | stktotal
       3 |   271210 | 386500 

我想在文本框vb.net中添加invtotal和stktotal后显示结果,即518700

请指导我如何获得所需的结果?

1 个答案:

答案 0 :(得分:0)

;with cte as
(
   Select cl_ccode,co_scode, SUM(im_qty)  as invtotal from invmast 
   where cl_ccode='3'and co_scode='271210'
   group by cl_ccode,co_scode 

   union all

   Select cl_ccode,co_scode, SUM(st_qty) as invtotal from stock  
   where cl_ccode='3'and co_scode='271210'
   group by cl_ccode,co_scode 
)

Select sum(invtotal)
from cte