如何将这 2 个 SQL 命令合二为一?
Posted
技术标签:
【中文标题】如何将这 2 个 SQL 命令合二为一?【英文标题】:How to combine these 2 SQL command into one? 【发布时间】:2015-05-18 13:56:13 【问题描述】:我想创建一个同时显示年收入和支出的报告。由于我对 sql 太天真,我一次只能做一个。但是,我发现如果我想按月生成此报告,则显示我的报告需要很长时间。那么你能帮我把这两个 sql 结合起来吗?谢谢你。。
我当前的 sql 是.. 收入sql
select sum(tbt.bill_total) as bill_total,
iif(sum_vat_total is null, 0, sum_vat_total) as vat_total,
iif(sum_vat_total is null, 0, sum_vat_total) + sum(bill_total) as sum_of_all,
month(showndate) as [month]
from tbl_bill_total tbt
left join (select sum(vat_total) as sum_vat_total, month(showndate) as [month]
from tbl_vat_bill_total
where if_paid = true
and year([showndate]) =" & current_year & "
group by month(showndate)) tvt on tvt.month = month(tbt.showndate)
where year([showndate]) =" & current_year & "
group by month(showndate), sum_vat_total
费用
SELECT Sum(tbl_expense.exp_amt) AS SumOfexp_amt,
Year([tbl_expense].[exp_inv_date]) AS yr,
Month([tbl_expense].[exp_inv_date]) AS mth
FROM tbl_expense
GROUP BY Year([tbl_expense].[exp_inv_date]), Month([tbl_expense].[exp_inv_date])
HAVING (((Year([tbl_expense].[exp_inv_date]))=" & current_year & "))
以下是我目前实现的用于生成收入和支出报告的代码:
<table cellspacing="0" cellpadding="0" border="0" class="border" align="center">
<tr bgcolor="#dddddd" >
<td align="center" class="border"><font class="bold">Month (th)</font></td>
<td align="center" class="border"><font class="bold">Year</font></td>
<td class="border" align="center"><font class="bold">Total (Baht)</font></td>
<td class="border" align="center"><font class="bold">Vat Total (Baht)</font></td>
<td class="border" align="center"><font class="bold">Gross Total (Baht)</font></td>
<td class="border" align="center"><font class="bold red">Expense (Baht)</font></td>
</tr>
<%
if rsBill.eof then
call displayNotFoundRecord
Else
Dim Cmonth, CLoop
Cmonth = 1
CLoop = 1
Do while Not rsBill.EOF
dim color
y = n mod 2
if y > 0 then
color = "EFF4FA"
else
color = "ffffff"
end if
%>
<tr bgcolor="#<%=color%>">
<td align="center"><%=rsBill.Fields.item("month")%></td>
<td align="center"><%=current_year%></td>
<%
Dim SumOfbill_total, SumOfVat_total, SumOfAll
SumOfbill_total = rsBill.Fields.item("bill_total")
SumOfVat_total = rsBill.Fields.item("vat_total")
SumOfAll = SumOfbill_total + SumOfVat_total
%>
<td align="right" style="padding-right:5%"><%=formatnumber(SumOfbill_total,2)%></td>
<td align="right" style="padding-right:5%"><%=formatnumber(SumOfVat_total,2)%></td>
<td align="right" style="padding-right:5%" class="blue"><%=formatnumber(SumOfAll,2)%></td>
<td align="right" style="padding-right:5%;" class="red">
<%
if rsExp.eof then
response.write "No matches."
Else
Do while Not rsExp.EOF
if rsExp.Fields.item("mth") = rsBill.Fields.item("month") then
%>
<%=formatnumber(rsExp.Fields.item("SumOfexp_amt"),2)%>
<%
end if
%>
<%
rsExp.movenext
Cmonth = Cmonth + 1
n = n + 1
%>
<%
Loop 'end rsExp Loop
End if 'end of rsExp
SumOfYear = SumOfAll + SumOfYear
rsExp.movefirst
%>
</td>
</tr> <!-- end of loop table -->
<%
rsBill.movenext
Loop
rsBill.movefirst
End if
%>
<tr bgcolor="#FFFFCC">
<td colspan="4" align="right">Gross Total </td>
<td align="right" style="padding-right:5%" class="green bold"><%=formatnumber(SumOfYear,2)%></td>
<td align="right" style="padding-right:5%" class="red bold"><%=formatnumber(rsSumExp.Fields.item("SumOfexp"),2)%></td>
</tr>
</table>
【问题讨论】:
@hypetech,现在是早上 7.23。我的时间和检查这篇文章是我工作时做的第一件事。我没有看到我如何忽略某人的尝试。相比之下,我非常感谢所有的帮助。无论如何,在这篇文章中,我认为我已经努力解决我的问题。如果你阅读了我所有的代码,你会发现它有效!!但是,正如我在帖子中所写的,我需要更有效的方法来提出资源消耗较少的报告。请尝试以乐观的方式理解人们。我们是来互相帮助的,不是吗?? 【参考方案1】:UNION set operator 是我过去使用的。这可能会创建一个庞大的查询,可能会也可能不会。
select sum(tbt.bill_total) as bill_total,
iif(sum_vat_total is null, 0, sum_vat_total) as vat_total,
iif(sum_vat_total is null, 0, sum_vat_total) + sum(bill_total) as sum_of_all,
month(showndate) as [month]
from tbl_bill_total tbt
left join (select sum(vat_total) as sum_vat_total, month(showndate) as [month]
from tbl_vat_bill_total
where if_paid = true
and year([showndate]) =" & current_year & "
group by month(showndate)) tvt on tvt.month = month(tbt.showndate)
where year([showndate]) =" & current_year & "
group by month(showndate), sum_vat_total
UNION ALL
SELECT Sum(tbl_expense.exp_amt) AS SumOfexp_amt,
Year([tbl_expense].[exp_inv_date]) AS yr,
Month([tbl_expense].[exp_inv_date]) AS mth
FROM tbl_expense
GROUP BY Year([tbl_expense].[exp_inv_date]), Month([tbl_expense].[exp_inv_date])
HAVING (((Year([tbl_expense].[exp_inv_date]))=" & current_year & "))
我可能建议的唯一其他方法是编程方法,将第一个查询的结果存储到一个数组中,然后将第二个查询的结果附加到同一个数组中(确保您使用相同的表列),那么输出还是使用数组呢?
【讨论】:
非常感谢您的帮助。但是,当我运行此 sql 时,它说“两个选定表中的列数或联合查询的查询不匹配。” 是的,您只能合并两个选择相同列的查询。否则你为什么要结合查询呢?听起来程序化方法是最适合您的方法。以上是关于如何将这 2 个 SQL 命令合二为一?的主要内容,如果未能解决你的问题,请参考以下文章