Referencing field values between queries

前端 未结 2 1676
星月不相逢
星月不相逢 2021-01-26 06:17

Im trying to create a calculation in Access through the use of queries. At the moment one query calculates the value of \'MPP Oil\' (max production potential) and another query

相关标签:
2条回答
  • 2021-01-26 06:52

    From the error you mentioned in the comments:

    the error 'You tried to execute a query that does not include the specified expression 'CompanyName' as part of an aggregate function'

    Use of aggregate functions require you to group by the columns that appear in the SELECT list other than the aggregated columns.

    Edit:

    I think this is what you are looking for:

    SELECT
        qb1.CompanyName, 
        qb1.AssetName, 
        qb1.Year, 
        qb3.MPPOilRevised - TotalDataPointValue - TotalDataPointValueFactor
    FROM
        ((
            SELECT 
                qb1.CompanyName, 
                qb1.AssetName, 
                qb1.Year, 
                SUM(qb1.DatapointValue) 'TotalDataPointValue',
                SUM(qb2.DatapointValue * 1000000) 'TotalDataPointValueFactor'
            FROM 
                (PEBaseQuery AS qb1 
                INNER JOIN PEBaseQuery AS qb2 
                ON qb1.Year = qb2.Year AND qb1.AssetName = qb2.AssetName)
            WHERE 
                qb1.DatapointID in (2033, 2035, 2043, 2037, 2031) 
            AND qb2.DatapointID = 2003
            GROUP BY qb1.CompanyName, qb1.AssetName, qb1.Year
        ) qb1
        INNER JOIN PE_MPPOilRevised AS qb3 
        ON qb1.Year = qb3.Year AND qb1.AssetName=qb3.AssetName)
    
    0 讨论(0)
  • 2021-01-26 06:59

    When you create an aggregate query, every selected field must either be aggregated or grouped by.

    Try:

    SELECT 
        qb1.CompanyName, 
        qb1.AssetName, 
        qb1.Year, 
        (qb3.MPPOilRevised 
         - SUM(qb1.DatapointValue) 
         - SUM(qb2.DatapointValue * 1000000)) AS Result
    FROM 
        PEBaseQuery AS qb1 
        INNER JOIN PEBaseQuery AS qb2 
        ON qb1.Year = qb2.Year AND qb1.Assetname=qb2.AssetName
        INNER JOIN PE_MPPOilRevised AS qb3 
        ON qb1.Year = qb3.Year AND qb1.Assetname=qb3.AssetName
    WHERE 
        qb1.DatapointID in (2033, 2035, 2043, 2037, 2031) 
    AND qb2.DatapointID=2003;
    GROUP BY
        qb1.CompanyName, 
        qb1.AssetName, 
        qb1.Year
    
    0 讨论(0)
提交回复
热议问题