Neda avatar image
Neda asked

How to use one table multiple times for inner join in SQL Server?

I am trying to make a table that includes join between 3 tables in the MSSS 2008. There is a fact table, a date table, and a course table. I should join them to make a base table. In date table there is a one parameter that name is Academic Year lookup, and the values in this parameter is like 2000/1, 2001/2. This parameter in the base table should separate to three parameter such as CensusYear, StartYear, and ApplicationYear. Therefore, I need the data table multiple times. I executed a inner join query, and already I have four inner join statement, but I am getting some extra years, and I'm losing some years. I believe, my query should be wrong somewhere. The attached file is include the design view that created in the MS Access, it'll help to see the tables, and understand what I need to create. [design view that created in the MS Access][1] [Query that excuted to create a table in MSSS 2008][2] [1]: /storage/temp/3820-design-view-in-ms-access.docx [2]: /storage/temp/3821-basetable.txt
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

GPO avatar image
GPO answered
In your table Student.FactApplicationSnapshot do you have any NULL values in the columns CensusDateSK, AcademicYearStartDateSK, ApplicationDateSK or CourseSK? If so you might be inadvertently knocking out rows you want to see because of your INNER JOINs. If you LEFT JOIN from Student.FactApplicationSnapshot to any of those tables in which NULLs might come into play, you might have better luck. Be careful though to alter your WHERE clause as well though, as it will have the effect of turning the LEFT JOINs back into INNER JOINs where it refers to tables being joined to. SELECT appl.EventCount ,cens.AcademicYearLookup AS CensusYear ,acyr.AcademicYearLookup AS StartYear ,apdt.AcademicYearLookup AS ApplicationYear ,cens.CurrentWeekComparisonFlag ,cens.AcademicWeekOfYear ,CASE WHEN appl.ApplicationCensusSK = 1 THEN 'Same Year' WHEN appl.ApplicationCensusSK = 2 THEN 'Next Year' WHEN appl.ApplicationCensusSK = 5 THEN 'Last Year' ELSE 'Other' END AS CensusYearDescription ,cens.CurrentAcademicYear ,appl.StudentCodeBK ,appl.ApplicationSequenceNoBK ,appl.CourseSK ,appl.CourseGroupSK ,appl.CourseMoaSK ,appl.CboSK ,appl.CourseTaughtAbroadSK ,appl.ApplicationStatusSK ,appl.ApplicationFeeStatusSK ,appl.DecisionResponseSK ,appl.NationalityCountrySK ,appl.DomicileCountrySK ,appl.TargetRegionSK ,appl.InternationalSponsorSK INTO dbo.BaseTable3yrs FROM Student.FactApplicationSnapshot AS appl LEFT JOIN Conformed.DimDate AS cens ON appl.CensusDateSK = cens.DateSK AND cens.CurrentAcademicYear In (0,-1) LEFT JOIN Conformed.DimDate AS acyr ON appl.AcademicYearStartDateSK = acyr.DateSK LEFT JOIN Conformed.DimDate AS apdt ON appl.ApplicationDateSK = apdt.DateSK LEFT JOIN Student.DimCourse cors ON appl.CourseSK = cors.CourseSK AND cors.DepartmentShortName = 'TEACH ED' WHERE appl.ApplicationCensusSK In (1,2,5) ;
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Neda avatar image Neda commented ·
Thank you GPO for your answer, and advice. I am not sure that there is NULL values or not, as I remember now it wasn't any NULL values. As you see in the design-view-in-ms-access.docx that I've attached, MS Access queries also include INNER Join. So, if there wasn't any NULL values, could you please let me know what's the alternative solution instead of LEFT JOIN?
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.