发布于 2022-12-04 07:07:28
详细阐述6月7日的评论,并假设Table2:
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| ID | FinishedProductid | ItemNumber | Component | Uom | ComponentQuantity | ComponentUnit | stdCost |
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| 1 | 199127402 | 10 | 123123123 | PC | 3 | PC | $1.50 |
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| 2 | 199127402 | 20 | 321321321 | PC | 1 | PC | $2.50 |
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| 3 | 199127402 | 30 | 123456789 | PC | 1 | PC | $3.55 |
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| 4 | 199127402 | 40 | 987654321 | PC | 0.25 | H | $82.00 |
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| 5 | 199127403 | 10 | 111222333 | PC | 3 | PC | $1.50 |
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| 6 | 199127403 | 20 | 333222111 | PC | 1 | PC | |
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| 7 | 199127403 | 30 | 444555666 | PC | 1 | PC | $3.55 |
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| 8 | 199127403 | 40 | 666555444 | PC | 0.25 | H | $82.00 |
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

'resulting sql to get all FinishedProductids
SELECT Table2.FinishedProductid
FROM Table2
GROUP BY Table2.FinishedProductid;

'once have FinishedProductid's out of the group statement you can just use a calculated variable
SELECT Query2.FinishedProductid, getTotalCost([FinishedProductid]) AS TotalCost
FROM Query2;'of course a getTotalCost function is needed. Add the following to a code module
Public Function getTotalCost(FinishedProductid As Long) As String
If isPending(FinishedProductid) Then
getTotalCost = "Pending"
Else
getTotalCost = DSum("ComponentQuantity*stdCost", "Table2", "FinishedProductid = " & FinishedProductid)
End If
End Function
Public Function isPending(FinishedProductid) As Boolean
' if any values of stdCost are null set isPending to true
'public functions can be accessed most anywhere in Access
Dim nullCount As Long
nullCount = DCount("ID", "Table2", "FinishedProductid = " & FinishedProductid & " AND ISNULL(stdCost)")
If nullCount > 0 Then
isPending = True
Else
isPending = False
End If
End Function'result
-------------------------------------------------------
| FinishedProductid | TotalCost |
-------------------------------------------------------
| 199127402 | 31.05 |
-------------------------------------------------------
| 199127403 | Pending |
-------------------------------------------------------说明:我使用了两种查询方法来避免来自组的sql复杂性。这些功能相对来说是解密的,因此可以自我评论。最后,函数可以重用。
编辑: getTotalCost返回一个字符串,以满足返回字符串“待定”和总成本的要求。
https://stackoverflow.com/questions/74621501
复制相似问题