Sql 查询太复杂

Sql 查询太复杂,sql,ms-access-2010,Sql,Ms Access 2010,我正试图将一些计算从Excel移到Access数据库中,但当我使用上述5个输入值时,我遇到了“查询太复杂”错误。我应该将查询拆分,还是有更有效的解决方法??任何帮助都将不胜感激!代码如下: SELECT qb1.CompanyName, qb1.Assetname, qb1.Year, ((qb1.DatapointValue*1000000)+qb2.DatapointValue+ qb3.DatapointValue+qb4.DatapointValue+qb5.

我正试图将一些计算从Excel移到Access数据库中,但当我使用上述5个输入值时,我遇到了“查询太复杂”错误。我应该将查询拆分,还是有更有效的解决方法??任何帮助都将不胜感激!代码如下:

SELECT qb1.CompanyName, qb1.Assetname, qb1.Year,
      ((qb1.DatapointValue*1000000)+qb2.DatapointValue+
        qb3.DatapointValue+qb4.DatapointValue+qb5.DatapointValue+
        qb6.DatapointValue) AS MPPOilRevised

FROM ((((((PEBaseQuery AS qb1 
INNER JOIN PEBaseQuery AS qb2 ON qb1.AssetName=qb2.AssetName) 
INNER JOIN PEBaseQuery AS qb3 ON qb1.AssetName=qb3.AssetName) 
INNER JOIN PEBaseQuery AS qb4 ON qb1.AssetName=qb4.AssetName) 
INNER JOIN PEBaseQuery AS qb5 ON qb1.AssetName=qb5.AssetName) 
INNER JOIN PEBaseQuery AS qb6 ON qb1.AssetName=qb6.AssetName))

WHERE qb1.DatapointID=2003 And qb2.DatapointID=2032 
      And qb3.DatapointID=2034 And qb4.DatapointID=2042 
      And qb5.DatapointID=2036 And qb6.DatapointID=2030;

看起来您需要一个聚合查询,而不是这个复杂的查询。例如

select companyName, assetName, year, 
   Sum(DatapointValue) as MPPOilRevised
from PEBaseQuery 
where DatapointID in (2032, 2034, 2042, 2036)
group by companyName, assetName, year
唯一的问题是第一个数据点乘以1000000。你可以试试IIF:

select companyName, assetName, year, 
   Sum(IIf(DatapointID=2003,DatapointValue*1000000,DatapointValue)) as MPPOilRevised
from PEBaseQuery 
where DatapointID in (2032, 2034, 2042, 2036)
group by companyName, assetName, year
另外,请尝试这种“疯狂”查询,使用此特定DatapointID的子查询,而不使用IIF:

更新“最大生产潜力”。请尝试以下操作:

select b.companyName, b.assetName, IIf(b.calculationResult > mp.calculationResult,b.calculationResult,mp.calculationResult)  as MPPOilRevised
from 
    (select companyName, assetName, year, Sum(IIf(DatapointID=2003,DatapointValue*1000000,DatapointValue)) as calculationResult
        from PEBaseQuery 
        where DatapointID in (2032, 2034, 2042, 2036) 
        group by companyName, assetName, year) b --Base
     left join 
     (select companyName, assetName, year, 
        Sum(DatapointValue) as calculationResult
        from PEBaseQuery 
        where DatapointID = 2218
        group by companyName, assetName, year) mp -- Max Potential
    on b.companyName= mp.companyName
        and b.assetName = mp.assetName
        and b.year = mp.year
使用减法逻辑进行计算的示例。用最终疯狂SQL更新。还请注意,对于这类事情,我将使用应用程序逻辑或存储过程:

    select b.companyName, b.assetName, IIf(b.calculationResult > mp.calculationResult,b.calculationResult,mp.calculationResult)  as MPPOilRevised
from 
    (select companyName, assetName, year, SUM(DatapointValue) 
        + (select SUM(DatapointValue * 1000000) from  PEBaseQuery q2 
           where q2.companyName = q1.companyName 
              and q2.assetName= q1.assetName
              and q2.year= q1.year
              and q2.DatapointID = 2003
           group by companyName, assetName, year)
        - (select SUM(DatapointValue) from  PEBaseQuery q2 
           where q2.companyName = q1.companyName 
              and q2.assetName= q1.assetName
              and q2.year= q1.year
              and q2.DatapointID = 2029 
           group by companyName, assetName, year)
    from PEBaseQuery q1
    where DatapointID in (2032, 2034, 2042, 2036)
    group by companyName, assetName, year) b --Base
     left join 
     (select companyName, assetName, year, 
        Sum(DatapointValue) as calculationResult
        from PEBaseQuery 
        where DatapointID = 2218
        group by companyName, assetName, year) mp -- Max Potential
    on b.companyName= mp.companyName
        and b.assetName = mp.assetName
        and b.year = mp.year

尝试将qbX.DatapointValue操作数展平,从中选择qbX.DatapointValue。。。哪里例如:一些单独的问题。。为什么第一个DatapointValue需要乘以1000000?看起来很奇怪,因为这些值来自一列。我认为有一种方法可以让整个查询变得更简单,不用它。乘以一百万是因为我需要每百万桶石油的价值,我想你可以在IIf中加入一个值来进行乘法。我用修正后的DatapointID='2003',DatapointValue*1000000,DatapointValue,但它表示“条件表达式中的数据类型不匹配”,它应该读取SumIIfDatapointID=2003,DatapointValue*1000000,DatapointValue,根据此更正更新,thx。还增加了一个变体,类似的对Oracle很好。非常感谢您的帮助!两个版本都有效:我可能在这里碰运气,但我还有一个问题。。我需要在IF语句中包含这一点,即如果DatapointID 2218最大生产潜力的DatapointValue小于上述计算的结果,则MPPOilRevised=计算值,否则MPPOilRevised=2218的值
select b.companyName, b.assetName, IIf(b.calculationResult > mp.calculationResult,b.calculationResult,mp.calculationResult)  as MPPOilRevised
from 
    (select companyName, assetName, year, Sum(IIf(DatapointID=2003,DatapointValue*1000000,DatapointValue)) as calculationResult
        from PEBaseQuery 
        where DatapointID in (2032, 2034, 2042, 2036) 
        group by companyName, assetName, year) b --Base
     left join 
     (select companyName, assetName, year, 
        Sum(DatapointValue) as calculationResult
        from PEBaseQuery 
        where DatapointID = 2218
        group by companyName, assetName, year) mp -- Max Potential
    on b.companyName= mp.companyName
        and b.assetName = mp.assetName
        and b.year = mp.year
    select b.companyName, b.assetName, IIf(b.calculationResult > mp.calculationResult,b.calculationResult,mp.calculationResult)  as MPPOilRevised
from 
    (select companyName, assetName, year, SUM(DatapointValue) 
        + (select SUM(DatapointValue * 1000000) from  PEBaseQuery q2 
           where q2.companyName = q1.companyName 
              and q2.assetName= q1.assetName
              and q2.year= q1.year
              and q2.DatapointID = 2003
           group by companyName, assetName, year)
        - (select SUM(DatapointValue) from  PEBaseQuery q2 
           where q2.companyName = q1.companyName 
              and q2.assetName= q1.assetName
              and q2.year= q1.year
              and q2.DatapointID = 2029 
           group by companyName, assetName, year)
    from PEBaseQuery q1
    where DatapointID in (2032, 2034, 2042, 2036)
    group by companyName, assetName, year) b --Base
     left join 
     (select companyName, assetName, year, 
        Sum(DatapointValue) as calculationResult
        from PEBaseQuery 
        where DatapointID = 2218
        group by companyName, assetName, year) mp -- Max Potential
    on b.companyName= mp.companyName
        and b.assetName = mp.assetName
        and b.year = mp.year