主题:SQL语句查询
报表要求如下:
货场工作量:2008年4月份结账统计报表
计算单位:
转堆/分钟:火车/吨:汽车/吨:过磅/吨 统计日期:2008年4月24日 统计员:某某
_________________________________________________________________________________
序号 货主名称 品名 场租 转堆 装汽车 收现金 装火车 火车到达 过磅 备注
__________________________________________________________________________________
1 黄沙坪矿 锌 1500 60 1200 0 10 0 1500 未结账
解决的问题在于我的查询语句太繁杂,一共查询的四个表(货主信息表,储运信息表,铲车作业信息表,过称记录信息表)拜托请求大虾能否给予优化呢?
Set Rs = New ADODB.Recordset
Set Recordset = New ADODB.Recordset
If Recordset.State <> 0 Then
Recordset.Close: Set Recordset = Nothing
End If
Screen.MousePointer = 11
'这里获取货主表中的(名称,品名,货场租金)三列值.
GMsql = "select Uname,Uping,URMB from tbGoodsName group by Uname,Uping,URMB order by Uname DESC"
Set Recordset = GAcn.Execute(GMsql)
If Recordset.BOF = False And Recordset.EOF = False Then
Recordset.MoveFirst: XuHao = 1
Do Until Recordset.EOF
With MSF
.AddItem Empty
.Row = .Rows - 1: .ROWHEIGHT(.Rows - 1) = 400
.TextMatrix(.Rows - 1, 0) = XuHao
.TextMatrix(.Rows - 1, 1) = Recordset.Fields(0).Value
.TextMatrix(.Rows - 1, 2) = Recordset.Fields(1).Value & " "
.TextMatrix(.Rows - 1, 3) = Recordset.Fields(2).Value
'查询铲车作业表,统计转配矿时间与收现金额.
GMsql = "select SUM(UTime),SUM(URMB) from tbcar where UDate " & Between & " and UOwner='" & Recordset.Fields(0).Value & "'" & _
" and UHomework='" & "转配矿" & "'"
Set Rs = GAcn.Execute(GMsql)
If Rs.BOF = False And Rs.EOF = False Then
.TextMatrix(.Rows - 1, 4) = Rs.Fields(0).Value & ""
.TextMatrix(.Rows - 1, 6) = Val(.TextMatrix(.Rows - 1, 5)) + Rs.Fields(1).Value & ""
Else
.TextMatrix(.Rows - 1, 4) = Val(.TextMatrix(.Rows - 1, 4)) + 0
.TextMatrix(.Rows - 1, 6) = Val(.TextMatrix(.Rows - 1, 5)) + 0
End If
Rs.Close
'********************************************************************
'查询铲车作业表,统计装汽车重量与收现金额
GMsql = "select SUM(UTime),SUM(URMB) from tbcar where UDate " & Between & _
" and UOwner='" & Recordset.Fields(0).Value & "'" & _
" and UHomework='" & "装汽车" & "'"
Set Rs = GAcn.Execute(GMsql)
If Rs.BOF = False And Rs.EOF = False Then
.TextMatrix(.Rows - 1, 5) = Rs.Fields(0).Value & ""
.TextMatrix(.Rows - 1, 6) = Val(.TextMatrix(.Rows - 1, 6)) + Rs.Fields(1).Value & ""
Else
.TextMatrix(.Rows - 1, 5) = Val(.TextMatrix(.Rows - 1, 4)) + 0
.TextMatrix(.Rows - 1, 6) = Val(.TextMatrix(.Rows - 1, 5)) + 0
End If
Rs.Close
'********************************************************************
'查询储运作业表.统计重量.
GMsql = "select SUM(UHeavy) from tbTotalwatch where UDate " & Between & _
" and UGoodsName='" & Recordset.Fields(0).Value & "'" & _
" and UFa_Dao='" & "发运" & "'"
Set Rs = GAcn.Execute(GMsql)
If Rs.BOF = False And Rs.EOF = False Then
.TextMatrix(.Rows - 1, 7) = Rs.Fields(0).Value & ""
Else
.TextMatrix(.Rows - 1, 7) = Val(.TextMatrix(.Rows - 1, 7)) + 0
End If
Rs.Close
'********************************************************************
'查询储运作业表.统计重量
GMsql = "select SUM(UHeavy) from tbTotalwatch where UDate " & Between & _
" and UGoodsName='" & Recordset.Fields(0).Value & "'" & _
" and UFa_Dao='" & "到达" & "'"
Set Rs = GAcn.Execute(GMsql)
If Rs.BOF = False And Rs.EOF = False Then
.TextMatrix(.Rows - 1, 8) = Rs.Fields(0).Value & ""
Else
.TextMatrix(.Rows - 1, 8) = Val(.TextMatrix(.Rows - 1, 8)) + 0
End If
Rs.Close
'********************************************************************
'查询地磅过重表。统计重量
GMsql = "select SUM(Ujin) from TbDiBang where UDate " & Between & _
" and Ugoods='" & Recordset.Fields(0).Value & "'"
Set Rs = GAcn.Execute(GMsql)
If Rs.BOF = False And Rs.EOF = False Then
.TextMatrix(.Rows - 1, 9) = Rs.Fields(0).Value & ""
Else
.TextMatrix(.Rows - 1, 9) = Val(.TextMatrix(.Rows - 1, 9)) + 0
End If
Rs.Close
End With
Recordset.MoveNext: XuHao = XuHao + 1
Loop
MSF.AddItem Empty
MSF.TextMatrix(MSF.Rows - 1, 1) = "合计:" & XuHao - 1 & "项"
'合计:
With MSF
Dim temp As Single
'从第三行,三列,开始
.Row = 3: .Col = 3: temp = 0
.ROWHEIGHT(.Rows - 1) = 500
For i = 3 To .Cols - 2
temp = Val(.TextMatrix(.Row, i))
For j = (.Row + 1) To .Rows - 1
temp = temp + Val(.TextMatrix(j, i))
Next
.CellFontSize = 11
.CellAlignment = flexAlignCenterCenter '内容居中、居中对齐
.TextMatrix(.Rows - 1, i) = temp & ""
temp = 0
Next
End With
Else
Betweenstr = Right(Between, Len(Between) - 9)
MsgBox "目前没有:" & Betweenstr & "的数据可查询!", vbExclamation, "查询错误"
Rs.Close: Set Rs = Nothing
Recordset.Close: Set Recordset = Nothing
Exit Function
End If
货场工作量:2008年4月份结账统计报表
计算单位:
转堆/分钟:火车/吨:汽车/吨:过磅/吨 统计日期:2008年4月24日 统计员:某某
_________________________________________________________________________________
序号 货主名称 品名 场租 转堆 装汽车 收现金 装火车 火车到达 过磅 备注
__________________________________________________________________________________
1 黄沙坪矿 锌 1500 60 1200 0 10 0 1500 未结账
解决的问题在于我的查询语句太繁杂,一共查询的四个表(货主信息表,储运信息表,铲车作业信息表,过称记录信息表)拜托请求大虾能否给予优化呢?
Set Rs = New ADODB.Recordset
Set Recordset = New ADODB.Recordset
If Recordset.State <> 0 Then
Recordset.Close: Set Recordset = Nothing
End If
Screen.MousePointer = 11
'这里获取货主表中的(名称,品名,货场租金)三列值.
GMsql = "select Uname,Uping,URMB from tbGoodsName group by Uname,Uping,URMB order by Uname DESC"
Set Recordset = GAcn.Execute(GMsql)
If Recordset.BOF = False And Recordset.EOF = False Then
Recordset.MoveFirst: XuHao = 1
Do Until Recordset.EOF
With MSF
.AddItem Empty
.Row = .Rows - 1: .ROWHEIGHT(.Rows - 1) = 400
.TextMatrix(.Rows - 1, 0) = XuHao
.TextMatrix(.Rows - 1, 1) = Recordset.Fields(0).Value
.TextMatrix(.Rows - 1, 2) = Recordset.Fields(1).Value & " "
.TextMatrix(.Rows - 1, 3) = Recordset.Fields(2).Value
'查询铲车作业表,统计转配矿时间与收现金额.
GMsql = "select SUM(UTime),SUM(URMB) from tbcar where UDate " & Between & " and UOwner='" & Recordset.Fields(0).Value & "'" & _
" and UHomework='" & "转配矿" & "'"
Set Rs = GAcn.Execute(GMsql)
If Rs.BOF = False And Rs.EOF = False Then
.TextMatrix(.Rows - 1, 4) = Rs.Fields(0).Value & ""
.TextMatrix(.Rows - 1, 6) = Val(.TextMatrix(.Rows - 1, 5)) + Rs.Fields(1).Value & ""
Else
.TextMatrix(.Rows - 1, 4) = Val(.TextMatrix(.Rows - 1, 4)) + 0
.TextMatrix(.Rows - 1, 6) = Val(.TextMatrix(.Rows - 1, 5)) + 0
End If
Rs.Close
'********************************************************************
'查询铲车作业表,统计装汽车重量与收现金额
GMsql = "select SUM(UTime),SUM(URMB) from tbcar where UDate " & Between & _
" and UOwner='" & Recordset.Fields(0).Value & "'" & _
" and UHomework='" & "装汽车" & "'"
Set Rs = GAcn.Execute(GMsql)
If Rs.BOF = False And Rs.EOF = False Then
.TextMatrix(.Rows - 1, 5) = Rs.Fields(0).Value & ""
.TextMatrix(.Rows - 1, 6) = Val(.TextMatrix(.Rows - 1, 6)) + Rs.Fields(1).Value & ""
Else
.TextMatrix(.Rows - 1, 5) = Val(.TextMatrix(.Rows - 1, 4)) + 0
.TextMatrix(.Rows - 1, 6) = Val(.TextMatrix(.Rows - 1, 5)) + 0
End If
Rs.Close
'********************************************************************
'查询储运作业表.统计重量.
GMsql = "select SUM(UHeavy) from tbTotalwatch where UDate " & Between & _
" and UGoodsName='" & Recordset.Fields(0).Value & "'" & _
" and UFa_Dao='" & "发运" & "'"
Set Rs = GAcn.Execute(GMsql)
If Rs.BOF = False And Rs.EOF = False Then
.TextMatrix(.Rows - 1, 7) = Rs.Fields(0).Value & ""
Else
.TextMatrix(.Rows - 1, 7) = Val(.TextMatrix(.Rows - 1, 7)) + 0
End If
Rs.Close
'********************************************************************
'查询储运作业表.统计重量
GMsql = "select SUM(UHeavy) from tbTotalwatch where UDate " & Between & _
" and UGoodsName='" & Recordset.Fields(0).Value & "'" & _
" and UFa_Dao='" & "到达" & "'"
Set Rs = GAcn.Execute(GMsql)
If Rs.BOF = False And Rs.EOF = False Then
.TextMatrix(.Rows - 1, 8) = Rs.Fields(0).Value & ""
Else
.TextMatrix(.Rows - 1, 8) = Val(.TextMatrix(.Rows - 1, 8)) + 0
End If
Rs.Close
'********************************************************************
'查询地磅过重表。统计重量
GMsql = "select SUM(Ujin) from TbDiBang where UDate " & Between & _
" and Ugoods='" & Recordset.Fields(0).Value & "'"
Set Rs = GAcn.Execute(GMsql)
If Rs.BOF = False And Rs.EOF = False Then
.TextMatrix(.Rows - 1, 9) = Rs.Fields(0).Value & ""
Else
.TextMatrix(.Rows - 1, 9) = Val(.TextMatrix(.Rows - 1, 9)) + 0
End If
Rs.Close
End With
Recordset.MoveNext: XuHao = XuHao + 1
Loop
MSF.AddItem Empty
MSF.TextMatrix(MSF.Rows - 1, 1) = "合计:" & XuHao - 1 & "项"
'合计:
With MSF
Dim temp As Single
'从第三行,三列,开始
.Row = 3: .Col = 3: temp = 0
.ROWHEIGHT(.Rows - 1) = 500
For i = 3 To .Cols - 2
temp = Val(.TextMatrix(.Row, i))
For j = (.Row + 1) To .Rows - 1
temp = temp + Val(.TextMatrix(j, i))
Next
.CellFontSize = 11
.CellAlignment = flexAlignCenterCenter '内容居中、居中对齐
.TextMatrix(.Rows - 1, i) = temp & ""
temp = 0
Next
End With
Else
Betweenstr = Right(Between, Len(Between) - 9)
MsgBox "目前没有:" & Betweenstr & "的数据可查询!", vbExclamation, "查询错误"
Rs.Close: Set Rs = Nothing
Recordset.Close: Set Recordset = Nothing
Exit Function
End If