0

我不明白为什么我收到错误

“指定字段 qb2.WellworkOIL 可以引用 SQL 语句中 FROM 子句中列出的多个表”

根据下面的查询

SELECT 
    qb1.CompanyName, 
    qb1.AssetName, 
    qb1.Year, 
    IIf(qb1.PrimaryProductionType='Oil', 
        qb2.WellworkOIL, 
        IIF(qb1.PrimaryProductionType='Gas', 
            qb3.WellworkGAS, 
            qb4.WellworkOilGas)) AS Selected
FROM 
    (
        (
            PEBaseQueryConditions AS qb1 
            INNER JOIN PE_Field_WellworkOIL AS qb2 
            ON (qb1.Year=qb2.Year) AND (qb1.AssetName=qb2.AssetName)
        ) 
        INNER JOIN PE_Field_WellworkGAS AS qb3 
        ON (qb1.Year=qb3.Year) AND (qb1.AssetName=qb3.AssetName)
    ) 
    INNER JOIN PE_Field_WellworkOILGAS AS qb4 
    ON (qb1.Year=qb4.Year) AND (qb1.AssetName=qb4.AssetName)
GROUP BY 
    qb1.CompanyName, 
    qb1.AssetName, 
    qb1.Year, 
    qb1.PrimaryProductionType,
    qb2.WellworkOIL, 
    qb3.WellworkGAS, 
    qb4.WellworkOilGas;
4

2 回答 2

0

没有时间尝试这个(如果这不起作用,请道歉)但我之前没有见过别名语法(我认为你不需要“AS”) - 试试这个;

SELECT qb1.CompanyName, qb1.AssetName, qb1.Year, 
       IIf(qb1.PrimaryProductionType='Oil', qb2.WellworkOIL, 
             IIF(qb1.PrimaryProductionType='Gas', qb3.WellworkGAS, 
                           qb4.WellworkOilGas)) AS Selected

FROM PEBaseQueryConditions qb1 
INNER JOIN PE_Field_WellworkOIL qb2 ON (qb1.Year=qb2.Year AND qb1.AssetName=qb2.AssetName)
INNER JOIN PE_Field_WellworkGAS qb3 ON (qb1.Year=qb3.Year AND qb1.AssetName=qb3.AssetName) 
INNER JOIN PE_Field_WellworkOILGAS qb4 ON (qb1.Year=qb4.Year AND qb1.AssetName=qb4.AssetName)

此外,根据 Jon Woo 的建议,如果您不使用聚合函数(例如 COUNT、SUM ...),则不需要“分组依据”

编辑:你也可以失去一些括号

于 2012-08-30T15:47:35.853 回答
0

我认为摆脱你的GROUP BY条款可以消除这个问题。您没有使用聚合函数,因此不需要 group by。

SELECT 
    qb1.CompanyName, 
    qb1.AssetName, 
    qb1.Year, 
    IIf(qb1.PrimaryProductionType='Oil', 
        qb2.WellworkOIL, 
        IIF(qb1.PrimaryProductionType='Gas', 
            qb3.WellworkGAS, 
            qb4.WellworkOilGas)) AS Selected
FROM    PEBaseQueryConditions AS qb1 
            INNER JOIN PE_Field_WellworkOIL AS qb2 
                 ON (qb1.Year=qb2.Year) AND (qb1.AssetName=qb2.AssetName)
            INNER JOIN PE_Field_WellworkGAS AS qb3 
                 ON (qb1.Year=qb3.Year) AND (qb1.AssetName=qb3.AssetName)
            INNER JOIN PE_Field_WellworkOILGAS AS qb4 
                 ON (qb1.Year=qb4.Year) AND (qb1.AssetName=qb4.AssetName)
于 2012-08-30T15:44:59.797 回答