简体   繁体   English

使用多值Count()数据创建Access表达式

[英]Create an Access expression using multi-value Count() data

The data I have is in a few tables. 我的数据在几个表中。 Table 1 has: 表1具有:
[invoice] (key), [发票](密钥),
[invoice total], and [发票总额],以及
[associated assets] (multi-value linked to [assets] in table 2). [相关资产](与表2中的[资产]链接的多值)。

Table two includes: 表二包括:
[Assets] (key), and [资产](键),以及
other asset data fields. 其他资产数据字段。

What I need to do is take the invoice total and divide it by the number of assets that go into that price to determine the cost of each asset. 我需要做的是将发票总额除以该价格中的资产数量,以确定每项资产的成本。 I have used the count() function in a query to get the number of assets for each invoice, but I can't seem to use the data anywhere else. 我在查询中使用了count()函数来获取每个发票的资产数量,但是我似乎无法在其他任何地方使用数据。

I have created another query to use the expression 我创建了另一个查询以使用表达式

= [invoice total]/count([associated assets]

In queries, I keep getting an aggregate error and can't seem to make this work. 在查询中,我不断收到一个汇总错误,似乎无法完成这项工作。 I would like for the expression result to populate a field in table 1. I need help either setting up a working query to pull the data from, or a form item that will pull the data and auto-populate the field. 我希望表达式结果填充表1中的一个字段。我需要帮助来设置一个工作查询以从中提取数据,或者需要一个表单项来提取数据并自动填充该字段。

For a table named [Invoices] 对于名为[发票]的表

invoice  invoice total  associated assets
-------  -------------  -----------------
      1          $3.00  bicycles
      2          $5.00  bicycles, ham
      3          $1.00  

where [associated assets] is a multi-value lookup field against the table [Assets] [关联资产]是针对[资产]表的多值查找字段

AssetID  AssetName
-------  ---------
      1  bicycles 
      2  ham      

The following query 以下查询

SELECT 
    i.invoice,
    i.inv_tot AS [invoice total],
    i.assetCount,
    IIf(i.assetCount=0, NULL, i.[invoice total]/i.assetCount) AS avgCostPerAsset
FROM
    (
        SELECT 
            Invoices.invoice, 
            Min(Invoices.[invoice total]) AS inv_tot,
            Count(Invoices.[associated assets].Value) AS assetCount
        FROM Invoices
        GROUP BY Invoices.invoice
    ) i

produces the following result 产生以下结果

invoice  invoice total  assetCount  avgCostPerAsset
-------  -------------  ----------  ---------------
      1          $3.00           1                3
      2          $5.00           2              2.5
      3          $1.00           0                 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM