0

我正在尝试合并两个没有共同日期的表,只有一个 caseid。
这是我的 SQL 代码。我还尝试合并 EcoDate 和 ProductionMonth 列,因为它们都包含日期信息。

SELECT     cmp.ProductionMonth, cmp.ProductionAmount, rce.EcoDate, rcl.CaseCaseId, cmp.CaseCaseId AS CaseId, rce.GrossOil  
FROM         PhdRpt.ReportCaseList_465 AS rcl INNER JOIN
                      PhdRpt.RptCaseEco_465 AS rce ON rcl.ReportRunCaseId = rce.ReportRunCaseId RIGHT OUTER JOIN
                      CaseMonthlyProduction AS cmp ON rcl.CaseCaseId = cmp.CaseCaseId

如果我将此查询作为 2 个不同的查询运行,我将得到如下输出:

CaseCaseId-----EcoDate----GrossOil
12345------------2013-1-1------125.3
12345------------2013-2-1------15.3
12345------------2013-3-1------12.3
12345------------2013-4-1------125.0
12345------------2013-5-1------15.0
12345------------2013-6-1------120.3
12346------------2013-1-1------422.2
12346------------2013-2-1------325.2
12346------------2013-3-1------100.0


CaseId--------ProductionMonth------ProductionAmount
12345------------2016-1-1-----------------223.0
12345------------2016-2-1-----------------254.1
12345------------2016-3-1-----------------652.1
12345------------2016-4-1-----------------255.9
12346------------2016-1-1-----------------111.1
12346------------2016-2-1-----------------621.2

我的输出表应该是这样的:

CaseCaseId-------Date--------GrossOil--------ProductionAmount
12345------------2013-1-1------125.3-----------------null
12345------------2013-2-1------15.3------------------null
12345------------2013-3-1------12.3------------------null
12345------------2013-4-1------125.0-----------------null
12345------------2013-5-1------15.0------------------null
12345------------2013-6-1------120.3-----------------null
12345------------2016-1-1-------null------------------223.0
12345------------2016-2-1-------null------------------254.1
12345------------2016-3-1-------null------------------652.1
12345------------2016-4-1-------null------------------255.9
12346------------2013-1-1------422.2-----------------null
12346------------2013-2-1------325.2-----------------null
12346------------2013-3-1------100.0-----------------null
12346------------2016-1-1-------null------------------111.1
12346------------2016-2-1-------null------------------621.2

当我使用右外连接时,它会返回数据库中的所有 CaseId,而不仅仅是 PhdRpt.ReportCaseList_465 的一部分。另外,我不确定如何将两个日期字段合并为一个。任何建议表示赞赏!

4

3 回答 3

2

尝试这个:

select CaseCaseId AS CaseCaseId, EcoDate AS Date, GrossOil AS GrossOil, NULL AS ProductionAmount FROM table1
union all
select CaseId AS CaseCaseId, ProductionMonth AS Date, NULL AS GrossOil, ProductionAmount AS ProductionAmount FROM table2

联合运营

于 2013-07-25T18:45:55.577 回答
1

你可以 FULL OUTER JOIN,然后使用 ISNULL 来得到你想要的。别忘了也参加约会。

SELECT     isnull(rcl.CaseCaseId,cmp.CaseCaseId) as CaseId,
           isnull(cmp.ProductionMonth,rce.EcoDate) as Date,
           rce.GrossOil, 
           cmp.ProductionAmount
FROM       PhdRpt.ReportCaseList_465 AS rcl 
INNER JOIN PhdRpt.RptCaseEco_465 AS rce 
ON rcl.ReportRunCaseId = rce.ReportRunCaseId 
LEFT OUTER JOIN CaseMonthlyProduction AS cmp 
ON rcl.CaseCaseId = cmp.CaseCaseId
and rce.EcoDate = cmp.ProductionMonth
于 2013-07-25T18:50:11.493 回答
1

我认为您正在尝试做的是这样的事情:

select * from (
select
 rc1.CaseId,
 rce.EcoDat as Date,
 rce.GrossOil,
 0 as ProductionAmount
from phdrpt.reportcaselist_465 as rcl
 inner join phdrpt.rptcaseeco_465 as rce
  on rcl.ReportRunCaseId = rce.ReportRunCaseId
union
 rc1.CaseId,
 cmp.ProductionMonth as Date,
 0 as GrossOil,
 cmp.ProductionAmount
from phdrpt.reportcaselist_465 as rcl
 inner join CaseMonthlyProduction AS cmp
  on rcl.CaseCaseId = cmp.CaseCaseId
) order by date
于 2013-07-25T19:00:52.130 回答