如何获取访问中的连接行的总和

时间:2016-04-06 09:48:06

标签: sql ms-access

我的数据表是:enter image description here

我需要的表是: enter image description here

我使用函数来连接ms访问中的行,如下所示:

    Public Function GetList(SQL As String _
                            , Optional ColumnDelimeter As String = ", " _
                            , Optional RowDelimeter As String = vbCrLf) As String
'PURPOSE: to return a combined string from the passed query
'ARGS:
'   1. SQL is a valid Select statement
'   2. ColumnDelimiter is the character(s) that separate each column
'   3. RowDelimiter is the character(s) that separate each row
'RETURN VAL: Concatenated list
'DESIGN NOTES:
'EXAMPLE CALL: =GetList("Select Col1,Col2 From Table1 Where Table1.Key = " & OuterTable.Key)

Const PROCNAME = "GetList"
Const adClipString = 2
Dim oConn As ADODB.Connection
Dim oRS As ADODB.Recordset
Dim sResult As String

On Error GoTo ProcErr

Set oConn = CurrentProject.Connection
Set oRS = oConn.Execute(SQL)

sResult = oRS.GetString(adClipString, -1, ColumnDelimeter, RowDelimeter)

If Right(sResult, Len(RowDelimeter)) = RowDelimeter Then
    sResult = Mid$(sResult, 1, Len(sResult) - Len(RowDelimeter))
End If

GetList = sResult
oRS.Close
oConn.Close

CleanUp:
    Set oRS = Nothing
    Set oConn = Nothing

Exit Function
ProcErr:
    ' insert error handler
    Resume CleanUp

End Function

我使用的查询是:

SELECT OB.Operation_Type, OB.Machine_Type, OB.Attatchment, GetList("Select Operation_Name From OB As T1 Where T1.Operation_Type = """ & [ob].[Operation_Type] & """ and  T1.Machine_Type = """ & [ob].[Machine_Type] & """ and  T1.Attatchment = """ & [ob].[Attatchment] & """ ",""," + ") AS Expr1 
FROM ob
GROUP BY ob.Operation_Type, Machine_Type, Attatchment;

给我结果

enter image description here

我需要连续行的SAM的总和。 请帮忙 谢谢

1 个答案:

答案 0 :(得分:0)

如果SAM是数字字段,您只需添加SUM()聚合即可。除非我遗漏了什么。

SELECT OB.Operation_Type, OB.Machine_Type, OB.Attatchment, 
  GetList("Select Operation_Name From OB As T1 Where T1.Operation_Type = """ & [ob].[Operation_Type] & """ and  T1.Machine_Type = """ & [ob].[Machine_Type] & """ and  T1.Attatchment = """ & [ob].[Attatchment] & """ ",""," + ") AS Expr1,
  SUM([SAM]) AS SumSAM
FROM ob
GROUP BY ob.Operation_Type, Machine_Type, Attatchment;