Parece que necesita una consulta de agregación, en lugar de esta compleja. P.ej.
select companyName, assetName, year,
Sum(DatapointValue) as MPPOilRevised
from PEBaseQuery
where DatapointID in (2032, 2034, 2042, 2036)
group by companyName, assetName, year
El único problema está multiplicando a 1.000.000 para el primer punto de datos. Usted podría intentar IIF
para ello:
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
También por favor tratar dicha consulta 'loca', la consulta de este sub DatapointID particular, sin IIF
:
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)
from PEBaseQuery q1
where DatapointID in (2032, 2034, 2042, 2036)
group by companyName, assetName, year
Actualización para 'Max Producción Potencial'. Intente lo siguiente:
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
Ejemplo de cálculo con lógica de resta. Actualizado con SQL loco final. Tenga también en cuenta que yo iría con la lógica de aplicaciones o procedimientos almacenados para este tipo de cosas:
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
Trate aplanamiento los operandos '' qbX.DatapointValue' en (SELECT ... qbX.DatapointValue DE DONDE ... .) AS ExprX'. – EthanB
Alguna pregunta por separado ... ¿por qué necesita esta multiplicación a 1000000 para el primer DatapointValue? Parece muy extraño, ya que los valores son de una columna. Creo que existe la manera de hacer toda la consulta mucho más simple sin ella. – udalmik
la multiplicación por un millón se debe a que necesito el valor por millón de barriles de petróleo – Magda