我有一個簡單的查詢,我返回4個字段:[Year]
,[ServiceCredited]
,[Salary]
和[CoveredEmployer]
。簡單的SELECT查詢導致笛卡爾連接?
使用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年的結果,實際結果回到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
這是我第一次處理笛卡爾聯接。有沒有人知道在我的查詢中解決這個問題的可能方法?
所以,你必須爲每個'tbl1'記錄每個'tbl2'記錄多個'tbl2'記錄,和/或多個'tbl3'記錄?其中一個最終結果的原始數據樣本可能會有所幫助。你爲什麼不在你的查詢中分組/計數? – 2014-10-09 16:01:04
感謝您的回覆亞歷克斯。我最終弄清楚了這個問題。我有'JOIN'正確的Tbl2&Tbl3,但是對於Tbl1&Tbl2,我使用了錯誤的Key值作爲Join。我做了一些其他修改,這是我的問題的來源。 – 2014-10-09 16:30:24
@AnalyticLunatic祝賀你自己解決了你的問題。你能否請你張貼 - 並自己接受 - 你的解決方案,所以這個問題可能會顯示爲「回答」。這對未來的訪客很重要。 – 2014-10-09 17:52:49