0

我有一个存储过程,它需要 1 个参数,一个 ID 号(systudentid)。

该过程返回 3 行:学生的学术顾问 (AC)、财务顾问 (FC) 和招生顾问 (EC) 以及相关的联系信息;3个不同的人。

某些学生的 AC 和 FC 是同一个人,但查询仍将返回 3 行。

AdvisorType|AdvisorLastName|AdvisorFirstName|(other data)|systaffID

AC          DOE             JOHN             .....        12345

AC          DOE             JOHN             .....        12345

EC          SMITH           JANE             .....        45678

在我的代码中,我可以在哪里插入逻辑(以及如何插入,我是 sql 的新手),以便当传递给程序的 systudentid 识别出一个学生在 AC 和 FC 中具有相同的人时,它将显示结果方法。

顾问类型更改为“SSA”,并且只返回一个双重职责顾问的记录。

AdvisorType|AdvisorLastName|AdvisorFirstName|(other data)|SystaffID

SSA         DOE             JOHN             ......       12345

EC          SMITH           JANE             ......       45678

这是我的选择声明:

SELECT 
        SyStaffGroup.Descrip AS AdvisorType
        ,SyStaff.LastName AS AdvisorLastName
        ,SyStaff.FirstName      AS AdvisorFirstName
        ,SyStaff.Phone          AS AdvisorPhone
        ,SyStaff.Ext            AS AdvisorExtention
        ,SyStaff.eMail          AS AdvisorEMail
        ,SyStaff.SyStaffID      AS SyStaffID

    FROM SyStaff (NOLOCK)
    JOIN SyAdvisorByEnroll (NOLOCK)
      ON SyAdvisorByEnroll.SyStaffID = SyStaff.SyStaffID
    JOIN SyStaffGroup (NOLOCK)
      ON SyStaffGroup.SyStaffGroupID = SyAdvisorByEnroll.SyStaffGroupID
    JOIN AdEnroll (NOLOCK)
      ON AdEnroll.AdEnrollID = SyAdvisorByEnroll.AdEnrollID
    JOIN SyStudent (NOLOCK)
        ON AdEnroll.SyStudentID = SyStudent.SyStudentId

  WHERE 
        SyStaff.Active = 1

        --AND
        --syadvisorbyenroll.adenrollid = (
        --SELECT adenrollid from dbo.fn_student_enrollment_activeenrollmentlist (@systudentid)
        --)   

        AND adEnroll.adEnrollID IN (
              SELECT adEnrollID FROM dbo.fn_Student_Enrollment_ActiveEnrollmentList(@SyStudentID)      
        )
  AND SyAdvisorByEnroll.AdvisorModule IN ('AD','FA')
  AND SyStaffGroup.Descrip IN ('AC - Academic Counselor', 'FC - Finance Counselors', 'EC - Adm. Counselor With Reg') 

UNION

SELECT DISTINCT
      'Admissions Counselor' AS AdvisorType
            ,SyStaff.LastName AS AdvisorLastName
            ,SyStaff.FirstName      AS AdvisorFirstName
            ,SyStaff.Phone          AS AdvisorPhone
            ,SyStaff.Ext            AS AdvisorExtention
            ,SyStaff.eMail          AS AdvisorEMail
            ,SyStaff.SyStaffID      AS SyStaffID
FROM systudent
      INNER JOIN AmRep ON SyStudent.AMREpID = AmREp.AMREpid
      INNER JOIN SyStaff ON SyStaff.SyStaffID = AmRep.AmRepID
WHERE Systudent.SYStudentid = @systudentid

任何我可以尝试或谷歌的提示或建议的方法(我已经尝试过搜索,但如果我知道要寻找什么,结果会更有用)将不胜感激。

4

1 回答 1

0

您可以添加一个嵌套子查询来指示哪些学生有相同的顾问填补多个职位,并相应地调整类型选择。以下是上述查询的更改部分:

SELECT 
    CASE WHEN (mutiples.SyStaffID IS NOT NULL) THEN 'SSA' 
    ELSE SyStaffGroup.Descrip END AS AdvisorType
    -- other columns omitted
FROM SyStaff (NOLOCK)
JOIN SyAdvisorByEnroll (NOLOCK)
  ON SyAdvisorByEnroll.SyStaffID = SyStaff.SyStaffID
LEFT JOIN (
    SELECT SyStaffID,AdEnrollID
    FROM SyAdvisorByEnroll
    GROUP BY SyStaffID,AdEnrollID
    HAVING COUNT(DISTINCT SyStaffGroupID) > 1
) multiples
  ON multiples.SyStaffID = SyAdvisorByEnroll.SyStaffID
  AND multiples.AdEnrollID = SyAdvisorByEnroll.AdEnrollID
-- rest of query omitted

这可能有一两个错误,因为您没有包含表模式。嵌套子查询“multiples”包含顾问位于多个组中的所有顾问/登记者对。如果嵌套子查询中有匹配的条目,则您对此进行了左连接并将最终类型选择调整为“SSA”。

重要说明:正如所写,这将包括符合条件的顾问/登记者对的两个 SSA 行。但是,最终结果不会,因为您在此查询中使用了 UNION,它会过滤掉重复项,即使它们只存在于联合的一半中。如果将其更改为 UNION ALL 或完全消除 UNION,则需要将 DISTINCT 添加到查询的顶部,如下所示:

SELECT DISTINCT CASE WHEN (mutiples.SyStaffID IS NOT NULL) ...
于 2013-06-21T20:15:55.087 回答