Avatar of mkamp81
mkamp81
 asked on

Sql Question

I have this dataset from my query

section_id      user_id      Role              Status      Entity_ID       Course_Name
592253             12345       Student      Active      001                   PRINCIPLES OF TECHNOLOGY A
592255             12345      Student      Active      001                  ADVANCED PLANT & SOIL SCIENCE
593571             12345      Student      Active      002                  PRINCIPLES OF TECHNOLOGY A
593573             12345      Student      Active      002                  ADVANCED PLANT & SOIL SCIENCE

You can see I have user 12345 that have both entity_ID 001 and 002, but if you notice the course names are the same for 001 as they are for 002. 002 is the campus that the student resides on, he then goes to 001 to complete the course( cross-entity).

What I need to accomplish is in the data extract only pull the entity that the student resides on, which in this case is 002, and the course information. I do not want to pull the 001 data.

Now, this is just an example, I have students at other entities like 010, 003, 012, etc that I am in the same boat with. Additionally, I have students that are at 001, so those are good because they are not going anywhere else to do the class since they are on that home campus.
SELECT DISTINCT 
                      ZZ_TEST_SIS_AllCourses.Course_ID AS section_id, ZZ_TEST_SISCombinedScheduleandDemographics.StudentInternalID AS user_id, 'Student' AS Role, 
                      'Active' AS Status, ZZ_TEST_SIS_AllCourses.Entity_ID, ZZ_TEST_SIS_AllCourses.Course_Name
FROM         ZZ_TEST_SIS_AllCourses INNER JOIN
                      ZZ_TEST_SISCombinedScheduleandDemographics ON ZZ_TEST_SIS_AllCourses.Course_Number = ZZ_TEST_SISCombinedScheduleandDemographics.Course AND 
                      ZZ_TEST_SIS_AllCourses.sky_Sect = ZZ_TEST_SISCombinedScheduleandDemographics.Section
WHERE     (ZZ_TEST_SIS_AllCourses.Course_ID IN ('592253', '592255', '592266', '593571', '593573', '593585', '595105', '595207')) AND 
                      (ZZ_TEST_SISCombinedScheduleandDemographics.StudentInternalID = '12345')

Open in new window


Does anyone have an idea on how I can accomplish this?  Thank you so much
Microsoft SQL Server

Avatar of undefined
Last Comment
zephyr_hex (Megan)

8/22/2022 - Mon
zephyr_hex (Megan)

If students are at other entities, how do we know which one you want?  Or is there only ever one other entity possible, and in that case, you want the one that isn't 001 ?
Randy Peterson

This can be done if you can tell me how to identify the campus the student resides on.  I guess the question is, if the student doesn't reside on the home campus (001)  will their campus always have a duplicate entry for 001?
Duy Pham

I don't really understand what you are trying to accomplish. If you don't want 001 data, why don't you just filter it out (by adding another condition into WHERE clause like AND ZZ_TEST_SIS_AllCourses.Entity_ID <> '001')?

Or you are trying to get the courses that student resides on and also completes those courses at home campus (001)? If that is the case, you can add another contidion into WHERE clause to only get the courses that student completes at home campus like AND ZZ_TEST_SIS_AllCourses.Course_Name IN (SELECT ZZ_TEST_SIS_AllCourses.Course_Name FROM ZZ_TEST_SIS_AllCourses INNER JOIN ZZ_TEST_SISCombinedScheduleandDemographics ON ZZ_TEST_SIS_AllCourses.Course_Number = ZZ_TEST_SISCombinedScheduleandDemographics.Course AND ZZ_TEST_SIS_AllCourses.sky_Sect = ZZ_TEST_SISCombinedScheduleandDemographics.Section WHERE (ZZ_TEST_SIS_AllCourses.Course_ID IN ('592253', '592255', '592266', '593571', '593573', '593585', '595105', '595207')) AND (ZZ_TEST_SISCombinedScheduleandDemographics.StudentInternalID = '12345') AND (ZZ_TEST_SIS_AllCourses.Entity_ID = '001'))

