【发布时间】:2014-07-31 12:49:06
【问题描述】:
我正在研究一个将历史与预测相结合的存储过程。我有一个位列(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 中标记重复年份,然后比较 Projection 和 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
如何消除 Projection 和 PHP 不匹配的重复年份?
【问题讨论】:
标签: sql sql-server sql-server-2012 duplicates