0

我有一个有点简单的查询,我在其中返回 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;

然后,我在代码中按 [Year] 对结果进行分组,其中有超过 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();

然而,我的上述查询的问题是我得到了一个笛卡尔连接,因此导致我分组的每条记录的输出为“多个雇主”(下面是 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

这是我第一次处理笛卡尔连接。有谁知道在我的查询中解决此问题的可能方法?

4

1 回答 1

0

原来我是在错误的领域加入的Tbl1Tbl2应该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;
于 2014-10-09T21:13:04.623 回答