我的数据库中有两个查询,它们从不同的表中返回总数,然后运行第三个查询来比较这些总数?
我需要在单个语句中执行此操作,以便我可以在我的asp.net站点上发布。这些查询可以合并吗?
以下是三个查询
qry_control_d_total
SELECT
tbl_control_d.processed_date,
Sum(tbl_control_d.dc_amt)
AS
SumOfdc_amt,
tbl_currency.cur_type
FROM
tbl_control_d
INNER JOIN
tbl_currency
ON
tbl_control_d.cur_code = tbl_currency.cur_code
GROUP BY
tbl_control_d.processed_date, tbl_currency.cur_type
AS
qry_control_d_total;
qry_sap_total
SELECT
tbl_sap.doc_date,
Sum
(tbl_sap.dc_amt)
AS
SumOfdc_amt,
tbl_sap.cur_type
FROM
tbl_sap
GROUP BY
tbl_sap.doc_date,
tbl_sap.cur_type
AS
qry_sap_total;
最终查询
SELECT
qry_control_d_total.processed_date,
qry_control_d_total.cur_type,
[qry_control_d_total].[sumofdc_amt]-[qry_control_d_total].[sumofdc_amt]
AS
balance,
qry_control_d_total.SumOfdc_amt,
qry_sap_total.SumOfdc_amt
FROM
qry_control_d_total
INNER JOIN
qry_sap_total
ON
(qry_control_d_total.processed_date = qry_sap_total.doc_date)
AND
(qry_control_d_total.cur_type = qry_sap_total.cur_type);
欢迎任何建议
这是我在我的vb.net页面中使用的子
sub page_load
lblyear.text = dateTime.Now.ToString("yyyy")
Dim objconn as new oledbconnection("provider=Microsoft.jet.oledb.4.0;data source=F:\CCMI\asp\db\control.mdb")
Dim objcmd As Oledbcommand
Dim objRdr as OledbDatareader
Dim strCmd as string
Dim objDA as oleDbDataAdapter
if not ispostback then
'****************
objcmd = new oledbcommand("SELECT...", objconn)
objconn.open()
objRdr = objcmd.executereader()
dgbal_mc_roi.datasource = objrdr
dgbal_mc_roi.databind()
objrdr.close()
objconn.close()
end if
end sub
答案 0 :(得分:0)
您几乎就在那里,您需要将以前的查询用作派生表:
SELECT
qry_control_d_total.processed_date,
qry_control_d_total.cur_type,
qry_control_d_total.[sumofdc_amt]-qry_control_d_total.[sumofdc_amt]
AS
balance,
qry_control_d_total.SumOfdc_amt,
qry_sap_total.SumOfdc_amt
FROM ( SELECT tbl_control_d.processed_date, Sum(tbl_control_d.dc_amt) AS SumOfdc_amt, tbl_currency.cur_type
FROM tbl_control_d
INNER JOIN tbl_currency
ON tbl_control_d.cur_code = tbl_currency.cur_code
GROUP BY tbl_control_d.processed_date, tbl_currency.cur_type) AS qry_control_d_total
INNER JOIN (SELECT tbl_sap.doc_date, Sum(tbl_sap.dc_amt) AS SumOfdc_amt, tbl_sap.cur_type
FROM tbl_sap
GROUP BY tbl_sap.doc_date, tbl_sap.cur_type) AS qry_sap_total
ON qry_control_d_total.processed_date = qry_sap_total.doc_date
AND qry_control_d_total.cur_type = qry_sap_total.cur_type
答案 1 :(得分:-1)
尝试做一件事
如果您在存储过程中执行这些查询,
将第一个查询的结果放入临时表中 第二个表的结果变成了另一个临时表
为这两个临时表写一个连接查询!