我正在处理将历史记录与预测相结合的存储过程。我有一个列(PHP),它指定投影是否优先于历史记录。我还有一个列,指定数据是否来自历史记录或投影表。我的存储过程的输出是这样的:优先消除重复行
CaseId Year Projection PHP Gas Oil
1 2004 0 1
1 2005 0 1
1 2005 1 1
1 2006 1 1
1 2007 1 1
1 2008 1 1
1 2009 1 1
2 2003 0 0
2 2004 0 0
2 2005 0 0
2 2005 1 0
2 2006 1 0
2 2007 1 0
2 2008 1 0
2 2006 1 0
在这个例子中,我需要消除在第二排,因为CaseId 1个投影具有优先,因此重叠的历史日期应该被删除。另外,CaseId 2的第四行应该被删除,因为历史记录具有优先权。
CaseId Year Projection PHP Gas Oil
1 2004 0 1
1 2005 1 1
1 2006 1 1
1 2007 1 1
1 2008 1 1
1 2009 1 1
2 2003 0 0
2 2004 0 0
2 2005 0 0
2 2006 1 0
2 2007 1 0
2 2008 1 0
2 2006 1 0
我需要标记CaseId中的重复年份,然后比较投影和PHP列并删除它们不匹配的行。
SELECT rcl.ReportRunCaseId AS CaseId,
year(rce.EcoDate) as Year,
1 as Projection,
cpq.ProjectionHasPrecedence as PHP,
rce.GrossOil as Oil,
rce.GrossGas as Gas
from phdreports.PhdRpt.ReportCaseList_28 rcl
inner join phdreports.PhdRpt.RptCaseEco_28 rce on
rce.ReportRunCaseId = rcl.ReportRunCaseId
inner join dbo.caseQualifier cq on
cq.CorpScenarioId = 1 and
cq.CaseCaseId = rcl.ReportRunCaseId and
cq.CorpQualifierTypeId = 1
inner join dbo.caseProjectionQualifier cpq on
cpq.CaseCaseId = rcl.ReportRunCaseId and
cpq.CorpQualifierId = cq.QualifierHasData
where rcl.ReportRunCaseId <=2
group by year(rce.EcoDate), rcl.ReportRunCaseId, cpq.ProjectionHasPrecedence, rce.GrossGas, rce.GrossOil
union all
select rmp.ReportRunCaseId AS CaseId,
year(rmp.EcoDate) as Year,
0 as Projection,
cpq.ProjectionHasPrecedence as PHP,
rmp.GrossOil as Oil,
rmp.GrossGas as Gas
from PhdReports.PhdRpt.RptMonthlyProduction_50 rmp
inner join dbo.caseQualifier cq on
cq.CorpScenarioId = 1 and
cq.CaseCaseId = rmp.ReportRunCaseId and
cq.CorpQualifierTypeId = 1
inner join dbo.caseProjectionQualifier cpq on
cpq.CaseCaseId = rmp.ReportRunCaseId and
cpq.CorpQualifierId = cq.QualifierHasData
where rmp.ReportRunCaseId <= 2
group by year(rmp.EcoDate), rmp.ReportRunCaseId, cpq.ProjectionHasPrecedence, rmp.GrossGas, rmp.GrossOil
我怎样才能消除重复年,在那里投影和PHP不匹配:
下面是该查询我一起工作?
我尝试过使用这种方法,它在PHP设置为0时返回投影行。 –
谢谢。我从来不知道你可以按顺序使用一个案例。 –