Code sample could look like:
SELECT DISTINCT 
                      ZZ_TEST_SIS_AllCourses.Course_ID AS section_id, ZZ_TEST_SISCombinedScheduleandDemographics.StudentInternalID AS user_id, 'Student' AS Role, 
                      'Active' AS Status, ZZ_TEST_SIS_AllCourses.Entity_ID, ZZ_TEST_SIS_AllCourses.Course_Name
FROM         ZZ_TEST_SIS_AllCourses INNER JOIN
                      ZZ_TEST_SISCombinedScheduleandDemographics ON ZZ_TEST_SIS_AllCourses.Course_Number = ZZ_TEST_SISCombinedScheduleandDemographics.Course AND 
                      ZZ_TEST_SIS_AllCourses.sky_Sect = ZZ_TEST_SISCombinedScheduleandDemographics.Section
WHERE     (ZZ_TEST_SIS_AllCourses.Course_ID IN ('592253', '592255', '592266', '593571', '593573', '593585', '595105', '595207')) AND 
                      (ZZ_TEST_SISCombinedScheduleandDemographics.StudentInternalID = '12345') AND

-- if filtering out only 001 - home campus
                      ( ZZ_TEST_SIS_AllCourses.Entity_ID <> '001) AND

-- if we need to make sure the courses also be completed by the student at home campus
                      (ZZ_TEST_SIS_AllCourses.Course_Name IN (
                              SELECT ZZ_TEST_SIS_AllCourses.Course_Name
                              FROM   ZZ_TEST_SIS_AllCourses INNER JOIN
                                            ZZ_TEST_SISCombinedScheduleandDemographics ON ZZ_TEST_SIS_AllCourses.Course_Number = ZZ_TEST_SISCombinedScheduleandDemographics.Course AND ZZ_TEST_SIS_AllCourses.sky_Sect = ZZ_TEST_SISCombinedScheduleandDemographics.Section
                              WHERE (ZZ_TEST_SIS_AllCourses.Course_ID IN ('592253', '592255', '592266', '593571', '593573', '593585', '595105', '595207')) AND (ZZ_TEST_SISCombinedScheduleandDemographics.StudentInternalID = '12345') AND (ZZ_TEST_SIS_AllCourses.Entity_ID = '001')))

Open in new window

Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
mkamp81

ASKER
There is a field called School number I brought in. That is how I know which school the student is from.  
The ones highlighted in the yellow are the ones I need. Where the school number and the away school match. So if there is a 002 as the school number and away school has 002 that is what I need.

2016-09-12_13-40-02.png
SELECT DISTINCT 
                      ZZ_TEST_SIS_AllCourses.Course_ID AS section_id, ZZ_TEST_SISCombinedScheduleandDemographics.StudentInternalID AS user_id, 'Student' AS Role, 
                      'Active' AS Status, ZZ_TEST_SIS_AllCourses.Course_Name, ZZ_TEST_SISCombinedScheduleandDemographics.SchoolNumber, 
                      ZZ_TEST_SIS_AllCourses.Entity_ID as 'Away School'
FROM         ZZ_TEST_SIS_AllCourses INNER JOIN
                      ZZ_TEST_SISCombinedScheduleandDemographics ON ZZ_TEST_SIS_AllCourses.Course_Number = ZZ_TEST_SISCombinedScheduleandDemographics.Course AND 
                      ZZ_TEST_SIS_AllCourses.sky_Sect = ZZ_TEST_SISCombinedScheduleandDemographics.Section
WHERE     (ZZ_TEST_SISCombinedScheduleandDemographics.StudentInternalID = '12346')

Open in new window

ASKER CERTIFIED SOLUTION
zephyr_hex (Megan)

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
mkamp81

ASKER
Thank you, I had figured that out before I saw that you posted this. I guess I just needed to get ideas then that got me seeing what I was doing incorrectly. Again thank you
zephyr_hex (Megan)

Glad you figured it out.  Sometimes I find I end up solving my own problem just in the process of thinking through how I would explain the problem when posting an EE question.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.