简体   繁体   English

找到不同表的两列之和,然后减去它们的总和

[英]Find sum of two columns of different tables then subtract their sum

hi I want to make a query that first find the sum of two columns of different table then subtract it Example it is a visual basic program 嗨,我想做一个查询,首先找到不同表的两列之和,然后减去它。

Dim large_tbl As String
Dim sell_large As String

large_tbl = "SELECT Sum(No_Of_Bottle) FROM add_cotton where Cateogry='Large'"
sell_large = "SELECT Sum(Quantity) FROM Sell_Detail where Cateogry='Large'"

Adodc2.RecordSource = large_tbl - sell_large

Please help it shows an error type miss match 请帮助它显示错误类型错过匹配

The two variables large_tbl and sell_large are strings -- just text as far as VB is concerned. 两个变量large_tblsell_large是字符串-就VB而言只是文本。 You need to connect to a data source (ie a Sql Server) and execute a query to get a numerical result. 您需要连接到数据源(即Sql Server)并执行查询以获取数值结果。

What you've asked the computer to do is subtract "blah" from "blah" and execute it. 您要求计算机执行的操作是从“ blah”中减去“ blah”并执行它。

What I guess you meant is something more like: 我想你的意思是更像:

Dim oCNX as ADODB.Connection

:----Put code in here to open the connection to your database

Dim resultA as ADODB.Recordset
Dim resultB as ADODB.Recordset

Dim large_tbl As String
Dim sell_large As String

large_tbl = "SELECT Sum(No_Of_Bottle) FROM add_cotton where Cateogry='Large'"
sell_large = "SELECT Sum(Quantity) FROM Sell_Detail where Cateogry='Large'"

Set resultA = oCNX.Execute(large_tbl)
Set resultB = oCNX.Execute(sell_large)

Do while not oRS.Eof()

   Debug.Print "Result =" & (resultA(1).Value - resultB(1).Value)

   oRS.MoveNext
Loop

Or something like that. 或类似的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM