简单的SELECT查询导致笛卡尔加入?

时间:2014-10-09 15:39:37

标签: sql database oracle join query-designer

我有一个简单的查询,我将返回4个字段:[Year][ServiceCredited][Salary][CoveredEmployer]

使用Table1,我可以检索前3个字段,但我无法获取[CoveredEmployer]。要获得[CoveredEmployer],我必须加入Table2,其中包含更多详细信息,例如Members Employer_Org_ID,然后使用Employer_Org_Id将Table2连接到Table3以获取雇主名称,或[CoveredEmployer]

SELECT a.SCHOOL_YEAR as Year, a.TOTAL_SERVICE_CREDIT as ServiceCredited, a.RETIREMENT_SALARY as Salary, c.ORG_NAME as CoveredEmployer 
FROM TBL1 a 
INNER JOIN TBL2 b ON a.MEMBER_ACCOUNT_ID = b.MEMBER_ACCOUNT_ID
INNER JOIN TBL3  c ON b.ORG_ID = c.ORG_ID 
WHERE a.MEMBER_ACCOUNT_ID = 136079
ORDER BY YEAR DESC;

然后,我在代码中按[年份]对结果进行分组,并且有超过1个雇主,其中[CoveredEmployer]值为“多个雇主”:

var lyosGroupedByYear = lyos.GroupBy(x => x.Year).Select(x => new Years_Of_Service()
            {
                Year = x.Key,
                //ServiceCredited = x.Sum(y => y.ServiceCredited),
                //Salary = x.Sum(y => y.Salary),
                ServiceCredited = x.First().ServiceCredited,
                Salary = x.First().Salary,
                CoveredEmployer = x.Count() > 1 ? "Multiple Employers" : x.First().CoveredEmployer
            }).ToList();

我上面的查询的问题是我得到Cartesion加入,从而导致我分组的每条记录的输出“多个雇主”(下面只是2013年和2014年的结果,实际结果是回到1982年同样的问题):

2014            Special School Dist-St. Louis
2014            Special School Dist-St. Louis
2014            Special School Dist-St. Louis
2014            Special School Dist-St. Louis
2014            Special School Dist-St. Louis
2014            Special School Dist-St. Louis
2014            Special School Dist-St. Louis
2014            Special School Dist-St. Louis
2014            Special School Dist-St. Louis
2014            Special School Dist-St. Louis
2014            Special School Dist-St. Louis
2014            Special School Dist-St. Louis
2014            Special School Dist-St. Louis
2014            Special School Dist-St. Louis
2014            Special School Dist-St. Louis
2014            Special School Dist-St. Louis
2014            Special School Dist-St. Louis
2014            Special School Dist-St. Louis
2014            Special School Dist-St. Louis
2014            Special School Dist-St. Louis
2014            Special School Dist-St. Louis
2014            Special School Dist-St. Louis
2014            Special School Dist-St. Louis
2014            Hazelwood R-I Schools
2014            St. Louis Community College
2014            Special School Dist-St. Louis
2014            St. Louis Community College
2014            Special School Dist-St. Louis
2014            St. Louis Community College
2014            Special School Dist-St. Louis
2014            St. Louis Community College
2014            Special School Dist-St. Louis
2014            St. Louis Community College
2014            Special School Dist-St. Louis
2014            St. Louis Community College
2014            Special School Dist-St. Louis
2014            St. Louis Community College
2014            St. Louis Community College
2013    1   95476.8 Special School Dist-St. Louis
2013    1   95476.8 Special School Dist-St. Louis
2013    1   95476.8 Special School Dist-St. Louis
2013    1   95476.8 Special School Dist-St. Louis
2013    1   95476.8 Special School Dist-St. Louis
2013    1   95476.8 Hazelwood R-I Schools
2013    1   95476.8 St. Louis Community College
2013    1   95476.8 Special School Dist-St. Louis
2013    1   95476.8 St. Louis Community College
2013    1   95476.8 Special School Dist-St. Louis
2013    1   95476.8 St. Louis Community College
2013    1   95476.8 Special School Dist-St. Louis
2013    1   95476.8 St. Louis Community College
2013    1   95476.8 Special School Dist-St. Louis
2013    1   95476.8 St. Louis Community College
2013    1   95476.8 Special School Dist-St. Louis
2013    1   95476.8 St. Louis Community College
2013    1   95476.8 Special School Dist-St. Louis
2013    1   95476.8 St. Louis Community College
2013    1   95476.8 Special School Dist-St. Louis
2013    1   95476.8 Special School Dist-St. Louis
2013    1   95476.8 St. Louis Community College
2013    1   95476.8 Special School Dist-St. Louis
2013    1   95476.8 Special School Dist-St. Louis
2013    1   95476.8 Special School Dist-St. Louis
2013    1   95476.8 Special School Dist-St. Louis
2013    1   95476.8 Special School Dist-St. Louis
2013    1   95476.8 Special School Dist-St. Louis
2013    1   95476.8 Special School Dist-St. Louis
2013    1   95476.8 Special School Dist-St. Louis
2013    1   95476.8 Special School Dist-St. Louis
2013    1   95476.8 Special School Dist-St. Louis
2013    1   95476.8 Special School Dist-St. Louis
2013    1   95476.8 Special School Dist-St. Louis
2013    1   95476.8 Special School Dist-St. Louis
2013    1   95476.8 Special School Dist-St. Louis
2013    1   95476.8 Special School Dist-St. Louis
2013    1   95476.8 Special School Dist-St. Louis

这是我第一次与笛卡尔联合打交道。有谁知道在我的查询中修复此问题的可能方法?

1 个答案:

答案 0 :(得分:0)

原来我加入了Tbl1& Tbl2在错误的字段上。应该是MEMBER_CONTR_ID而不是MEMBER_ACCOUNT_ID

SELECT a.SCHOOL_YEAR as Year, a.TOTAL_SERVICE_CREDIT as ServiceCredited, a.RETIREMENT_SALARY as Salary, c.ORG_NAME as CoveredEmployer 
FROM TBL1 a 
INNER JOIN TBL2 b ON a.MEMBER_CONTR_ID = b.MEMBER_CONTR_ID
INNER JOIN TBL3  c ON b.ORG_ID = c.ORG_ID 
WHERE a.MEMBER_ACCOUNT_ID = 136079
ORDER BY YEAR DESC;