组合2个表,没有共同的日期

时间:2013-07-25 18:32:36

标签: sql sql-server-2008

我正在尝试将两个没有共同日期的表组合成一个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的CaseIds。另外,我不确定如何将两个日期字段合并为一个。任何建议都表示赞赏!

3 个答案:

答案 0 :(得分: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

Union operation

答案 1 :(得分: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

答案 2 :(得分: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