如何将这两个SQL命令合并为一个?

时间:2015-05-18 13:56:14

标签: sql ms-access asp-classic

我想创建一个同时显示年收入和费用的报告。因为我对sql太天真了,所以我一次只能做一个。但是,如果我想每月生成此报告,我发现显示我的报告需要花费大量时间。那么请你帮我把这2个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" width="80%" class="border" align="center">
    <tr bgcolor="#dddddd" >
       <td width="10%" align="center" class="border"><font class="bold">Month (th)</font></td>
        <td width="10%" align="center" class="border"><font class="bold">Year</font></td>                            
        <td  width="20%" class="border" align="center"><font class="bold">Total (Baht)</font></td>
        <td  width="20%" class="border" align="center"><font class="bold">Vat Total (Baht)</font></td>
        <td  width="20%" class="border" align="center"><font class="bold">Gross Total (Baht)</font></td>
        <td  width="20%" 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>

1 个答案:

答案 0 :(得分:0)

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 & ")) 

我可能建议的唯一其他方法是程序方法,将第一个查询的结果存储到数组中,然后将第二个查询的结果附加到同一个数组中(确保使用相同的表列),然后输出或使用数组?